You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2021/01/03 16:51:29 UTC

[commons-lang] branch master updated: Rename private class nad make it final; pre-compute hashcode, fix NPE compiler warning.

This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-lang.git


The following commit(s) were added to refs/heads/master by this push:
     new 3ba0107  Rename private class nad make it final; pre-compute hashcode, fix NPE compiler warning.
3ba0107 is described below

commit 3ba01073f647da53e4ee662a491be88728cb8b6c
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Jan 3 11:51:25 2021 -0500

    Rename private class nad make it final; pre-compute hashcode, fix NPE
    compiler warning.
    
    Javadoc: No need for paragraph tags for the 1st and only sentence.
    Format code to line length 120.
---
 .../org/apache/commons/lang3/time/FormatCache.java | 94 +++++++++++-----------
 1 file changed, 48 insertions(+), 46 deletions(-)

diff --git a/src/main/java/org/apache/commons/lang3/time/FormatCache.java b/src/main/java/org/apache/commons/lang3/time/FormatCache.java
index 4786cd9..ffe41e3 100644
--- a/src/main/java/org/apache/commons/lang3/time/FormatCache.java
+++ b/src/main/java/org/apache/commons/lang3/time/FormatCache.java
@@ -29,7 +29,7 @@ import org.apache.commons.lang3.LocaleUtils;
 import org.apache.commons.lang3.Validate;
 
 /**
- * <p>FormatCache is a cache and factory for {@link Format}s.</p>
+ * FormatCache is a cache and factory for {@link Format}s.
  *
  * @since 3.0
  */
@@ -41,15 +41,13 @@ abstract class FormatCache<F extends Format> {
      */
     static final int NONE = -1;
 
-    private final ConcurrentMap<MultipartKey, F> cInstanceCache
-        = new ConcurrentHashMap<>(7);
+    private final ConcurrentMap<ArrayKey, F> cInstanceCache = new ConcurrentHashMap<>(7);
 
-    private static final ConcurrentMap<MultipartKey, String> cDateTimeInstanceCache
-        = new ConcurrentHashMap<>(7);
+    private static final ConcurrentMap<ArrayKey, String> cDateTimeInstanceCache = new ConcurrentHashMap<>(7);
 
     /**
-     * <p>Gets a formatter instance using the default pattern in the
-     * default time zone and locale.</p>
+     * Gets a formatter instance using the default pattern in the
+     * default time zone and locale.
      *
      * @return a date/time formatter
      */
@@ -58,8 +56,8 @@ abstract class FormatCache<F extends Format> {
     }
 
     /**
-     * <p>Gets a formatter instance using the specified pattern, time zone
-     * and locale.</p>
+     * Gets a formatter instance using the specified pattern, time zone
+     * and locale.
      *
      * @param pattern  {@link java.text.SimpleDateFormat} compatible
      *  pattern, non-null
@@ -75,7 +73,7 @@ abstract class FormatCache<F extends Format> {
             timeZone = TimeZone.getDefault();
         }
         locale = LocaleUtils.toLocale(locale);
-        final MultipartKey key = new MultipartKey(pattern, timeZone, locale);
+        final ArrayKey key = new ArrayKey(pattern, timeZone, locale);
         F format = cInstanceCache.get(key);
         if (format == null) {
             format = createInstance(pattern, timeZone, locale);
@@ -90,8 +88,8 @@ abstract class FormatCache<F extends Format> {
     }
 
     /**
-     * <p>Create a format instance using the specified pattern, time zone
-     * and locale.</p>
+     * Create a format instance using the specified pattern, time zone
+     * and locale.
      *
      * @param pattern  {@link java.text.SimpleDateFormat} compatible pattern, this will not be null.
      * @param timeZone  time zone, this will not be null.
@@ -103,8 +101,8 @@ abstract class FormatCache<F extends Format> {
     protected abstract F createInstance(String pattern, TimeZone timeZone, Locale locale);
 
     /**
-     * <p>Gets a date/time formatter instance using the specified style,
-     * time zone and locale.</p>
+     * Gets a date/time formatter instance using the specified style,
+     * time zone and locale.
      *
      * @param dateStyle  date style: FULL, LONG, MEDIUM, or SHORT, null indicates no date in format
      * @param timeStyle  time style: FULL, LONG, MEDIUM, or SHORT, null indicates no time in format
@@ -123,8 +121,8 @@ abstract class FormatCache<F extends Format> {
     }
 
     /**
-     * <p>Gets a date/time formatter instance using the specified style,
-     * time zone and locale.</p>
+     * Gets a date/time formatter instance using the specified style,
+     * time zone and locale.
      *
      * @param dateStyle  date style: FULL, LONG, MEDIUM, or SHORT
      * @param timeStyle  time style: FULL, LONG, MEDIUM, or SHORT
@@ -141,8 +139,8 @@ abstract class FormatCache<F extends Format> {
     }
 
     /**
-     * <p>Gets a date formatter instance using the specified style,
-     * time zone and locale.</p>
+     * Gets a date formatter instance using the specified style,
+     * time zone and locale.
      *
      * @param dateStyle  date style: FULL, LONG, MEDIUM, or SHORT
      * @param timeZone  optional time zone, overrides time zone of
@@ -158,8 +156,8 @@ abstract class FormatCache<F extends Format> {
     }
 
     /**
-     * <p>Gets a time formatter instance using the specified style,
-     * time zone and locale.</p>
+     * Gets a time formatter instance using the specified style,
+     * time zone and locale.
      *
      * @param timeStyle  time style: FULL, LONG, MEDIUM, or SHORT
      * @param timeZone  optional time zone, overrides time zone of
@@ -175,7 +173,7 @@ abstract class FormatCache<F extends Format> {
     }
 
     /**
-     * <p>Gets a date/time format for the specified styles and locale.</p>
+     * Gets a date/time format for the specified styles and locale.
      *
      * @param dateStyle  date style: FULL, LONG, MEDIUM, or SHORT, null indicates no date in format
      * @param timeStyle  time style: FULL, LONG, MEDIUM, or SHORT, null indicates no time in format
@@ -186,7 +184,7 @@ abstract class FormatCache<F extends Format> {
     // package protected, for access from test code; do not make public or protected
     static String getPatternForStyle(final Integer dateStyle, final Integer timeStyle, final Locale locale) {
         final Locale safeLocale = LocaleUtils.toLocale(locale);
-        final MultipartKey key = new MultipartKey(dateStyle, timeStyle, safeLocale);
+        final ArrayKey key = new ArrayKey(dateStyle, timeStyle, safeLocale);
 
         String pattern = cDateTimeInstanceCache.get(key);
         if (pattern == null) {
@@ -215,47 +213,51 @@ abstract class FormatCache<F extends Format> {
     }
 
     /**
-     * <p>Helper class to hold multi-part Map keys</p>
+     * Helper class to hold multi-part Map keys as arrays.
      */
-    private static class MultipartKey {
+    private static final class ArrayKey {
+
+        private static int computeHashCode(Object[] keys) {
+            final int prime = 31;
+            int result = 1;
+            result = prime * result + Arrays.hashCode(keys);
+            return result;
+        }
+
         private final Object[] keys;
         private int hashCode;
 
         /**
          * Constructs an instance of {@code MultipartKey} to hold the specified objects.
+         *
          * @param keys the set of objects that make up the key.  Each key may be null.
          */
-        MultipartKey(final Object... keys) {
+        ArrayKey(final Object... keys) {
             this.keys = keys;
+            this.hashCode = computeHashCode(keys);
         }
 
-        /**
-         * {@inheritDoc}
-         */
         @Override
-        public boolean equals(final Object obj) {
-            // Eliminate the usual boilerplate because
-            // this inner static class is only used in a generic ConcurrentHashMap
-            // which will not compare against other Object types
-            return Arrays.equals(keys, ((MultipartKey) obj).keys);
+        public int hashCode() {
+            return hashCode;
         }
 
-        /**
-         * {@inheritDoc}
-         */
         @Override
-        public int hashCode() {
-            if (hashCode == 0) {
-                int rc = 0;
-                for (final Object key : keys) {
-                    if (key != null) {
-                        rc = rc * 7 + key.hashCode();
-                    }
-                }
-                hashCode = rc;
+        public boolean equals(Object obj) {
+            if (this == obj) {
+                return true;
             }
-            return hashCode;
+            if (obj == null) {
+                return false;
+            }
+            if (getClass() != obj.getClass()) {
+                return false;
+            }
+            ArrayKey other = (ArrayKey) obj;
+            return Arrays.deepEquals(keys, other.keys);
         }
+
+
     }
 
 }