xapi.collect.impl.ObjectToAbstract 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.impl;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import javax.inject.Provider;
import xapi.collect.api.CollectionOptions;
import xapi.collect.api.IntTo;
import xapi.collect.api.ObjectTo;
import xapi.collect.proxy.CollectionProxy;
public abstract class ObjectToAbstract implements ObjectTo {
public static abstract class ManyAbstract extends ObjectToAbstract> implements ObjectTo.Many {
private Class componentType;
@SuppressWarnings("unchecked")
public ManyAbstract(
Class keyType, Class valueType,
CollectionProxy> store,
Provider>>> iteratorProvider,
Comparator keyComparator, Comparator> valueComparator) {
super(keyType, Class.class.cast(IntTo.class),// ya, that's what java generics require...
store, iteratorProvider, keyComparator, valueComparator);
this.componentType = valueType;
}
@Override
public Class> componentType() {
return componentType;
}
}
private final CollectionProxy store;
private Provider>> iterator;
@SuppressWarnings("rawtypes")
private Comparator keyComparator;
@SuppressWarnings("rawtypes")
private Comparator valueComparator;
private Class keyType;
private Class valueType;
public ObjectToAbstract(
Class keyType, Class valueType,
CollectionProxy store,
Provider>> iteratorProvider,
Comparator keyComparator,
Comparator valueComparator
) {
this.keyType = keyType;
this.valueType = valueType;
this.store = store;
this.iterator = iteratorProvider;
this.keyComparator = keyComparator;
this.valueComparator = valueComparator;
}
protected Collection newCollection() {
return new ArrayList();
}
protected Map newMap() {
return new HashMap();
}
/**
* @return - The class of the type used for keys.
*/
@Override
public Class keyType() {
return keyType;
}
/**
* @return - The class of the type used for values.
* Note that this may not be the value type V; {@link ObjectTo.Many} uses IntTo
*/
@Override
public Class valueType() {
return valueType;
}
/**
* @return - The class of the root component type of values.
* Where {@link ObjectTo.Many#getValueType()} returns IntTo.class,
* getComponentType() will return V.class
*/
@Override
public Class> componentType() {
return valueType;
}
@Override
public Iterable> entries() {
return iterator.get();
}
@Override
public V[] toArray() {
return store.toArray();
}
@Override
public Collection toCollection(Collection into) {
if (into == null) {
into = newCollection();
}
fillCollection(into);
return into;
}
protected void fillCollection(Collection into) {
for (Entry key : entries()) {
into.add(key.getValue());
}
}
protected void fillMap(Map into) {
for (Entry key : entries()) {
into.put(key.getKey(), key.getValue());
}
}
@Override
public Map toMap(Map into) {
if (into == null) {
into = newMap();
}
fillMap(into);
return into;
}
@Override
public ObjectTo clone(CollectionOptions options) {
ObjectTo map = null;
return map;
}
@Override
@SuppressWarnings("unchecked")
public boolean containsKey(Object key) {
for (Entry entry : entries()) {
if (keyComparator.compare(entry.getKey(), key) == 0) return true;
}
return false;
}
@Override
@SuppressWarnings("unchecked")
public boolean containsValue(Object value) {
for (Entry entry : entries()) {
if (valueComparator.compare(entry.getValue(), value) == 0) return true;
}
return false;
}
@Override
public V get(Object key) {
return store.get(key);
}
@Override
public V put(Entry item) {
return store.put(item);
}
@Override
public V remove(Object key) {
return store.remove(key);
}
@Override
public void putAll(Iterable> items) {
for (Entry item : items) {
assert item != null;
put(item.getKey(), item.getValue());
}
}
@Override
public void removeAll(Iterable items) {
for (K key : items) {
remove(key);
}
}
@Override
public Iterable keys() {
return new EntryKeyAdapter(entries());
}
@Override
public Iterable values() {
return new EntryValueAdapter(entries());
}
@Override
public int size() {
return store.size();
}
@Override
public void clear() {
store.clear();
}
@Override
public boolean isEmpty() {
return store.isEmpty();
}
}