public class Storage<T> extends 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 float |
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(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> 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() |
Iterator<T> |
iterator() |
T |
put(T t) |
T |
putUnique(T t) |
private int |
rehash(int h)
Additional mixing of hash
|
boolean |
remove(Object o) |
T |
removeElem(T t) |
int |
size() |
equals, removeAll
addAll, containsAll, isEmpty, retainAll, toArray, toArray, toString
private int mask
private int size
private transient volatile int modCount
private float 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
- capacity
- 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.private void copyArray()
public int size()
size
in interface Collection<T>
size
in interface Set<T>
size
in class AbstractCollection<T>
public boolean contains(Object o)
contains
in interface Collection<T>
contains
in interface Set<T>
contains
in class AbstractCollection<T>
public boolean add(T t)
add
in interface Collection<T>
add
in interface Set<T>
add
in class AbstractCollection<T>
public boolean remove(Object o)
remove
in interface Collection<T>
remove
in interface Set<T>
remove
in class AbstractCollection<T>
public void clear()
clear
in interface Collection<T>
clear
in interface Set<T>
clear
in class AbstractCollection<T>
public int hashCode()
hashCode
in interface Collection<T>
hashCode
in interface Set<T>
hashCode
in class AbstractSet<T>
public T removeElem(T t)
public <K> Map<K,T> foreignKey(Hash<K,? super T> h)
private 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()