Package org.openstreetmap.josm.data.osm
Class Storage<T>
java.lang.Object
java.util.AbstractCollection<E>
java.util.AbstractSet<T>
org.openstreetmap.josm.data.osm.Storage<T>
- Type Parameters:
T- type of stored objects
- All Implemented Interfaces:
Iterable<T>,Collection<T>,Set<T>
A Set-like class that allows looking up equivalent preexisting instance.
It is useful wherever one would use self-mapping construct like
Map<T,T>.put(t,t), that is, for caches, uniqueness filters or similar.
The semantics of equivalency can be external to the object, using the
Hash interface. The set also supports querying for entries using
different key type, in case you can provide a Hash implementation
that can resolve the equality.
Examples
- A String cache:
Storage<String> cache = new Storage(); // use default Hash for (String input : data) { String onlyOne = cache.putIfUnique(input); .... } - Identity-based set:
Storage<Object> identity = new Storage(new Hash<Object,Object> { public int getHashCode(Object o) { return System.identityHashCode(o); } public boolean equals(Object o1, Object o2) { return o1 == o2; } }); - An object with int ID and id-based lookup:
class Thing { int id; } Storage<Thing> things = new Storage(new Hash<Thing,Thing>() { public int getHashCode(Thing t) { return t.id; } public boolean equals(Thing t1, Thing t2) { return t1 == t2; } }); Map<Integer,Thing> fk = things.foreignKey(new Hash<Integer,Thing>() { public int getHashCode(Integer i) { return i.getIntValue(); } public boolean equals(Integer k, Thing t) { return t.id == k.getIntvalue(); } } things.put(new Thing(3)); assert things.get(new Thing(3)) == fk.get(3);
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate classprivate final classprivate final classstatic classHash forPrimitiveId.private final class -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate booleanprivate T[]private static final intprivate static final doubleprivate intprivate intprivate final booleanprivate int -
Constructor Summary
ConstructorsConstructorDescriptionStorage()Constructs a newStoragewith default capacity (16).Storage(boolean safeIterator) Constructs a newStorage.Storage(int capacity) Constructs a newStoragewith given capacity.Storage(int capacity, boolean safeIterator) Constructs a newStoragewith given capacity.Constructs a newStoragewith given hash.Constructs a newStoragewith given hash.Constructs a newStoragewith given hash and capacity.Constructs a newStoragewith given hash and capacity. -
Method Summary
Modifier and TypeMethodDescriptionbooleanvoidclear()booleanprivate voidstatic <O> Hash<O,O> A factory for default hash implementation.private TdoRemove(int slot) private voidbooleanprivate voidfillTheHole(int hole) foreignKey(Hash<K, ? super T> h) private <K> intFinds a bucket for given key.inthashCode()iterator()private static intrehash(int h) Additional mixing of hashbooleanremoveElem(T t) intsize()Methods inherited from class java.util.AbstractSet
removeAllMethods inherited from class java.util.AbstractCollection
addAll, containsAll, isEmpty, retainAll, toArray, toArray, toStringMethods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArrayMethods inherited from interface java.util.Set
addAll, containsAll, isEmpty, retainAll, spliterator, toArray, toArray
-
Field Details
-
hash
-
data
-
mask
-
size
-
modCount
-
LOAD_FACTOR
- See Also:
-
DEFAULT_CAPACITY
- See Also:
-
safeIterator
-
arrayCopyNecessary
-
-
Constructor Details
-
Storage
public Storage()Constructs a newStoragewith default capacity (16). -
Storage
Constructs a newStoragewith given capacity.- Parameters:
capacity- capacity
-
Storage
Constructs a newStoragewith given hash.- Parameters:
ha- hash
-
Storage
Constructs a newStorage.- Parameters:
safeIterator- If set to false, you must not modify the Storage while iterating over it. If set to true, you can safely modify, but the read-only iteration will happen on a copy of the unmodified Storage. This is similar to CopyOnWriteArrayList.
-
Storage
Constructs a newStoragewith given capacity.- Parameters:
capacity- capacitysafeIterator- If set to false, you must not modify the Storage while iterating over it. If set to true, you can safely modify, but the read-only iteration will happen on a copy of the unmodified Storage. This is similar to CopyOnWriteArrayList.
-
Storage
Constructs a newStoragewith given hash.- Parameters:
ha- hashsafeIterator- If set to false, you must not modify the Storage while iterating over it. If set to true, you can safely modify, but the read-only iteration will happen on a copy of the unmodified Storage. This is similar to CopyOnWriteArrayList.
-
Storage
Constructs a newStoragewith given hash and capacity.- Parameters:
ha- hashcapacity- capacity
-
Storage
Constructs a newStoragewith given hash and capacity.- Parameters:
ha- hashcapacity- capacitysafeIterator- If set to false, you must not modify the Storage while iterating over it. If set to true, you can safely modify, but the read-only iteration will happen on a copy of the unmodified Storage. This is similar to CopyOnWriteArrayList.
-
-
Method Details
-
copyArray
-
size
- Specified by:
sizein interfaceCollection<T>- Specified by:
sizein interfaceSet<T>- Specified by:
sizein classAbstractCollection<T>
-
iterator
-
contains
- Specified by:
containsin interfaceCollection<T>- Specified by:
containsin interfaceSet<T>- Overrides:
containsin classAbstractCollection<T>
-
add
- Specified by:
addin interfaceCollection<T>- Specified by:
addin interfaceSet<T>- Overrides:
addin classAbstractCollection<T>
-
remove
- Specified by:
removein interfaceCollection<T>- Specified by:
removein interfaceSet<T>- Overrides:
removein classAbstractCollection<T>
-
clear
- Specified by:
clearin interfaceCollection<T>- Specified by:
clearin interfaceSet<T>- Overrides:
clearin classAbstractCollection<T>
-
hashCode
- Specified by:
hashCodein interfaceCollection<T>- Specified by:
hashCodein interfaceSet<T>- Overrides:
hashCodein classAbstractSet<T>
-
equals
- Specified by:
equalsin interfaceCollection<T>- Specified by:
equalsin interfaceSet<T>- Overrides:
equalsin classAbstractSet<T>
-
put
-
get
-
putUnique
-
removeElem
-
foreignKey
-
rehash
Additional mixing of hash- Parameters:
h- hash- Returns:
- new hash
-
getBucket
Finds a bucket for given key.- Type Parameters:
K- type for hashCode and first equals parameter- Parameters:
ha- hash functionkey- The key to compare- Returns:
- the bucket equivalent to the key or -(bucket) as an empty slot where such an entry can be stored.
-
doRemove
-
fillTheHole
-
ensureSpace
-
defaultHash
A factory for default hash implementation.- Type Parameters:
O- type for hash- Returns:
- a hash implementation that just delegates to object's own hashCode and equals.
-