All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
xapi.collect.impl.CollectionServiceDefault Maven / Gradle / Ivy
package xapi.collect.impl;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentSkipListMap;
import xapi.annotation.inject.SingletonDefault;
import xapi.collect.api.ClassTo;
import xapi.collect.api.CollectionOptions;
import xapi.collect.api.Fifo;
import xapi.collect.api.IntTo;
import xapi.collect.api.ObjectTo;
import xapi.collect.api.StringDictionary;
import xapi.collect.api.ObjectTo.Many;
import xapi.collect.api.StringTo;
import xapi.collect.proxy.CollectionProxy;
import xapi.collect.proxy.MapOf;
import xapi.collect.service.CollectionService;
import xapi.except.NotYetImplemented;
import xapi.platform.AndroidPlatform;
import xapi.platform.GwtDevPlatform;
import xapi.platform.JrePlatform;
import xapi.util.X_Runtime;
@GwtDevPlatform
@JrePlatform
@AndroidPlatform
@SingletonDefault(implFor=CollectionService.class)
public class CollectionServiceDefault implements CollectionService{
static final Comparator STRING_CMP = new Comparator() {
@Override
public int compare(String o1, String o2) {
if (o1 == null)
return o2 == null ? 0 : "".compareTo(o2);
return o1.compareTo(o2 == null ? "" : o2);
}
};
static final Comparator> ENUM_CMP = new Comparator>() {
@Override
public int compare(Enum> o1, Enum> o2) {
if (o1 == null)
return o2 == null ? 0 : -o2.ordinal();
return o1.ordinal() - (o2 == null ? 0 : o2.ordinal());
}
};
public static final Comparator> CLASS_CMP = new Comparator>() {
@Override
public int compare(Class> o1, Class> o2) {
if (o1 == null)
return o2 == null ? 0 : -o2.hashCode();
return o1.hashCode() - (o2 == null ? 0 : o2.hashCode());
}
};
static final Comparator NUMBER_CMP = new Comparator() {
@Override
public int compare(Number o1, Number o2) {
if (o1==null)o1=0;
if (o2==null)o2=0;
double delta = o1.doubleValue() - o2.doubleValue();
if (Math.abs(delta)<0.0000000001)return 0;
return delta < 0 ? -1 : 1;
}
};
static final Comparator OBJECT_CMP = new Comparator() {
@Override
@SuppressWarnings({"unchecked","rawtypes"})
public int compare(Object o1, Object o2) {
if (o1 instanceof Comparable) {
return ((Comparable)o1).compareTo(o2);
}
return System.identityHashCode(o1) - System.identityHashCode(o2);
}
};
@SuppressWarnings("unchecked")
private final CollectionProxy,Comparator>> comparators =
newProxy(Class.class.cast(Class.class), Comparator.class, CollectionOptions.asConcurrent(true).build());
public CollectionServiceDefault() {
comparators.entryFor(Object.class).setValue(OBJECT_CMP);
comparators.entryFor(String.class).setValue(STRING_CMP);
comparators.entryFor(Enum.class).setValue(ENUM_CMP);
comparators.entryFor(Class.class).setValue(CLASS_CMP);
comparators.entryFor(Number.class).setValue(NUMBER_CMP);
comparators.entryFor(Byte.class).setValue(NUMBER_CMP);
comparators.entryFor(Short.class).setValue(NUMBER_CMP);
comparators.entryFor(Integer.class).setValue(NUMBER_CMP);
comparators.entryFor(Long.class).setValue(NUMBER_CMP);
comparators.entryFor(Float.class).setValue(NUMBER_CMP);
comparators.entryFor(Double.class).setValue(NUMBER_CMP);
}
protected Map newMap() {
if (X_Runtime.isMultithreaded()) {
return new ConcurrentHashMap();
} else {
return new HashMap();
}
}
@Override
public IntTo newList(Class cls, CollectionOptions opts) {
throw new NotYetImplemented("IntTo not yet implemented");
}
@Override
public IntTo newSet(Class cls, CollectionOptions opts) {
throw new NotYetImplemented("IntTo not yet implemented");
}
@Override
public ObjectTo newMap(Class key, Class cls, CollectionOptions opts) {
return new MapOf(this.newMap(), key, cls);
}
@Override
public ClassTo newClassMap(Class cls, CollectionOptions opts) {
return new ClassToDefault(this., V>newMap(), cls);
}
@Override
public StringTo newStringMap(Class cls, CollectionOptions opts) {
return new StringToAbstract();
}
protected CollectionProxy newProxy(Class keyType, Class valueType, CollectionOptions opts) {
if (opts.insertionOrdered()) {
if (opts.concurrent()) {
return new MapOf(new ConcurrentSkipListMap(), keyType, valueType);
} else {
return new MapOf(new LinkedHashMap(), keyType, valueType);
}
}
if (opts.concurrent()) {
return new MapOf(new ConcurrentHashMap(), keyType, valueType);
} else {
return new MapOf(new HashMap(), keyType, valueType);
}
}
@SuppressWarnings("unchecked")
private Comparator getComparator(Class> cls) {
Comparator> cmp = null;
while ((cmp = comparators.get(cls))==null)
cls = cls.getSuperclass();
return (Comparator)cmp;
}
@Override
public Many newMultiMap(Class key, Class cls, CollectionOptions opts) {
throw new NotYetImplemented("Multi-map not yet implemented");
}
@Override
public xapi.collect.api.ClassTo.Many newClassMultiMap(Class cls, CollectionOptions opts) {
throw new NotYetImplemented("Multi-map not yet implemented");
}
@Override
public xapi.collect.api.StringTo.Many newStringMultiMap(Class cls,
CollectionOptions opts) {
throw new NotYetImplemented("Multi-map not yet implemented");
}
@Override
public StringDictionary newDictionary() {
return new StringDictionaryDefault();
}
@Override
public Fifo newFifo() {
return new SimpleFifo();
}
}