src/java.base/share/classes/java/nio/charset/CoderResult.java

Print this page

        

@@ -23,12 +23,12 @@
  * questions.
  */
 
 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;
 
 /**
  * A description of the result state of a coder.

@@ -188,38 +188,17 @@
      * room in the output buffer.
      */
     public static final CoderResult OVERFLOW
         = new CoderResult(CR_OVERFLOW, 0);
 
-    private abstract static class Cache {
+    private static final class Cache {
+        static final Cache INSTANCE = new Cache();
+        private Cache() {}
 
-        private Map<Integer,WeakReference<CoderResult>> cache = null;
-
-        protected abstract CoderResult create(int len);
-
-        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),
         new CoderResult(CR_MALFORMED, 3),

@@ -238,19 +217,14 @@
     public static CoderResult malformedForLength(int length) {
         if (length <= 0)
             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),
         new CoderResult(CR_UNMAPPABLE, 3),
         new CoderResult(CR_UNMAPPABLE, 4),

@@ -268,11 +242,12 @@
     public static CoderResult unmappableForLength(int length) {
         if (length <= 0)
             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));
     }
 
     /**
      * Throws an exception appropriate to the result described by this object.
      *