8198966: To make CoderResult.Cache.cache final and allocate it eagerly
authorsherman
Sun, 04 Mar 2018 00:32:53 -0800
changeset 49125 c5eb27eed365
parent 49124 6abbc1f5c2a1
child 49126 f29637bb390e
8198966: To make CoderResult.Cache.cache final and allocate it eagerly Reviewed-by: redestad
src/java.base/share/classes/java/nio/charset/CoderResult.java
--- a/src/java.base/share/classes/java/nio/charset/CoderResult.java	Sat Mar 03 08:39:08 2018 +0100
+++ b/src/java.base/share/classes/java/nio/charset/CoderResult.java	Sun Mar 04 00:32:53 2018 -0800
@@ -25,8 +25,8 @@
 
 package java.nio.charset;
 
-import java.lang.ref.WeakReference;
-import java.nio.*;
+import java.nio.BufferOverflowException;
+import java.nio.BufferUnderflowException;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.Map;
 
@@ -190,35 +190,14 @@
     public static final CoderResult OVERFLOW
         = new CoderResult(CR_OVERFLOW, 0);
 
-    private abstract static class Cache {
-
-        private Map<Integer,WeakReference<CoderResult>> cache = null;
-
-        protected abstract CoderResult create(int len);
+    private static final class Cache {
+        static final Cache INSTANCE = new Cache();
+        private Cache() {}
 
-        private CoderResult get(int len) {
-            Integer k = len;
-            WeakReference<CoderResult> w;
-            CoderResult e = null;
-            if (cache == null) {
-                cache = new ConcurrentHashMap<>();
-            } else if ((w = cache.get(k)) != null) {
-                e = w.get();
-            }
-            if (e == null) {
-                e = create(len);
-                cache.put(k, new WeakReference<>(e));
-            }
-            return e;
-        }
+        final Map<Integer, CoderResult> unmappable = new ConcurrentHashMap<>();
+        final Map<Integer, CoderResult> malformed  = new ConcurrentHashMap<>();
     }
 
-    private static final Cache malformedCache
-        = new Cache() {
-                public CoderResult create(int len) {
-                    return new CoderResult(CR_MALFORMED, len);
-                }};
-
     private static final CoderResult[] malformed4 = new CoderResult[] {
         new CoderResult(CR_MALFORMED, 1),
         new CoderResult(CR_MALFORMED, 2),
@@ -240,15 +219,10 @@
             throw new IllegalArgumentException("Non-positive length");
         if (length <= 4)
             return malformed4[length - 1];
-        return malformedCache.get(length);
+        return Cache.INSTANCE.malformed.computeIfAbsent(length,
+                n -> new CoderResult(CR_MALFORMED, n));
     }
 
-    private static final Cache unmappableCache
-        = new Cache() {
-                public CoderResult create(int len) {
-                    return new CoderResult(CR_UNMAPPABLE, len);
-                }};
-
     private static final CoderResult[] unmappable4 = new CoderResult[] {
         new CoderResult(CR_UNMAPPABLE, 1),
         new CoderResult(CR_UNMAPPABLE, 2),
@@ -270,7 +244,8 @@
             throw new IllegalArgumentException("Non-positive length");
         if (length <= 4)
             return unmappable4[length - 1];
-        return unmappableCache.get(length);
+        return Cache.INSTANCE.unmappable.computeIfAbsent(length,
+                n -> new CoderResult(CR_UNMAPPABLE, n));
     }
 
     /**