@Override public V replace(K key, V value) { Assert.notNull(key, "key"); writeLock.lock(); try { if (entries.containsKey(key)) { return putInternal(key, value, expirationPolicy.get(), expirationNanos.get()); } else return null; } finally { writeLock.unlock(); } }
@Override public boolean containsValue(Object value) { readLock.lock(); try { return entries.containsValue(value); } finally { readLock.unlock(); } }
@Override public boolean replace(K key, V oldValue, V newValue) { Assert.notNull(key, "key"); writeLock.lock(); try { ExpiringEntry<K, V> entry = entries.get(key); if (entry != null && entry.getValue().equals(oldValue)) { putInternal(key, newValue, expirationPolicy.get(), expirationNanos.get()); return true; } else return false; } finally { writeLock.unlock(); } }
writeLock.lock(); try { ExpiringEntry<K, V> entry = entries.get(key); V oldValue = null; variableExpiration ? new AtomicReference<ExpirationPolicy>(expirationPolicy) : this.expirationPolicy, variableExpiration ? new AtomicLong(expirationNanos) : this.expirationNanos); if (entries.size() >= maxSize) { ExpiringEntry<K, V> expiredEntry = entries.first(); entries.remove(expiredEntry.key); notifyListeners(expiredEntry); entries.put(key, entry); if (entries.size() == 1 || entries.first().equals(entry)) scheduleEntry(entry); } else {
writeLock.lock(); try { ExpiringEntry<K, V> entry = entries.get(key); V oldValue = null; variableExpiration ? new AtomicReference<ExpirationPolicy>(expirationPolicy) : this.expirationPolicy, variableExpiration ? new AtomicLong(expirationNanos) : this.expirationNanos); if (entries.size() >= maxSize) { ExpiringEntry<K, V> expiredEntry = entries.first(); entries.remove(expiredEntry.key); notifyListeners(expiredEntry); entries.put(key, entry); if (entries.size() == 1 || entries.first().equals(entry)) scheduleEntry(entry); } else {
writeLock.lock(); try { ExpiringEntry<K, V> entry = entries.get(key); V oldValue = null; variableExpiration ? new AtomicReference<ExpirationPolicy>(expirationPolicy) : this.expirationPolicy, variableExpiration ? new AtomicLong(expirationNanos) : this.expirationNanos); if (entries.size() >= maxSize) { ExpiringEntry<K, V> expiredEntry = entries.first(); entries.remove(expiredEntry.key); notifyListeners(expiredEntry); entries.put(key, entry); if (entries.size() == 1 || entries.first().equals(entry)) scheduleEntry(entry); } else {
/** * Sets the expiration duration for the entry corresponding to the given key. Supported only if variable expiration is * enabled. * * @param key Key to set expiration for * @param duration the length of time after an entry is created that it should be removed * @param timeUnit the unit that {@code duration} is expressed in * @throws NullPointerException if {@code key} or {@code timeUnit} are null * @throws UnsupportedOperationException If variable expiration is not enabled */ public void setExpiration(K key, long duration, TimeUnit timeUnit) { Assert.notNull(key, "key"); Assert.notNull(timeUnit, "timeUnit"); Assert.operation(variableExpiration, "Variable expiration is not enabled"); writeLock.lock(); try { ExpiringEntry<K, V> entry = entries.get(key); if (entry != null) { entry.expirationNanos.set(TimeUnit.NANOSECONDS.convert(duration, timeUnit)); resetEntry(entry, true); } } finally { writeLock.unlock(); } }
/** * Sets the expiration duration for the entry corresponding to the given key. Supported only if variable expiration is * enabled. * * @param key Key to set expiration for * @param duration the length of time after an entry is created that it should be removed * @param timeUnit the unit that {@code duration} is expressed in * @throws NullPointerException if {@code key} or {@code timeUnit} are null * @throws UnsupportedOperationException If variable expiration is not enabled */ public void setExpiration(K key, long duration, TimeUnit timeUnit) { Assert.notNull(key, "key"); Assert.notNull(timeUnit, "timeUnit"); Assert.operation(variableExpiration, "Variable expiration is not enabled"); writeLock.lock(); try { ExpiringEntry<K, V> entry = entries.get(key); if (entry != null) { entry.expirationNanos.set(TimeUnit.NANOSECONDS.convert(duration, timeUnit)); resetEntry(entry, true); } } finally { writeLock.unlock(); } }
/** * Sets the expiration duration for the entry corresponding to the given key. Supported only if variable expiration is * enabled. * * @param key Key to set expiration for * @param duration the length of time after an entry is created that it should be removed * @param timeUnit the unit that {@code duration} is expressed in * @throws NullPointerException if {@code key} or {@code timeUnit} are null * @throws UnsupportedOperationException If variable expiration is not enabled */ public void setExpiration(K key, long duration, TimeUnit timeUnit) { Assert.notNull(key, "key"); Assert.notNull(timeUnit, "timeUnit"); Assert.operation(variableExpiration, "Variable expiration is not enabled"); writeLock.lock(); try { ExpiringEntry<K, V> entry = entries.get(key); if (entry != null) { entry.expirationNanos.set(TimeUnit.NANOSECONDS.convert(duration, timeUnit)); resetEntry(entry, true); } } finally { writeLock.unlock(); } }
/** * Resets the given entry's schedule canceling any existing scheduled expiration and reordering the entry in the * internal map. Schedules the next entry in the map if the given {@code entry} was scheduled or if * {@code scheduleNext} is true. * * @param entry to reset * @param scheduleFirstEntry whether the first entry should be automatically scheduled */ void resetEntry(ExpiringEntry<K, V> entry, boolean scheduleFirstEntry) { writeLock.lock(); try { boolean scheduled = entry.cancel(); entries.reorder(entry); if (scheduled || scheduleFirstEntry) scheduleEntry(entries.first()); } finally { writeLock.unlock(); } }
/** * Resets the given entry's schedule canceling any existing scheduled expiration and reordering the entry in the * internal map. Schedules the next entry in the map if the given {@code entry} was scheduled or if * {@code scheduleNext} is true. * * @param entry to reset * @param scheduleFirstEntry whether the first entry should be automatically scheduled */ void resetEntry(ExpiringEntry<K, V> entry, boolean scheduleFirstEntry) { writeLock.lock(); try { boolean scheduled = entry.cancel(); entries.reorder(entry); if (scheduled || scheduleFirstEntry) scheduleEntry(entries.first()); } finally { writeLock.unlock(); } }
/** * Resets the given entry's schedule canceling any existing scheduled expiration and reordering the entry in the * internal map. Schedules the next entry in the map if the given {@code entry} was scheduled or if * {@code scheduleNext} is true. * * @param entry to reset * @param scheduleFirstEntry whether the first entry should be automatically scheduled */ void resetEntry(ExpiringEntry<K, V> entry, boolean scheduleFirstEntry) { writeLock.lock(); try { boolean scheduled = entry.cancel(); entries.reorder(entry); if (scheduled || scheduleFirstEntry) scheduleEntry(entries.first()); } finally { writeLock.unlock(); } }
@Override public boolean remove(Object key, Object value) { Assert.notNull(key, "key"); writeLock.lock(); try { ExpiringEntry<K, V> entry = entries.get(key); if (entry != null && entry.getValue().equals(value)) { entries.remove(key); if (entry.cancel()) scheduleEntry(entries.first()); return true; } else return false; } finally { writeLock.unlock(); } }
@Override public boolean remove(Object key, Object value) { Assert.notNull(key, "key"); writeLock.lock(); try { ExpiringEntry<K, V> entry = entries.get(key); if (entry != null && entry.getValue().equals(value)) { entries.remove(key); if (entry.cancel()) scheduleEntry(entries.first()); return true; } else return false; } finally { writeLock.unlock(); } }
@Override public boolean remove(Object key, Object value) { Assert.notNull(key, "key"); writeLock.lock(); try { ExpiringEntry<K, V> entry = entries.get(key); if (entry != null && entry.getValue().equals(value)) { entries.remove(key); if (entry.cancel()) scheduleEntry(entries.first()); return true; } else return false; } finally { writeLock.unlock(); } }
@Override public boolean replace(K key, V oldValue, V newValue) { Assert.notNull(key, "key"); writeLock.lock(); try { ExpiringEntry<K, V> entry = entries.get(key); if (entry != null && entry.getValue().equals(oldValue)) { putInternal(key, newValue, expirationPolicy.get(), expirationNanos.get()); return true; } else return false; } finally { writeLock.unlock(); } }
@Override public boolean replace(K key, V oldValue, V newValue) { Assert.notNull(key, "key"); writeLock.lock(); try { ExpiringEntry<K, V> entry = entries.get(key); if (entry != null && entry.getValue().equals(oldValue)) { putInternal(key, newValue, expirationPolicy.get(), expirationNanos.get()); return true; } else return false; } finally { writeLock.unlock(); } }