/** * Returns the value for {@code key} if it exists in the cache or can be * created by {@code #create}. If a value was returned, it is moved to the * head of the queue. This returns null if a value is not cached and cannot * be created. */ public synchronized final V get(K key) { if (key == null) { throw new NullPointerException("key == null"); } V result = map.get(key); if (result != null) { return result; } result = create(key); if (result != null) { map.put(key, result); trimToSize(maxSize); } return result; }
private void trimToSize(int maxSize) { while (map.size() > maxSize) { Map.Entry<K, V> toEvict = map.eldest(); K key = toEvict.getKey(); V value = toEvict.getValue(); map.remove(key); entryEvicted(key, value); } }
/** * Removes all entries from the cache. */ public void clear() { cache.evictAll(); }
public static String getBestDateTimePattern(String skeleton, String localeName) { String key = skeleton + "\t" + localeName; synchronized (CACHED_PATTERNS) { String pattern = CACHED_PATTERNS.get(key); if (pattern == null) { pattern = getBestDateTimePatternNative(skeleton, localeName); CACHED_PATTERNS.put(key, pattern); } return pattern; } }
/** * Clear the cache, calling {@link #entryEvicted} on each removed entry. */ public synchronized final void evictAll() { trimToSize(0); } }
/** * Records that 'hostname' is known not to have any associated addresses. (I.e. insert a * negative cache entry.) */ public void putUnknownHost(String hostname, String detailMessage) { cache.put(hostname, new AddressCacheEntry(detailMessage)); } }
/** * Returns a shared, mutable array containing the constants of this enum. It * is an error to modify the returned array. * * @hide */ @SuppressWarnings("unchecked") // the cache always returns the type matching enumType public static <T extends Enum<T>> T[] getSharedConstants(Class<T> enumType) { return (T[]) sharedConstantsCache.get(enumType); }
public static String getBestDateTimePattern(String skeleton, String localeName) { String key = skeleton + "\t" + localeName; synchronized (CACHED_PATTERNS) { String pattern = CACHED_PATTERNS.get(key); if (pattern == null) { pattern = getBestDateTimePatternNative(skeleton, localeName); CACHED_PATTERNS.put(key, pattern); } return pattern; } }
/** * Caches {@code value} for {@code key}. The value is moved to the head of * the queue. * * @return the previous value mapped by {@code key}. Although that entry is * no longer cached, it has not been passed to {@link #entryEvicted}. */ public synchronized final V put(K key, V value) { if (key == null) { throw new NullPointerException("key == null"); } else if (value == null) { throw new NullPointerException("value == null"); } V previous = map.put(key, value); trimToSize(maxSize); return previous; }
/** * Associates the given 'addresses' with 'hostname'. The association will expire after a * certain length of time. */ public void put(String hostname, InetAddress[] addresses) { cache.put(hostname, new AddressCacheEntry(addresses)); }
/** * Returns the cached InetAddress[] associated with 'hostname'. Returns null if nothing is known * about 'hostname'. Returns a String suitable for use as an UnknownHostException detail * message if 'hostname' is known not to exist. */ public Object get(String hostname) { AddressCacheEntry entry = cache.get(hostname); // Do we have a valid cache entry? if (entry != null && entry.expiryNanos >= System.nanoTime()) { return entry.value; } // Either we didn't find anything, or it had expired. // No need to remove expired entries: the caller will provide a replacement shortly. return null; }
public static String getBestDateTimePattern(String skeleton, String localeName) { String key = skeleton + "\t" + localeName; synchronized (CACHED_PATTERNS) { String pattern = CACHED_PATTERNS.get(key); if (pattern == null) { pattern = getBestDateTimePatternNative(skeleton, localeName); CACHED_PATTERNS.put(key, pattern); } return pattern; } }
/** * Returns the value for {@code key} if it exists in the cache or can be * created by {@code #create}. If a value was returned, it is moved to the * head of the queue. This returns null if a value is not cached and cannot * be created. */ public synchronized final V get(K key) { if (key == null) { throw new NullPointerException("key == null"); } V result = map.get(key); if (result != null) { return result; } result = create(key); if (result != null) { map.put(key, result); trimToSize(maxSize); } return result; }
/** * Clear the cache, calling {@link #entryEvicted} on each removed entry. */ public synchronized final void evictAll() { trimToSize(0); } }
/** * Records that 'hostname' is known not to have any associated addresses. (I.e. insert a * negative cache entry.) */ public void putUnknownHost(String hostname, String detailMessage) { cache.put(hostname, new AddressCacheEntry(detailMessage)); } }
/** * Returns a shared, mutable array containing the constants of this enum. It * is an error to modify the returned array. * * @hide */ @SuppressWarnings("unchecked") // the cache always returns the type matching enumType public static <T extends Enum<T>> T[] getSharedConstants(Class<T> enumType) { return (T[]) sharedConstantsCache.get(enumType); }
/** * Removes all entries from the cache. */ public void clear() { cache.evictAll(); }
private void trimToSize(int maxSize) { while (map.size() > maxSize) { Map.Entry<K, V> toEvict = map.eldest(); K key = toEvict.getKey(); V value = toEvict.getValue(); map.remove(key); entryEvicted(key, value); } }
public static String getBestDateTimePattern(String skeleton, String localeName) { String key = skeleton + "\t" + localeName; synchronized (CACHED_PATTERNS) { String pattern = CACHED_PATTERNS.get(key); if (pattern == null) { pattern = getBestDateTimePatternNative(skeleton, localeName); CACHED_PATTERNS.put(key, pattern); } return pattern; } }
/** * Returns the value for {@code key} if it exists in the cache or can be * created by {@code #create}. If a value was returned, it is moved to the * head of the queue. This returns null if a value is not cached and cannot * be created. */ public synchronized final V get(K key) { if (key == null) { throw new NullPointerException("key == null"); } V result = map.get(key); if (result != null) { return result; } result = create(key); if (result != null) { map.put(key, result); trimToSize(maxSize); } return result; }