com.ajjpj.abase.collection.immutable.JavaUtilMapWrapper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of a-base Show documentation
Show all versions of a-base Show documentation
a-base is a library of basic (hence the name) classes, most notably immutable collection classes with copy-on-write operations
package com.ajjpj.abase.collection.immutable;
import com.ajjpj.abase.collection.APair;
import java.util.*;
/**
* @author arno
*/
class JavaUtilMapWrapper implements java.util.Map {
private final AMap inner;
JavaUtilMapWrapper(AMap inner) {
this.inner = inner;
}
@Override
public int size() {
return inner.size();
}
@Override
public boolean isEmpty() {
return inner.isEmpty();
}
@SuppressWarnings("unchecked")
@Override
public boolean containsKey(Object key) {
return inner.containsKey((K) key);
}
@SuppressWarnings("unchecked")
@Override
public boolean containsValue(Object value) {
return inner.containsValue((V) value);
}
@SuppressWarnings("unchecked")
@Override
public V get(Object key) {
return inner.get((K) key).getOrElse(null);
}
/**
* There is usually a performance gain to be had by overriding this default implementation
*/
@Override
public Set keySet() {
return new AbstractSet() {
@Override
public Iterator iterator() {
return new Iterator() {
final Iterator> it = inner.iterator();
@Override
public boolean hasNext() {
return it.hasNext();
}
@Override
public K next() {
return it.next()._1;
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
};
}
@Override
public int size() {
return inner.size();
}
};
}
@Override
public Collection values() {
return new AbstractCollection() {
@Override
public Iterator iterator() {
return new Iterator() {
final Iterator> it = inner.iterator();
@Override
public boolean hasNext() {
return it.hasNext();
}
@Override
public V next() {
return it.next()._2;
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
};
}
@Override
public int size() {
return inner.size();
}
};
}
@Override
public Set> entrySet() {
return new AbstractSet>() {
@Override
public Iterator> iterator() {
return new Iterator>() {
final Iterator> it = inner.iterator();
@Override
public boolean hasNext() {
return it.hasNext();
}
@Override
public Entry next() {
final APair kv = it.next();
return new Entry() {
@Override
public K getKey() {
return kv._1;
}
@Override
public V getValue() {
return kv._2;
}
@Override
public V setValue(V value) {
throw new UnsupportedOperationException();
}
};
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
};
}
@Override
public int size() {
return inner.size();
}
};
}
//------------- mutators
@Override
public V put(K key, V value) {
throw new UnsupportedOperationException();
}
@Override
public V remove(Object key) {
throw new UnsupportedOperationException();
}
@Override
public void putAll(Map extends K, ? extends V> m) {
throw new UnsupportedOperationException();
}
@Override
public void clear() {
throw new UnsupportedOperationException();
}
}