Advertisement
magik6000

TriMap

Aug 10th, 2014
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.84 KB | None | 0 0
  1.  
  2.  
  3. package common.utils;
  4.  
  5. import java.util.Collection;
  6. import java.util.Set;
  7.  
  8. public interface TriMap<K, V1, V2> {
  9.    
  10.     public TriMap<V1,V2,K> rotateKey();
  11.     public TriMap<V2,K,V1> reverseRotateKey();
  12.    
  13.     public Set<TriMap.TriEntry<K, V1, V2>> entrySet();
  14.     public Collection<TriMap.TriValue<V1, V2>> values();
  15.     public Set<K> getKeySet();
  16.    
  17.    
  18.    
  19.     public boolean containsKey(K key);
  20.     public boolean containsValue(TriMap.TriValue<V1, V2> value);
  21.     public TriMap.TriValue<V1, V2> getValue(K key);
  22.    
  23.     public boolean isEmpty();
  24.    
  25.     public int size();
  26.    
  27.     public static abstract class TriEntry<K, V1, V2> {
  28.         abstract public K getKey();
  29.         abstract public TriValue<V1, V2> getValue();
  30.        
  31.         @Override
  32.         public boolean equals(Object obj) {
  33.             if (obj == null)
  34.                 return false;
  35.             if (this == obj)
  36.                 return true;
  37.             if (!(obj instanceof TriEntry<?, ?, ?>))
  38.                 return false;
  39.  
  40.             TriEntry<?, ?, ?> that = (TriEntry<?, ?, ?>) obj;
  41.  
  42.             if (this.getKey() == that.getKey() && this.getValue() == that.getValue())
  43.                 return true;
  44.  
  45.             if (this.getKey() != that.getKey())
  46.                 if (this.getKey() != null && !this.getKey().equals(that.getKey()))
  47.                     return false;
  48.                 else if (!this.getKey().equals(that.getKey()))
  49.                     return false;          
  50.            
  51.             if (this.getValue() != that.getValue())
  52.                 if (this.getValue() != null && !this.getValue().equals(that.getValue()))
  53.                     return false;
  54.                 else if (!this.getValue().equals(that.getValue()))
  55.                     return false;
  56.  
  57.             return true;
  58.         }
  59.        
  60.         @Override
  61.         public int hashCode() {
  62.             int result = 31;
  63.             result = 37 * result + (getValue()!=null?getValue().hashCode():0);
  64.             result = 37 * result + (getKey()!=null?getKey().hashCode():0);
  65.             return result;
  66.         }
  67.  
  68.     }
  69.  
  70.     public static class TriValue<V1, V2> {
  71.  
  72.         public TriValue(V1 value1, V2 value2) {
  73.             this.value1 = value1;
  74.             this.value2 = value2;
  75.         }
  76.        
  77.         final public V1 value1;
  78.         final public V2 value2;
  79.  
  80.         @Override
  81.         public boolean equals(Object obj){
  82.             if (obj == null)
  83.                 return false;
  84.             if (this == obj)
  85.                 return true;
  86.             if (!(obj instanceof TriValue<?, ?>))
  87.                 return false;
  88.  
  89.             TriValue<?, ?> that = (TriValue<?, ?>) obj;
  90.  
  91.             if (this.value1 == that.value1 && this.value2 == that.value2)
  92.                 return true;
  93.            
  94.             if (this.value1 != that.value1)
  95.                 if (this.value1 != null && !this.value1.equals(that.value1))
  96.                     return false;
  97.                 else if (!this.value1.equals(that.value1))
  98.                     return false;
  99.  
  100.             if (this.value2 != that.value2)
  101.                 if (this.value2 != null && !this.value2.equals(that.value2))
  102.                     return false;
  103.                 else if (!this.value2.equals(that.value2))
  104.                     return false;
  105.  
  106.             return true;
  107.         }
  108.  
  109.         @Override
  110.         public int hashCode() {
  111.             int result = 31;
  112.             result = 37 * result + (value1!=null?value1.hashCode():0);
  113.             result = 37 * result + (value2!=null?value2.hashCode():0);
  114.             return result;
  115.         }
  116.     }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement