public 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.FMap<K> |
private class |
Storage.Iter |
static class |
Storage.PrimitiveIdHash |
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 double |
loadFactor |
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) |
Storage(Hash<? super T,? super T> ha) |
Storage(Hash<? super T,? super T> ha,
boolean safeIterator) |
Storage(Hash<? super T,? super T> ha,
int capacity) |
Storage(Hash<? super T,? super T> ha,
int capacity,
boolean safeIterator)
constructor
|
Storage(int capacity)
Constructs a new
Storage with given capacity. |
Storage(int capacity,
boolean safeIterator) |
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() |
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 double loadFactor
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.public Storage(boolean safeIterator)
public Storage(int capacity, boolean safeIterator)
public Storage(Hash<? super T,? super T> ha, int capacity, boolean safeIterator)
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 T removeElem(T t)
public <K> java.util.Map<K,T> foreignKey(Hash<K,? super T> h)
private static int rehash(int h)
private <K> int getBucket(Hash<K,? super T> ha, K key)
key
- The key to compareprivate void fillTheHole(int hole)
private void ensureSpace()
public static <O> Hash<O,O> defaultHash()