com.softicar.platform.common.container.map.list.AbstractListMap Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of platform-common Show documentation
Show all versions of platform-common Show documentation
The SoftiCAR Platform is a lightweight, Java-based library to create interactive business web applications.
package com.softicar.platform.common.container.map.list;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
/**
* Map from a key to a list of values.
*
* @param
* the key class
* @param
* the values in the sets
* @author Boris Schaa
* @author Oliver Richers
*/
public abstract class AbstractListMap implements IListMap {
private final Map> lists;
public AbstractListMap(Map> lists) {
this.lists = lists;
}
// -------------------- read-only -------------------- //
@Override
public boolean containsKey(K key) {
return lists.containsKey(key);
}
@Override
public boolean isEmpty() {
return lists.isEmpty();
}
@Override
public int size() {
return lists.size();
}
@Override
public Set keySet() {
return lists.keySet();
}
@Override
public Set>> entrySet() {
return lists.entrySet();
}
@Override
public Collection> values() {
return lists.values();
}
@Override
public List getList(K key) {
List list = lists.get(key);
if (list == null) {
lists.put(key, list = new ArrayList<>());
}
return list;
}
@Override
public List getListsContent() {
List contentList = new ArrayList<>();
for (List list: values()) {
for (V v: list) {
contentList.add(v);
}
}
return contentList;
}
// -------------------- mutating -------------------- //
@Override
public void clear() {
lists.clear();
}
@Override
public List removeKey(K key) {
if (key == null) {
return null;
}
List result = lists.get(key);
lists.remove(key);
return result;
}
@Override
public void addToList(K key, V value) {
List list = lists.get(key);
if (list == null) {
lists.put(key, list = new ArrayList<>());
}
list.add(value);
}
@Override
public void addAllToList(K key, Collection values) {
List list = lists.get(key);
if (list == null) {
lists.put(key, new ArrayList<>(values));
} else {
list.addAll(values);
}
}
// -------------------- standard methods -------------------- //
@Override
public boolean equals(Object object) {
if (object instanceof AbstractListMap) {
return Objects.equals(lists, ((AbstractListMap) object).lists);
} else {
return false;
}
}
@Override
public int hashCode() {
return lists.hashCode();
}
@Override
public String toString() {
String result = "";
int maxLength = 0;
for (K k: keySet()) {
maxLength = Math.max(k.toString().length() + ("(h:" + k.hashCode() + ")").length(), maxLength);
}
for (K k: keySet()) {
String line = k.toString();
while (line.length() < maxLength) {
line += " ";
}
line += "(h:" + k.hashCode() + ")";
line += ":";
if (getList(k).size() == 0) {
result += line + " empty\n";
} else {
for (V v: getList(k)) {
while (line.length() < maxLength + 2) {
line += " ";
}
line += v.toString() + "(hash: " + v.hashCode() + ")\n";
result += line;
line = "";
}
}
}
return result;
}
}