T
- type of stored objectspublic class Storage<T> extends java.util.AbstractSet<T>
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.
Storage<String> cache = new Storage(); // use default Hash for (String input : data) { String onlyOne = cache.putIfUnique(input); .... }
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; } });
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);
Modifier and Type | Class and Description |
---|---|
private class |
Storage.AbstractIter |
private class |
Storage.FMap<K> |
private class |
Storage.Iter |
static class |
Storage.PrimitiveIdHash
Hash for
PrimitiveId . |
private class |
Storage.SafeReadonlyIter |
Modifier and Type | Field and Description |
---|---|
private boolean |
arrayCopyNecessary |
private T[] |
data |
private static int |
DEFAULT_CAPACITY |
private Hash<? super T,? super T> |
hash |
private static double |
LOAD_FACTOR |
private int |
mask |
private int |
modCount |
private boolean |
safeIterator |
private int |
size |
Constructor and Description |
---|
Storage()
Constructs a new
Storage with default capacity (16). |
Storage(boolean safeIterator)
Constructs a new
Storage . |
Storage(Hash<? super T,? super T> ha)
Constructs a new
Storage with given hash. |
Storage(Hash<? super T,? super T> ha,
boolean safeIterator)
Constructs a new
Storage with given hash. |
Storage(Hash<? super T,? super T> ha,
int capacity)
Constructs a new
Storage with given hash and capacity. |
Storage(Hash<? super T,? super T> ha,
int capacity,
boolean safeIterator)
Constructs a new
Storage with given hash and capacity. |
Storage(int capacity)
Constructs a new
Storage with given capacity. |
Storage(int capacity,
boolean safeIterator)
Constructs a new
Storage with given capacity. |
Modifier and Type | Method and Description |
---|---|
boolean |
add(T t) |
void |
clear() |
boolean |
contains(java.lang.Object o) |
private void |
copyArray() |
static <O> Hash<O,O> |
defaultHash()
A factory for default hash implementation.
|
private T |
doRemove(int slot) |
private void |
ensureSpace() |
boolean |
equals(java.lang.Object obj) |
private void |
fillTheHole(int hole) |
<K> java.util.Map<K,T> |
foreignKey(Hash<K,? super T> h) |
T |
get(T t) |
private <K> int |
getBucket(Hash<K,? super T> ha,
K key)
Finds a bucket for given key.
|
int |
hashCode() |
java.util.Iterator<T> |
iterator() |
T |
put(T t) |
T |
putUnique(T t) |
private static int |
rehash(int h)
Additional mixing of hash
|
boolean |
remove(java.lang.Object o) |
T |
removeElem(T t) |
int |
size() |
addAll, containsAll, isEmpty, retainAll, toArray, toArray, toString
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
private int mask
private int size
private volatile int modCount
private static final double LOAD_FACTOR
private static final int DEFAULT_CAPACITY
private final boolean safeIterator
private boolean arrayCopyNecessary
public Storage()
Storage
with default capacity (16).public Storage(int capacity)
Storage
with given capacity.capacity
- capacitypublic Storage(Hash<? super T,? super T> ha)
Storage
with given hash.ha
- hashpublic Storage(boolean safeIterator)
Storage
.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.public Storage(int capacity, boolean safeIterator)
Storage
with given capacity.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.public Storage(Hash<? super T,? super T> ha, boolean safeIterator)
Storage
with given hash.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.public Storage(Hash<? super T,? super T> ha, int capacity)
Storage
with given hash and capacity.ha
- hashcapacity
- capacitypublic Storage(Hash<? super T,? super T> ha, int capacity, boolean safeIterator)
Storage
with given hash and capacity.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.private void copyArray()
public int size()
public boolean contains(java.lang.Object o)
public boolean remove(java.lang.Object o)
public void clear()
public int hashCode()
public boolean equals(java.lang.Object obj)
public T removeElem(T t)
public <K> java.util.Map<K,T> foreignKey(Hash<K,? super T> h)
private static int rehash(int h)
h
- hashprivate <K> int getBucket(Hash<K,? super T> ha, K key)
K
- type for hashCode and first equals parameterha
- hash functionkey
- The key to compareprivate void fillTheHole(int hole)
private void ensureSpace()
public static <O> Hash<O,O> defaultHash()
O
- type for hash