Class Storage<T>

  • Type Parameters:
    T - type of stored objects
    All Implemented Interfaces:
    Iterable<T>, Collection<T>, Set<T>

    public class Storage<T>
    extends AbstractSet<T>
    A Set-like class that allows looking up equivalent preexising 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);
       
    • Constructor Detail

      • Storage

        public Storage()
        Constructs a new Storage with default capacity (16).
      • Storage

        public Storage​(int capacity)
        Constructs a new Storage with given capacity.
        Parameters:
        capacity - capacity
      • Storage

        public Storage​(Hash<? super T,​? super T> ha)
        Constructs a new Storage with given hash.
        Parameters:
        ha - hash
      • Storage

        public Storage​(boolean safeIterator)
        Constructs a new Storage.
        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

        public Storage​(int capacity,
                       boolean safeIterator)
        Constructs a new Storage with given capacity.
        Parameters:
        capacity - 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.
      • Storage

        public Storage​(Hash<? super T,​? super T> ha,
                       boolean safeIterator)
        Constructs a new Storage with given hash.
        Parameters:
        ha - hash
        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

        public Storage​(Hash<? super T,​? super T> ha,
                       int capacity)
        Constructs a new Storage with given hash and capacity.
        Parameters:
        ha - hash
        capacity - capacity
      • Storage

        public Storage​(Hash<? super T,​? super T> ha,
                       int capacity,
                       boolean safeIterator)
        Constructs a new Storage with given hash and capacity.
        Parameters:
        ha - hash
        capacity - 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.