Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.infinispan.commons.equivalence;
  
  import java.util.Iterator;
  import java.util.Map;
Custom hash-based linked list map which accepts no null keys nor null values, where equality and hash code calculations are done based on passed Equivalence function implementations for keys and values, as opposed to relying on their own equals/hashCode/toString implementations. This is handy when using key/values whose mentioned methods cannot be overriden, i.e. arrays, and in situations where users want to avoid using wrapper objects. In order to provide linked list behaviour, entries are linked with each other in a predictable order.

Author(s):
Galder ZamarreƱo
Since:
6.0
 
 public class EquivalentLinkedHashMap<K, V> extends EquivalentHashMap<K, V> {
 
    private transient LinkedNode<K, V> header;
 
    private final IterationOrder iterationOrder;
 
    public EquivalentLinkedHashMap(int initialCapacityfloat loadFactor,
          IterationOrder iterationOrder,
          Equivalence<? super K> keyEqEquivalence<? super V> valueEq) {
       super(initialCapacityloadFactorkeyEqvalueEq);
       this. = iterationOrder;
       addFirstEntry();
    }
 
    private void addFirstEntry() {
        = createLinkedNode();
       . = ;
       . = ;
    }
 
    @Override
    void addEntry(int index, K key, V valueint hash) {
       super.addEntry(indexkeyvaluehash);
       LinkedNode<K,V> eldest = .;
       if (removeEldestEntry(eldest)) {
          remove(eldest.getKey());
       }
    }
 
    protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
       return false;
    }
 
    @SuppressWarnings("unchecked")
    private <K, V> LinkedNode<K, V> createLinkedNode() {
       return new LinkedNode<K, V>(null, -1, nullnull);
    }
 
    @Override
    Node<K, V> createNode(K key, V valueint hashNode<K, V> node) {
       LinkedNode<K, V> linkedNode = new LinkedNode<K, V>(keyhashvaluenode);
       linkedNode.addBefore();
       return linkedNode;
    }
 
    @Override
    public V get(Object key) {
       LinkedNode<K, V> n = getNode(key);
       return n == null ? null : n.recordAccess(this);
    }
 
    @Override
    public V remove(Object key) {
       LinkedNode<K, V> prevNode = removeNode(key);
       return prevNode == null ? null : prevNode.remove();
    }
 
    @Override
    public void clear() {
       super.clear();
       . = ;
       . = ;
    }
 
    private static final class LinkedNode<K, V> extends Node<K,V> {
 
       LinkedNode<K, V> before;
       LinkedNode<K, V> after;
 
       private LinkedNode(K keyint hash, V valueNode<K, V> next) {
          super(keyhashvaluenext);
       }
 
       private V remove() {
          . = ;
          . = ;
          return ;
      }
      private void addBefore(LinkedNode<K, V> entry) {
           = entry;
          = entry.before;
         . = this;
         . = this;
      }
      V recordAccess(EquivalentHashMap<K, V> m) {
         EquivalentLinkedHashMap<K, V> linkedMap = (EquivalentLinkedHashMap<K, V>)m;
         if (linkedMap.iterationOrder == .) {
            linkedMap.modCount++;
            remove();
            addBefore(linkedMap.header);
         }
         return ;
      }
      @Override
      protected V setValue(V valueEquivalentHashMap<K, V> map) {
         V retValue = super.setValue(valuemap);
         recordAccess(map);
         return retValue;
      }
   }
   public enum IterationOrder {
      ACCESS_ORDER, INSERT_ORDER;
      public boolean toJdkAccessOrder() {
         return this == ;
      }
   }

   
Exported Entry for iterators
   private static class LinkedEntry<K,V> extends MapEntry<K,V> {
      LinkedNode<K, V> before;
      LinkedNode<K, V> after;
      LinkedEntry(K key, V valLinkedNode<K, V> beforeLinkedNode<K, V> afterEquivalentHashMap<K, V> map) {
         super(keyvalmap);
         this. = before;
         this. = after;
      }
   }
   private abstract class EquivalentLinkedHashIterator<T> implements Iterator<T> {
      final EquivalentHashMap<K, V> map;
      LinkedEntry<K, V> nextEntry;
      LinkedEntry<K, V> lastReturned = null;
      protected EquivalentLinkedHashIterator(EquivalentHashMap<K, V> map) {
         this. = map;
          = new LinkedEntry<K, V>(
               ....,
               ....map);
      }

      
The modCount value that the iterator believes that the backing List should have. If this expectation is violated, the iterator has detected concurrent modification.
      int expectedModCount = ;
      public boolean hasNext() {
         return !equals();
      }
      public void remove() {
         if ( == null)
            throw new IllegalStateException();
         if ( != )
            throw new ConcurrentModificationException();
          = null;
          = ;
      }
      LinkedEntry<K,V> nextEntry() {
         if ( != )
            throw new ConcurrentModificationException();
         if (equals())
            throw new NoSuchElementException();
         LinkedEntry<K, V> e = ;
          = ;
          = new LinkedEntry<K, V>(
               e.after.keye.after.value,
               e.after.beforee.after.after);
         return e;
      }
      boolean equals(LinkedEntry<K, V> entryLinkedNode<K, V> node) {
         return entry.key == node.key
               && entry.val == node.value
               && entry.before == node.before
               && entry.after == node.after;
      }
   }
   private class KeyIterator extends EquivalentLinkedHashIterator<K> {
      protected KeyIterator(EquivalentHashMap<K, V> map) {
         super(map);
      }
      public K next() {
         return nextEntry().getKey();
      }
   }
   private class ValueIterator extends EquivalentLinkedHashIterator<V> {
      protected ValueIterator(EquivalentHashMap<K, V> map) {
         super(map);
      }
      public V next() {
         return nextEntry().;
      }
   }
   private class EntryIterator extends EquivalentLinkedHashIterator<Map.Entry<K, V>> {
      protected EntryIterator(EquivalentHashMap<K, V> map) {
         super(map);
      }
      public Map.Entry<K, V> next() {
         return nextEntry();
      }
   }
      return new KeyIterator(this);
   }
      return new ValueIterator(this);
   }
      return new EntryIterator(this);
   }
New to GrepCode? Check out our FAQ X