org.testng.collections.ListMultiMap Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of testng Show documentation
Show all versions of testng Show documentation
Testing framework for Java
package org.testng.collections;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
/**
* A container to hold lists indexed by a key.
*/
public class ListMultiMap {
private Map> m_objects = Maps.newHashMap();
public void put(K key, V method) {
List l = m_objects.get(key);
if (l == null) {
l = Lists.newArrayList();
m_objects.put(key, l);
}
l.add(method);
}
public List get(K key) {
return m_objects.get(key);
}
public List getKeys() {
return new ArrayList(m_objects.keySet());
// List result = new ArrayList();
// for (K k : m_objects.keySet()) {
// result.add(k);
// }
// Collections.sort(result);
// return result;
}
public boolean containsKey(K k) {
return m_objects.containsKey(k);
}
@Override
public String toString() {
StringBuilder result = new StringBuilder();
List indices = getKeys();
// Collections.sort(indices);
for (K i : indices) {
result.append("\n ").append(i).append(" <-- ");
for (Object o : m_objects.get(i)) {
result.append(o).append(" ");
}
}
return result.toString();
}
public boolean isEmpty() {
return m_objects.size() == 0;
}
public int getSize() {
return m_objects.size();
}
public List remove(K key) {
return m_objects.remove(key);
}
public Set>> getEntrySet() {
return m_objects.entrySet();
}
public Collection> getValues() {
return m_objects.values();
}
public void putAll(K k, Collection values) {
for (V v : values) {
put(k, v);
}
}
public static ListMultiMap create() {
return new ListMultiMap();
}
}