Advertisement
sci4me

Pool

Oct 2nd, 2015
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.80 KB | None | 0 0
  1. public final class Pool<E>
  2. {
  3.     private Map<Integer, E> indexToData;
  4.     private Map<E, Integer> dataToIndex;
  5.     private int index;
  6.  
  7.     public Pool()
  8.     {
  9.         this.indexToData = new HashMap<>();
  10.         this.dataToIndex = new HashMap<>();
  11.     }
  12.  
  13.     public int get(final E e)
  14.     {
  15.         if (this.dataToIndex.containsKey(e))
  16.         {
  17.             return this.dataToIndex.get(e);
  18.         }
  19.         else
  20.         {
  21.             final int index = this.index;
  22.             this.index++;
  23.             this.dataToIndex.put(e, index);
  24.             this.indexToData.put(index, e);
  25.             return index;
  26.         }
  27.     }
  28.  
  29.     public E getByIndex(final int index)
  30.     {
  31.         return this.indexToData.get(index);
  32.     }
  33.  
  34.     public int size()
  35.     {
  36.         return this.index;
  37.     }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement