Exemple #1
0
  @Override
  public V put(final K key, final V value) {
    final Entry<K, V>[] table = this.table;
    final int hash = key.hashCode();
    final int index = HashUtil.indexFor(hash, table.length, shift, mask);

    for (Entry<K, V> e = table[index]; e != null; e = e.hashNext) {
      final K entryKey;
      if (e.keyHash == hash && ((entryKey = e.key) == key || entryKey.equals(key))) {
        moveToTop(e);
        return e.setValue(value);
      }
    }

    final Entry<K, V> e = new Entry<K, V>(key, value);
    e.hashNext = table[index];
    table[index] = e;
    final Entry<K, V> top = this.top;
    e.next = top;
    if (top != null) {
      top.previous = e;
    } else {
      back = e;
    }
    this.top = e;
    size = size + 1;

    if (removeEldestEntry(back)) {
      remove(back.key);
    } else if (size > capacity) {
      rehash(HashUtil.nextCapacity(capacity));
    }
    return null;
  }
Exemple #2
0
 private void moveToTop(final Entry<K, V> e) {
   final Entry<K, V> top = this.top;
   if (top != e) {
     final Entry<K, V> prev = e.previous;
     final Entry<K, V> next = e.next;
     prev.next = next;
     if (next != null) {
       next.previous = prev;
     } else {
       back = prev;
     }
     top.previous = e;
     e.next = top;
     e.previous = null;
     this.top = e;
   }
 }
Exemple #3
0
 private void unlink(final Entry<K, V> e) {
   final Entry<K, V> prev = e.previous;
   final Entry<K, V> next = e.next;
   if (prev != null) {
     prev.next = next;
   } else {
     top = next;
   }
   if (next != null) {
     next.previous = prev;
   } else {
     back = prev;
   }
 }