PriorityCache { public synchronized boolean put(@NotNull K key, @NotNull V value, int generation, byte initialCost) { int hashCode = key.hashCode(); byte cheapest = initialCost; int index = -1; boolean eviction = false; for (int k = 0; k <= rehash; k++) { int i = project(hashCode, k); Entry<?, ?> entry = entries[i]; if (entry == Entry.NULL) { index = i; eviction = false; break; } else if (entry.generation <= generation && key.equals(entry.key)) { index = i; initialCost = entry.cost; if (initialCost < Byte.MAX_VALUE) { initialCost++; } eviction = false; break; } else if (entry.generation < generation) { index = i; eviction = false; break; } else if (entry.cost < cheapest) { cheapest = entry.cost; index = i; eviction = true; } } if (index >= 0) { Entry<?, ?> old = entries[index]; Entry<?, ?> newE = new Entry<>(key, value, generation, initialCost); entries[index] = newE; loadCount++; costs[initialCost - Byte.MIN_VALUE]++; if (old != Entry.NULL) { costs[old.cost - Byte.MIN_VALUE]--; if (eviction) { evictions[old.cost - Byte.MIN_VALUE]++; evictionCount++; } weight -= weighEntry(old); } else { size++; } weight += weighEntry(newE); return true; } else { loadExceptionCount++; return false; } } PriorityCache(int size, int rehash);  PriorityCache(int size, int rehash, @NotNull Weigher<K, V> weigher);  PriorityCache(int size, @NotNull Weigher<K, V> weigher);  PriorityCache(int size); static Supplier<PriorityCache<K, V>> factory(final int size, @NotNull final Weigher<K, V> weigher); static Supplier<PriorityCache<K, V>> factory(final int size); static long nextPowerOfTwo(int size); long size(); synchronized boolean put(@NotNull K key, @NotNull V value, int generation, byte initialCost); @SuppressWarnings("unchecked") @Nullable synchronized V get(@NotNull K key, int generation); synchronized void purgeGenerations(@NotNull Predicate<Integer> purge); @Override synchronized String toString(); @NotNull CacheStats getStats(); long estimateCurrentWeight();  }
@Test public void updateKey() { PriorityCache<String, Integer> cache = new PriorityCache<String, Integer>(1, 0); assertTrue(cache.put("one", 1, 0, (byte) 0)); assertFalse(cache.put("two", 2, 0, (byte) 0)); assertTrue(cache.put("one", 1, 0, (byte) 0)); assertFalse(cache.put("two", 2, 0, (byte) 1)); assertTrue(cache.put("two", 2, 0, (byte) 2)); }