diff --git a/src/share/classes/sun/security/pkcs11/KeyCache.java b/src/share/classes/sun/security/pkcs11/KeyCache.java index 359156f7dc9040e97c0972390af880c41938dcbb..1687649e10e957fa9dcac292cf07bdd91dfbd781 100644 --- a/src/share/classes/sun/security/pkcs11/KeyCache.java +++ b/src/share/classes/sun/security/pkcs11/KeyCache.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -48,7 +48,7 @@ import sun.security.util.Cache; */ final class KeyCache { - private final Cache strongCache; + private final Cache strongCache; private WeakReference> cacheReference; @@ -77,7 +77,7 @@ final class KeyCache { } synchronized P11Key get(Key key) { - P11Key p11Key = (P11Key)strongCache.get(new IdentityWrapper(key)); + P11Key p11Key = strongCache.get(new IdentityWrapper(key)); if (p11Key != null) { return p11Key; } @@ -94,8 +94,8 @@ final class KeyCache { Map map = (cacheReference == null) ? null : cacheReference.get(); if (map == null) { - map = new IdentityHashMap(); - cacheReference = new WeakReference>(map); + map = new IdentityHashMap<>(); + cacheReference = new WeakReference<>(map); } map.put(key, p11Key); } diff --git a/src/share/classes/sun/security/provider/X509Factory.java b/src/share/classes/sun/security/provider/X509Factory.java index 0260ff5c7bf3ea071498859725ae3000a7b4b095..b3d738fb8568fb18021901e1bf51cb725d502ae5 100644 --- a/src/share/classes/sun/security/provider/X509Factory.java +++ b/src/share/classes/sun/security/provider/X509Factory.java @@ -64,8 +64,10 @@ public class X509Factory extends CertificateFactorySpi { private static final int ENC_MAX_LENGTH = 4096 * 1024; // 4 MB MAX - private static final Cache certCache = Cache.newSoftMemoryCache(750); - private static final Cache crlCache = Cache.newSoftMemoryCache(750); + private static final Cache certCache + = Cache.newSoftMemoryCache(750); + private static final Cache crlCache + = Cache.newSoftMemoryCache(750); /** * Generates an X.509 certificate object and initializes it with @@ -90,7 +92,7 @@ public class X509Factory extends CertificateFactorySpi { try { byte[] encoding = readOneBlock(is); if (encoding != null) { - X509CertImpl cert = (X509CertImpl)getFromCache(certCache, encoding); + X509CertImpl cert = getFromCache(certCache, encoding); if (cert != null) { return cert; } @@ -151,7 +153,7 @@ public class X509Factory extends CertificateFactorySpi { } else { encoding = c.getEncoded(); } - X509CertImpl newC = (X509CertImpl)getFromCache(certCache, encoding); + X509CertImpl newC = getFromCache(certCache, encoding); if (newC != null) { return newC; } @@ -181,7 +183,7 @@ public class X509Factory extends CertificateFactorySpi { } else { encoding = c.getEncoded(); } - X509CRLImpl newC = (X509CRLImpl)getFromCache(crlCache, encoding); + X509CRLImpl newC = getFromCache(crlCache, encoding); if (newC != null) { return newC; } @@ -198,18 +200,17 @@ public class X509Factory extends CertificateFactorySpi { /** * Get the X509CertImpl or X509CRLImpl from the cache. */ - private static synchronized Object getFromCache(Cache cache, + private static synchronized V getFromCache(Cache cache, byte[] encoding) { Object key = new Cache.EqualByteArray(encoding); - Object value = cache.get(key); - return value; + return cache.get(key); } /** * Add the X509CertImpl or X509CRLImpl to the cache. */ - private static synchronized void addToCache(Cache cache, byte[] encoding, - Object value) { + private static synchronized void addToCache(Cache cache, + byte[] encoding, V value) { if (encoding.length > ENC_MAX_LENGTH) { return; } @@ -361,7 +362,7 @@ public class X509Factory extends CertificateFactorySpi { try { byte[] encoding = readOneBlock(is); if (encoding != null) { - X509CRLImpl crl = (X509CRLImpl)getFromCache(crlCache, encoding); + X509CRLImpl crl = getFromCache(crlCache, encoding); if (crl != null) { return crl; } diff --git a/src/share/classes/sun/security/provider/certpath/CertStoreHelper.java b/src/share/classes/sun/security/provider/certpath/CertStoreHelper.java index e325239d11489836b88368d1989799ccdb333aee..8670778ee7e7252483e54154a9482aa57f6428aa 100644 --- a/src/share/classes/sun/security/provider/certpath/CertStoreHelper.java +++ b/src/share/classes/sun/security/provider/certpath/CertStoreHelper.java @@ -59,12 +59,13 @@ public abstract class CertStoreHelper { "SSLServer", "sun.security.provider.certpath.ssl.SSLServerCertStoreHelper"); }; - private static Cache cache = Cache.newSoftMemoryCache(NUM_TYPES); + private static Cache cache + = Cache.newSoftMemoryCache(NUM_TYPES); public static CertStoreHelper getInstance(final String type) throws NoSuchAlgorithmException { - CertStoreHelper helper = (CertStoreHelper)cache.get(type); + CertStoreHelper helper = cache.get(type); if (helper != null) { return helper; } diff --git a/src/share/classes/sun/security/provider/certpath/URICertStore.java b/src/share/classes/sun/security/provider/certpath/URICertStore.java index da607724437ff04f92a45a206455e1103c4bc698..6fb7bc72af312902739de7fbb1aacd7775fa022e 100644 --- a/src/share/classes/sun/security/provider/certpath/URICertStore.java +++ b/src/share/classes/sun/security/provider/certpath/URICertStore.java @@ -100,8 +100,7 @@ class URICertStore extends CertStoreSpi { private final CertificateFactory factory; // cached Collection of X509Certificates (may be empty, never null) - private Collection certs = - Collections.emptySet(); + private Collection certs = Collections.emptySet(); // cached X509CRL (may be null) private X509CRL crl; @@ -157,14 +156,14 @@ class URICertStore extends CertStoreSpi { * Returns a URI CertStore. This method consults a cache of * CertStores (shared per JVM) using the URI as a key. */ - private static final Cache certStoreCache = - Cache.newSoftMemoryCache(CACHE_SIZE); + private static final Cache + certStoreCache = Cache.newSoftMemoryCache(CACHE_SIZE); static synchronized CertStore getInstance(URICertStoreParameters params) throws NoSuchAlgorithmException, InvalidAlgorithmParameterException { if (debug != null) { debug.println("CertStore URI:" + params.uri); } - CertStore ucs = (CertStore) certStoreCache.get(params); + CertStore ucs = certStoreCache.get(params); if (ucs == null) { ucs = new UCS(new URICertStore(params), null, "URI", params); certStoreCache.put(params, ucs); @@ -287,7 +286,7 @@ class URICertStore extends CertStoreSpi { } // exception, forget previous values lastModified = 0; - certs = Collections.emptySet(); + certs = Collections.emptySet(); return certs; } @@ -394,7 +393,7 @@ class URICertStore extends CertStoreSpi { // exception, forget previous values lastModified = 0; crl = null; - return Collections.emptyList(); + return Collections.emptyList(); } /** @@ -404,9 +403,9 @@ class URICertStore extends CertStoreSpi { private static Collection getMatchingCRLs (X509CRL crl, CRLSelector selector) { if (selector == null || (crl != null && selector.match(crl))) { - return Collections.singletonList(crl); + return Collections.singletonList(crl); } else { - return Collections.emptyList(); + return Collections.emptyList(); } } diff --git a/src/share/classes/sun/security/provider/certpath/X509CertificatePair.java b/src/share/classes/sun/security/provider/certpath/X509CertificatePair.java index 20d05f71be82e1d06ee117e1fb3a9168926711b7..3228b7ed62e269ed878fe781bc5aac757584c8e1 100644 --- a/src/share/classes/sun/security/provider/certpath/X509CertificatePair.java +++ b/src/share/classes/sun/security/provider/certpath/X509CertificatePair.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2000, 2002, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -79,7 +79,8 @@ public class X509CertificatePair { private X509Certificate reverse; private byte[] encoded; - private static final Cache cache = Cache.newSoftMemoryCache(750); + private static final Cache cache + = Cache.newSoftMemoryCache(750); /** * Creates an empty instance of X509CertificatePair. @@ -114,7 +115,7 @@ public class X509CertificatePair { * * For internal use only, external code should use generateCertificatePair. */ - private X509CertificatePair(byte[] encoded)throws CertificateException { + private X509CertificatePair(byte[] encoded) throws CertificateException { try { parse(new DerValue(encoded)); this.encoded = encoded; @@ -138,7 +139,7 @@ public class X509CertificatePair { public static synchronized X509CertificatePair generateCertificatePair (byte[] encoded) throws CertificateException { Object key = new Cache.EqualByteArray(encoded); - X509CertificatePair pair = (X509CertificatePair)cache.get(key); + X509CertificatePair pair = cache.get(key); if (pair != null) { return pair; } diff --git a/src/share/classes/sun/security/provider/certpath/ldap/LDAPCertStore.java b/src/share/classes/sun/security/provider/certpath/ldap/LDAPCertStore.java index 3eea0b25fff70fbc84426950c88203ef028f0509..eb20b3f704d6c32c4019a16fff0c2ecd1b7e8f5a 100644 --- a/src/share/classes/sun/security/provider/certpath/ldap/LDAPCertStore.java +++ b/src/share/classes/sun/security/provider/certpath/ldap/LDAPCertStore.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2000, 2006, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -160,7 +160,7 @@ public final class LDAPCertStore extends CertStoreSpi { */ private boolean prefetchCRLs = false; - private final Cache valueCache; + private final Cache valueCache; private int cacheHits = 0; private int cacheMisses = 0; @@ -207,10 +207,11 @@ public final class LDAPCertStore extends CertStoreSpi { * Returns an LDAP CertStore. This method consults a cache of * CertStores (shared per JVM) using the LDAP server/port as a key. */ - private static final Cache certStoreCache = Cache.newSoftMemoryCache(185); + private static final Cache + certStoreCache = Cache.newSoftMemoryCache(185); static synchronized CertStore getInstance(LDAPCertStoreParameters params) throws NoSuchAlgorithmException, InvalidAlgorithmParameterException { - CertStore lcs = (CertStore) certStoreCache.get(params); + CertStore lcs = certStoreCache.get(params); if (lcs == null) { lcs = CertStore.getInstance("LDAP", params); certStoreCache.put(params, lcs); @@ -232,7 +233,7 @@ public final class LDAPCertStore extends CertStoreSpi { private void createInitialDirContext(String server, int port) throws InvalidAlgorithmParameterException { String url = "ldap://" + server + ":" + port; - Hashtable env = new Hashtable(); + Hashtable env = new Hashtable<>(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, url); @@ -283,7 +284,7 @@ public final class LDAPCertStore extends CertStoreSpi { LDAPRequest(String name) { this.name = name; - requestedAttributes = new ArrayList(5); + requestedAttributes = new ArrayList<>(5); } String getName() { @@ -311,7 +312,7 @@ public final class LDAPCertStore extends CertStoreSpi { + cacheMisses); } String cacheKey = name + "|" + attrId; - byte[][] values = (byte[][])valueCache.get(cacheKey); + byte[][] values = valueCache.get(cacheKey); if (values != null) { cacheHits++; return values; @@ -347,7 +348,7 @@ public final class LDAPCertStore extends CertStoreSpi { System.out.println("LDAP requests: " + requests); } } - valueMap = new HashMap(8); + valueMap = new HashMap<>(8); String[] attrIds = requestedAttributes.toArray(STRING0); Attributes attrs; try { @@ -429,10 +430,10 @@ public final class LDAPCertStore extends CertStoreSpi { int n = encodedCert.length; if (n == 0) { - return Collections.emptySet(); + return Collections.emptySet(); } - List certs = new ArrayList(n); + List certs = new ArrayList<>(n); /* decode certs and check if they satisfy selector */ for (int i = 0; i < n; i++) { ByteArrayInputStream bais = new ByteArrayInputStream(encodedCert[i]); @@ -477,11 +478,10 @@ public final class LDAPCertStore extends CertStoreSpi { int n = encodedCertPair.length; if (n == 0) { - return Collections.emptySet(); + return Collections.emptySet(); } - List certPairs = - new ArrayList(n); + List certPairs = new ArrayList<>(n); /* decode each cert pair and add it to the Collection */ for (int i = 0; i < n; i++) { try { @@ -528,8 +528,7 @@ public final class LDAPCertStore extends CertStoreSpi { getCertPairs(request, CROSS_CERT); // Find Certificates that match and put them in a list - ArrayList matchingCerts = - new ArrayList(); + ArrayList matchingCerts = new ArrayList<>(); for (X509CertificatePair certPair : certPairs) { X509Certificate cert; if (forward != null) { @@ -587,7 +586,7 @@ public final class LDAPCertStore extends CertStoreSpi { int basicConstraints = xsel.getBasicConstraints(); String subject = xsel.getSubjectAsString(); String issuer = xsel.getIssuerAsString(); - HashSet certs = new HashSet(); + HashSet certs = new HashSet<>(); if (debug != null) { debug.println("LDAPCertStore.engineGetCertificates() basicConstraints: " + basicConstraints); @@ -706,10 +705,10 @@ public final class LDAPCertStore extends CertStoreSpi { int n = encodedCRL.length; if (n == 0) { - return Collections.emptySet(); + return Collections.emptySet(); } - List crls = new ArrayList(n); + List crls = new ArrayList<>(n); /* decode each crl and check if it matches selector */ for (int i = 0; i < n; i++) { try { @@ -765,13 +764,13 @@ public final class LDAPCertStore extends CertStoreSpi { throw new CertStoreException("need X509CRLSelector to find CRLs"); } X509CRLSelector xsel = (X509CRLSelector) selector; - HashSet crls = new HashSet(); + HashSet crls = new HashSet<>(); // Look in directory entry for issuer of cert we're checking. Collection issuerNames; X509Certificate certChecking = xsel.getCertificateChecking(); if (certChecking != null) { - issuerNames = new HashSet(); + issuerNames = new HashSet<>(); X500Principal issuer = certChecking.getIssuerX500Principal(); issuerNames.add(issuer.getName(X500Principal.RFC2253)); } else { @@ -796,7 +795,7 @@ public final class LDAPCertStore extends CertStoreSpi { issuerName = (String)nameObject; } // If all we want is CA certs, try to get the (probably shorter) ARL - Collection entryCRLs = Collections.emptySet(); + Collection entryCRLs = Collections.emptySet(); if (certChecking == null || certChecking.getBasicConstraints() != -1) { LDAPRequest request = new LDAPRequest(issuerName); request.addRequestedAttribute(CROSS_CERT); @@ -1028,9 +1027,9 @@ public final class LDAPCertStore extends CertStoreSpi { throws IOException { this.selector = selector == null ? new X509CRLSelector() : selector; this.certIssuers = certIssuers; - issuerNames = new HashSet(); + issuerNames = new HashSet<>(); issuerNames.add(ldapDN); - issuers = new HashSet(); + issuers = new HashSet<>(); issuers.add(new X500Name(ldapDN).asX500Principal()); } // we only override the get (accessor methods) since the set methods diff --git a/src/share/classes/sun/security/ssl/SSLSessionContextImpl.java b/src/share/classes/sun/security/ssl/SSLSessionContextImpl.java index ce1336bf122ced47111d4969ba8d970306fbb747..7d0ea3faf97a89d3bc3829d2ccdff7680b41b75b 100644 --- a/src/share/classes/sun/security/ssl/SSLSessionContextImpl.java +++ b/src/share/classes/sun/security/ssl/SSLSessionContextImpl.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1999, 2009, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -43,11 +43,14 @@ import javax.net.ssl.SSLPeerUnverifiedException; import javax.net.ssl.SSLSession; import sun.security.util.Cache; +import sun.security.util.Cache.CacheVisitor; final class SSLSessionContextImpl implements SSLSessionContext { - private Cache sessionCache; // session cache, session id as key - private Cache sessionHostPortCache; // session cache, "host:port" as key + private Cache sessionCache; + // session cache, session id as key + private Cache sessionHostPortCache; + // session cache, "host:port" as key private int cacheLimit; // the max cache size private int timeout; // timeout in seconds @@ -71,8 +74,7 @@ final class SSLSessionContextImpl implements SSLSessionContext { throw new NullPointerException("session id cannot be null"); } - SSLSessionImpl sess = - (SSLSessionImpl)sessionCache.get(new SessionId(sessionId)); + SSLSessionImpl sess = sessionCache.get(new SessionId(sessionId)); if (!isTimedout(sess)) { return sess; } @@ -157,8 +159,7 @@ final class SSLSessionContextImpl implements SSLSessionContext { return null; } - SSLSessionImpl sess = - (SSLSessionImpl)sessionHostPortCache.get(getKey(hostname, port)); + SSLSessionImpl sess = sessionHostPortCache.get(getKey(hostname, port)); if (!isTimedout(sess)) { return sess; } @@ -193,7 +194,7 @@ final class SSLSessionContextImpl implements SSLSessionContext { // package-private method, remove a cached SSLSession void remove(SessionId key) { - SSLSessionImpl s = (SSLSessionImpl)sessionCache.get(key); + SSLSessionImpl s = sessionCache.get(key); if (s != null) { sessionCache.remove(key); sessionHostPortCache.remove( @@ -233,17 +234,17 @@ final class SSLSessionContextImpl implements SSLSessionContext { } final class SessionCacheVisitor - implements sun.security.util.Cache.CacheVisitor { + implements Cache.CacheVisitor { Vector ids = null; - // public void visit(java.util.Map map) {} - public void visit(java.util.Map map) { - ids = new Vector(map.size()); + // public void visit(java.util.Map map) {} + public void visit(java.util.Map map) { + ids = new Vector<>(map.size()); - for (Object key : map.keySet()) { - SSLSessionImpl value = (SSLSessionImpl)map.get(key); + for (SessionId key : map.keySet()) { + SSLSessionImpl value = map.get(key); if (!isTimedout(value)) { - ids.addElement(((SessionId)key).getId()); + ids.addElement(key.getId()); } } } diff --git a/src/share/classes/sun/security/util/Cache.java b/src/share/classes/sun/security/util/Cache.java index 7c7f072b5de65f8c07e793ed25a0ffd4a677edce..8037324928af218ef34f93934dd4844c53825c84 100644 --- a/src/share/classes/sun/security/util/Cache.java +++ b/src/share/classes/sun/security/util/Cache.java @@ -43,7 +43,7 @@ import java.lang.ref.*; * * . optional lifetime, specified in seconds. * - * . save for concurrent use by multiple threads + * . safe for concurrent use by multiple threads * * . values are held by either standard references or via SoftReferences. * SoftReferences have the advantage that they are automatically cleared @@ -69,7 +69,7 @@ import java.lang.ref.*; * * @author Andreas Sterbenz */ -public abstract class Cache { +public abstract class Cache { protected Cache() { // empty @@ -88,12 +88,12 @@ public abstract class Cache { /** * Add an entry to the cache. */ - public abstract void put(Object key, Object value); + public abstract void put(K key, V value); /** * Get a value from the cache. */ - public abstract Object get(Object key); + public abstract V get(Object key); /** * Remove an entry from the cache. @@ -113,14 +113,14 @@ public abstract class Cache { /** * accept a visitor */ - public abstract void accept(CacheVisitor visitor); + public abstract void accept(CacheVisitor visitor); /** * Return a new memory cache with the specified maximum size, unlimited * lifetime for entries, with the values held by SoftReferences. */ - public static Cache newSoftMemoryCache(int size) { - return new MemoryCache(true, size); + public static Cache newSoftMemoryCache(int size) { + return new MemoryCache<>(true, size); } /** @@ -128,23 +128,24 @@ public abstract class Cache { * specified maximum lifetime (in seconds), with the values held * by SoftReferences. */ - public static Cache newSoftMemoryCache(int size, int timeout) { - return new MemoryCache(true, size, timeout); + public static Cache newSoftMemoryCache(int size, int timeout) { + return new MemoryCache<>(true, size, timeout); } /** * Return a new memory cache with the specified maximum size, unlimited * lifetime for entries, with the values held by standard references. */ - public static Cache newHardMemoryCache(int size) { - return new MemoryCache(false, size); + public static Cache newHardMemoryCache(int size) { + return new MemoryCache<>(false, size); } /** * Return a dummy cache that does nothing. */ - public static Cache newNullCache() { - return NullCache.INSTANCE; + @SuppressWarnings("unchecked") + public static Cache newNullCache() { + return (Cache) NullCache.INSTANCE; } /** @@ -152,8 +153,8 @@ public abstract class Cache { * specified maximum lifetime (in seconds), with the values held * by standard references. */ - public static Cache newHardMemoryCache(int size, int timeout) { - return new MemoryCache(false, size, timeout); + public static Cache newHardMemoryCache(int size, int timeout) { + return new MemoryCache<>(false, size, timeout); } /** @@ -193,15 +194,15 @@ public abstract class Cache { } } - public interface CacheVisitor { - public void visit(Map map); + public interface CacheVisitor { + public void visit(Map map); } } -class NullCache extends Cache { +class NullCache extends Cache { - final static Cache INSTANCE = new NullCache(); + final static Cache INSTANCE = new NullCache<>(); private NullCache() { // empty @@ -215,11 +216,11 @@ class NullCache extends Cache { // empty } - public void put(Object key, Object value) { + public void put(K key, V value) { // empty } - public Object get(Object key) { + public V get(Object key) { return null; } @@ -235,23 +236,26 @@ class NullCache extends Cache { // empty } - public void accept(CacheVisitor visitor) { + public void accept(CacheVisitor visitor) { // empty } } -class MemoryCache extends Cache { +class MemoryCache extends Cache { private final static float LOAD_FACTOR = 0.75f; // XXXX private final static boolean DEBUG = false; - private final Map cacheMap; + private final Map> cacheMap; private int maxSize; private long lifetime; - private final ReferenceQueue queue; + + // ReferenceQueue is of type V instead of Cache + // to allow SoftCacheEntry to extend SoftReference + private final ReferenceQueue queue; public MemoryCache(boolean soft, int maxSize) { this(soft, maxSize, 0); @@ -260,10 +264,13 @@ class MemoryCache extends Cache { public MemoryCache(boolean soft, int maxSize, int lifetime) { this.maxSize = maxSize; this.lifetime = lifetime * 1000; - this.queue = soft ? new ReferenceQueue() : null; + if (soft) + this.queue = new ReferenceQueue<>(); + else + this.queue = null; + int buckets = (int)(maxSize / LOAD_FACTOR) + 1; - cacheMap = new LinkedHashMap(buckets, - LOAD_FACTOR, true); + cacheMap = new LinkedHashMap<>(buckets, LOAD_FACTOR, true); } /** @@ -279,16 +286,17 @@ class MemoryCache extends Cache { } int startSize = cacheMap.size(); while (true) { - CacheEntry entry = (CacheEntry)queue.poll(); + @SuppressWarnings("unchecked") + CacheEntry entry = (CacheEntry)queue.poll(); if (entry == null) { break; } - Object key = entry.getKey(); + K key = entry.getKey(); if (key == null) { // key is null, entry has already been removed continue; } - CacheEntry currentEntry = cacheMap.remove(key); + CacheEntry currentEntry = cacheMap.remove(key); // check if the entry in the map corresponds to the expired // entry. If not, readd the entry if ((currentEntry != null) && (entry != currentEntry)) { @@ -314,9 +322,9 @@ class MemoryCache extends Cache { } int cnt = 0; long time = System.currentTimeMillis(); - for (Iterator t = cacheMap.values().iterator(); + for (Iterator> t = cacheMap.values().iterator(); t.hasNext(); ) { - CacheEntry entry = t.next(); + CacheEntry entry = t.next(); if (entry.isValid(time) == false) { t.remove(); cnt++; @@ -339,7 +347,7 @@ class MemoryCache extends Cache { if (queue != null) { // if this is a SoftReference cache, first invalidate() all // entries so that GC does not have to enqueue them - for (CacheEntry entry : cacheMap.values()) { + for (CacheEntry entry : cacheMap.values()) { entry.invalidate(); } while (queue.poll() != null) { @@ -349,12 +357,12 @@ class MemoryCache extends Cache { cacheMap.clear(); } - public synchronized void put(Object key, Object value) { + public synchronized void put(K key, V value) { emptyQueue(); long expirationTime = (lifetime == 0) ? 0 : System.currentTimeMillis() + lifetime; - CacheEntry newEntry = newEntry(key, value, expirationTime, queue); - CacheEntry oldEntry = cacheMap.put(key, newEntry); + CacheEntry newEntry = newEntry(key, value, expirationTime, queue); + CacheEntry oldEntry = cacheMap.put(key, newEntry); if (oldEntry != null) { oldEntry.invalidate(); return; @@ -362,8 +370,8 @@ class MemoryCache extends Cache { if (maxSize > 0 && cacheMap.size() > maxSize) { expungeExpiredEntries(); if (cacheMap.size() > maxSize) { // still too large? - Iterator t = cacheMap.values().iterator(); - CacheEntry lruEntry = t.next(); + Iterator> t = cacheMap.values().iterator(); + CacheEntry lruEntry = t.next(); if (DEBUG) { System.out.println("** Overflow removal " + lruEntry.getKey() + " | " + lruEntry.getValue()); @@ -374,9 +382,9 @@ class MemoryCache extends Cache { } } - public synchronized Object get(Object key) { + public synchronized V get(Object key) { emptyQueue(); - CacheEntry entry = cacheMap.get(key); + CacheEntry entry = cacheMap.get(key); if (entry == null) { return null; } @@ -393,7 +401,7 @@ class MemoryCache extends Cache { public synchronized void remove(Object key) { emptyQueue(); - CacheEntry entry = cacheMap.remove(key); + CacheEntry entry = cacheMap.remove(key); if (entry != null) { entry.invalidate(); } @@ -402,9 +410,9 @@ class MemoryCache extends Cache { public synchronized void setCapacity(int size) { expungeExpiredEntries(); if (size > 0 && cacheMap.size() > size) { - Iterator t = cacheMap.values().iterator(); + Iterator> t = cacheMap.values().iterator(); for (int i = cacheMap.size() - size; i > 0; i--) { - CacheEntry lruEntry = t.next(); + CacheEntry lruEntry = t.next(); if (DEBUG) { System.out.println("** capacity reset removal " + lruEntry.getKey() + " | " + lruEntry.getValue()); @@ -431,60 +439,61 @@ class MemoryCache extends Cache { } // it is a heavyweight method. - public synchronized void accept(CacheVisitor visitor) { + public synchronized void accept(CacheVisitor visitor) { expungeExpiredEntries(); - Map cached = getCachedEntries(); + Map cached = getCachedEntries(); visitor.visit(cached); } - private Map getCachedEntries() { - Map kvmap = new HashMap(cacheMap.size()); + private Map getCachedEntries() { + Map kvmap = new HashMap<>(cacheMap.size()); - for (CacheEntry entry : cacheMap.values()) { + for (CacheEntry entry : cacheMap.values()) { kvmap.put(entry.getKey(), entry.getValue()); } return kvmap; } - protected CacheEntry newEntry(Object key, Object value, - long expirationTime, ReferenceQueue queue) { + protected CacheEntry newEntry(K key, V value, + long expirationTime, ReferenceQueue queue) { if (queue != null) { - return new SoftCacheEntry(key, value, expirationTime, queue); + return new SoftCacheEntry<>(key, value, expirationTime, queue); } else { - return new HardCacheEntry(key, value, expirationTime); + return new HardCacheEntry<>(key, value, expirationTime); } } - private static interface CacheEntry { + private static interface CacheEntry { boolean isValid(long currentTime); void invalidate(); - Object getKey(); + K getKey(); - Object getValue(); + V getValue(); } - private static class HardCacheEntry implements CacheEntry { + private static class HardCacheEntry implements CacheEntry { - private Object key, value; + private K key; + private V value; private long expirationTime; - HardCacheEntry(Object key, Object value, long expirationTime) { + HardCacheEntry(K key, V value, long expirationTime) { this.key = key; this.value = value; this.expirationTime = expirationTime; } - public Object getKey() { + public K getKey() { return key; } - public Object getValue() { + public V getValue() { return value; } @@ -503,24 +512,25 @@ class MemoryCache extends Cache { } } - private static class SoftCacheEntry - extends SoftReference implements CacheEntry { + private static class SoftCacheEntry + extends SoftReference + implements CacheEntry { - private Object key; + private K key; private long expirationTime; - SoftCacheEntry(Object key, Object value, long expirationTime, - ReferenceQueue queue) { + SoftCacheEntry(K key, V value, long expirationTime, + ReferenceQueue queue) { super(value, queue); this.key = key; this.expirationTime = expirationTime; } - public Object getKey() { + public K getKey() { return key; } - public Object getValue() { + public V getValue() { return get(); }