org.opentripplanner.netex.index.hierarchy.HierarchicalMultimap Maven / Gradle / Ivy
package org.opentripplanner.netex.index.hierarchy;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import org.opentripplanner.netex.index.api.ReadOnlyHierarchicalMap;
import java.util.Collection;
import java.util.Map;
/**
* A concrete multimap implementation of {@link AbstractHierarchicalMap}.
*
* Note that the collection retuned by the {@link ReadOnlyHierarchicalMap#lookup(Object)} is
* not ReadOnly, but should be treated as such. It is just to painful to achieve this with the
* current verion of the Java Collection API - without any side-effects.
*
* @param the key type
* @param the base value type
*/
public class HierarchicalMultimap extends AbstractHierarchicalMap> {
private final Multimap map = ArrayListMultimap.create();
/** Create a root for the hierarchy */
public HierarchicalMultimap() {
super(null);
}
/** Create a child of the given {@code parent}. */
public HierarchicalMultimap(HierarchicalMultimap parent) {
super(parent);
}
/** Return a reference to the parent. */
@Override
public HierarchicalMultimap parent() {
return (HierarchicalMultimap) super.parent();
}
/** Add a new pair of {@code key & value} to the local map value collection. */
public void add(K key, V value) {
map.put(key, value);
}
/** Add a multimap to the local map */
public void addAll(Multimap other) {
for (Map.Entry it : other.entries()) {
add(it.getKey(), it.getValue());
}
}
@Override
public Collection localKeys() {
return map.keySet();
}
@Override
public Collection> localValues() {
return map.asMap().values();
}
@Override
Collection localGet(K key) {
return map.get(key);
}
@Override
boolean localContainsKey(K key) {
return map.containsKey(key);
}
@Override
protected int localSize() {
return map.size();
}
@Override
void localRemove(K key) {
map.removeAll(key);
}
}