Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.85 KB | None | 0 0
  1. package org.oreme.util;
  2.  
  3. import java.util.Collection;
  4. import java.util.HashMap;
  5. import java.util.HashSet;
  6. import java.util.Map;
  7. import java.util.Set;
  8.  
  9. /**
  10.  * Allows to query a hashmap from both directions: by key and by value (which
  11.  * returns the key).
  12.  *
  13.  * By value queries return their key. Underlying implementation consumes 2x
  14.  * memory compared to a {@link HashMap}, however provides O(1) access time (O(N)
  15.  * in {@link HashMap} case.)
  16.  *
  17.  * NOTE: Do not use Map as its data type if you need to use getByVal() method.
  18.  *
  19.  * <b>CAUTION:</b> If there are more than one entries with same value (<code>V</code>),
  20.  * getByVal() method will return most recently key-value tuple inserted.
  21.  *
  22.  * Example usage:
  23.  * <pre>
  24.  * DoubleAssociativeMap<String, Integer> map = new DoubleAssociativeMap<String, Integer>();
  25.  * map.put("ahmet", 1989);
  26.  * map.getByKey("ahmet"); // 1989
  27.  * map.getByVal(1989); // "ahmet"
  28.  * </pre>
  29.  *
  30.  * @author Ahmet Alp Balkan
  31.  *
  32.  * @param <K>
  33.  * @param <V>
  34.  *            hashCode() implementation is important for this class!
  35.  */
  36. @SuppressWarnings("unchecked")
  37. public class DoubleAssociativeMap<K,V> implements Map<K, V> {
  38.     protected static final String VALUE_PREFIX = "__val_";
  39.    
  40.     private HashMap<Object, Object> map;
  41.    
  42.     public DoubleAssociativeMap(){
  43.         map = new HashMap<Object, Object>();
  44.     }
  45.    
  46.     /**
  47.      * Prepares a private key for the entry
  48.      * that is actual value is its private key
  49.      * prepared by this method and its value is
  50.      * actual key.
  51.      *
  52.      * @param arg0
  53.      * @return
  54.      */
  55.     private Object getValueKey(Object arg0) {
  56.         return VALUE_PREFIX+arg0.hashCode();
  57.     }
  58.    
  59.     /**
  60.      * Identical to get(Object o)
  61.      * @param value
  62.      * @return
  63.      */
  64.     public V getByKey(K key) {
  65.         return this.get(key);
  66.     }
  67.    
  68.     /**
  69.      * Request key by its associated value.
  70.      *
  71.      * CAUTION: If there are more than one entries with this
  72.      * value, key of the most recently inserted entry will
  73.      * be returned.
  74.      *
  75.      * @param value
  76.      * @return
  77.      */
  78.     public K getByVal(V value) {
  79.         return (K) map.get(getValueKey(value));
  80.     }
  81.  
  82.     @Override
  83.     public void clear() {
  84.         map.clear();
  85.     }
  86.  
  87.     @Override
  88.     public boolean containsKey(Object arg0) {
  89.         return map.containsKey(arg0);
  90.     }
  91.  
  92.     @Override
  93.     public boolean containsValue(Object arg0) {
  94.         return map.containsKey(getValueKey(arg0));
  95.     }
  96.  
  97.     /**
  98.      * Does not return hidden entries.
  99.      */
  100.     @Override
  101.     public Set<Entry<K, V>> entrySet() {
  102.         Set<Entry<Object, Object>> entrySet = map.entrySet();
  103.         Set<Entry<K, V>> ret = new HashSet<Entry<K,V>>();
  104.        
  105.         for(Entry<Object, Object> entry : entrySet){
  106.             if(!entry.getKey().toString().startsWith(VALUE_PREFIX))
  107.                 ret.add((Entry<K, V>) entry);
  108.         }
  109.    
  110.         return ret;
  111.     }
  112.  
  113.     @Override
  114.     public V get(Object o) {
  115.         return (V) map.get(o);
  116.     }
  117.  
  118.    
  119.     @Override
  120.     public boolean isEmpty() {
  121.         return map.isEmpty();
  122.     }
  123.  
  124.     /**
  125.      * Does not return hidden entry keys.
  126.      */
  127.     @Override
  128.     public Set<K> keySet() {
  129.         Set<Object> keySet = map.keySet();
  130.         Set<K> ret = new HashSet<K>();
  131.        
  132.         for(Object key : keySet){
  133.             if(!key.toString().startsWith(VALUE_PREFIX))
  134.                 ret.add((K) key);
  135.         }
  136.         return ret;
  137.     }
  138.  
  139.     @Override
  140.     public V put(K key, V value) {
  141.         putValueKey(key, value);
  142.         return (V) map.put(key, value);
  143.     }
  144.    
  145.     private void putValueKey(K key, V value){
  146.         map.put(getValueKey(value), key);
  147.     }
  148.  
  149.     @Override
  150.     public void putAll(Map<? extends K, ? extends V> m) {
  151.         map.putAll(m);
  152.     }
  153.  
  154.     @Override
  155.     public V remove(Object o) {
  156.         return (V) map.remove(0);
  157.     }
  158.  
  159.     /**
  160.      * Caution: Time complexity is O(N).
  161.      */
  162.     @Override
  163.     public int size() {
  164.         return this.keySet().size();
  165.     }
  166.  
  167.     @Override
  168.     public Collection<V> values() {
  169.         Collection<V> values = new HashSet<V>();
  170.    
  171.         for(Entry<K, V> entry : this.entrySet()){
  172.             values.add((V) entry.getValue());
  173.         }
  174.        
  175.         return values;
  176.     }
  177.    
  178.     /**
  179.      * Caution: Set representation surrounded with [], instead
  180.      * of {} in Map representation. Does not return hidden
  181.      * entries.
  182.      */
  183.     @Override
  184.     public String toString() {
  185.         return this.entrySet().toString();
  186.     }
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement