
com.github.becausetesting.collections.MultiValueMapArrayList Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of commons Show documentation
Show all versions of commons Show documentation
A common libraries used for testing framework.
package com.github.becausetesting.collections;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* This class does not implement java.util.Map<K, V> interface.
* This class encapsulates a HashMap holding a List of values associated with a
* particular key.
* @author subwiz
*/
public class MultiValueMapArrayList implements MultiValueMap{
private final Map> map = new LinkedHashMap<>();
@Override
public Collection put(K key, V value){
List l = map.get(key);
if(l == null){
l = new ArrayList<>();
}
l.add(value);
return map.put(key, l);
}
@Override
public Collection get(K key){
return map.get(key);
}
@Override
public Set keySet(){
return map.keySet();
}
@Override
public int size() {
return map.size();
}
@Override
public boolean isEmpty() {
return size() == 0;
}
@Override
public boolean containsKey(K key) {
return map.containsKey(key);
}
@Override
public boolean containsValue(V value) {
return map.keySet().stream().map((key) -> map.get(key)).anyMatch((values) -> (values.contains(value)));
}
@Override
public Collection remove(K key) {
return map.remove(key);
}
@Override
public void clear() {
map.clear();
}
@Override
public Collection values() {
List values = new ArrayList<>();
map.keySet().stream().map((key) -> map.get(key)).forEach((v) -> {
values.addAll(v);
});
return values;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (!(obj instanceof MultiValueMap)) {
return false;
}
final MultiValueMap other = (MultiValueMap) obj;
if (this.keySet() != other.keySet() && (this.keySet() == null || !this.keySet().equals(other.keySet()))) {
return false;
}
final Collection thisValues = this.values();
final Collection otherValues = other.values();
if (thisValues != otherValues && (thisValues == null || !thisValues.equals(otherValues))) {
return false;
}
return true;
}
@Override
public int hashCode() {
int hash = 7;
hash = 47 * hash + (this.map != null ? this.map.hashCode() : 0);
return hash;
}
@Override
public String toString() {
return map.toString();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy