Advertisement
Guest User

Untitled

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