xapi.collect.proxy.MapOf Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of xapi-core-collect Show documentation
Show all versions of xapi-core-collect Show documentation
Core interfaces for our collections api.
package xapi.collect.proxy;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import xapi.collect.X_Collect;
import xapi.collect.api.CollectionOptions;
import xapi.collect.api.HasValues;
import xapi.collect.api.ObjectTo;
import xapi.reflect.X_Reflect;
public class MapOf
implements CollectionProxy, Map, HasValues, ObjectTo
{
private final Class keyClass;
private final Map map;
private final Class valueClass;
public MapOf(Map map, Class keyClass, Class valueClass) {
this.map = map;
this.keyClass = keyClass;
this.valueClass = valueClass;
}
@Override
public ObjectTo clone(CollectionOptions options) {
ObjectTo into = X_Collect.newMap(keyClass, valueClass, options);
for (Entry entry : map.entrySet()) {
// do not give access to our map's entry objects;
// this method is clone(), which implies copy and not reference sharing
into.put(entry.getKey(), entry.getValue());
}
return into;
}
@Override
public V get(Object key) {
return map.get(key);
}
@Override
public V remove(Object key) {
return map.remove(key);
}
@Override
public V[] toArray() {
V[] values = X_Reflect.newArray(valueClass, map.size());
map.values().toArray(values);
return values;
}
@Override
public Collection toCollection(Collection into) {
if (into == null)
into = new ArrayList();
into.addAll(map.values());
return into;
}
@Override
public Map toMap(Map into) {
if (into == null)
into = new LinkedHashMap();
into.putAll(map);
return into;
}
@Override
public V put(Entry item) {
return map.put(item.getKey(), item.getValue());
}
@Override
public int size() {
return map.size();
}
@Override
public boolean isEmpty() {
return map.isEmpty();
}
@Override
public void clear() {
map.clear();
}
@Override
@SuppressWarnings("unchecked")
public Entry entryFor(final Object key) {
// Encourage inlining; EntryProxy won't get loaded if we only
// use CollectionProxy based maps.
if (map instanceof CollectionProxy) {
return ((CollectionProxy)map).entryFor(key);
}
// Rather than iterate to get the map's actual Entry,
// We'll just return a proxy object that fulfills the same duty.
class EntryProxy implements Entry {
@Override
public K getKey() {
return (K)key;
}
@Override
public V getValue() {
return map.get(key);
}
@Override
public V setValue(V value) {
return map.put((K)key, value);
}
}
return new EntryProxy();
}
@Override
public void setValue(Object key, Object value) {
map.put((K)key, (V)value);
}
@Override
public boolean containsKey(Object key) {
return map.containsKey(key);
}
@Override
public boolean containsValue(Object value) {
return map.containsValue(value);
}
@Override
public V put(K key, V value) {
return map.put(key, value);
}
@Override
public void putAll(Map extends K,? extends V> m) {
map.putAll(m);
}
@Override
public Set keySet() {
return map.keySet();
}
@Override
public Collection values() {
return map.values();
}
@Override
public Set> entrySet() {
return map.entrySet();
}
@Override
public Iterable> entries() {
return entrySet();
}
@Override
public void putAll(Iterable> items) {
for (Entry entry : items) {
map.put(entry.getKey(), entry.getValue());
}
}
@Override
public void removeAll(Iterable keys) {
for (K key : keys) {
map.remove(key);
}
}
@Override
public Iterable keys() {
return keySet();
}
@Override
public Class keyType() {
return keyClass;
}
@Override
public Class valueType() {
return valueClass;
}
@Override
public Class> componentType() {
return valueType();
}
}