sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1394573 - /sis/branches/JDK6/sis-utility/src/main/java/org/apache/sis/util/collection/WeakEntry.java
Date Fri, 05 Oct 2012 15:12:07 GMT
Author: desruisseaux
Date: Fri Oct  5 15:12:06 2012
New Revision: 1394573

URL: http://svn.apache.org/viewvc?rev=1394573&view=rev
Log:
Leverage the MathFunctions.nextPrimeNumber(int) added in the previous commit, for reducing
the risk of key collisions.

Modified:
    sis/branches/JDK6/sis-utility/src/main/java/org/apache/sis/util/collection/WeakEntry.java

Modified: sis/branches/JDK6/sis-utility/src/main/java/org/apache/sis/util/collection/WeakEntry.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK6/sis-utility/src/main/java/org/apache/sis/util/collection/WeakEntry.java?rev=1394573&r1=1394572&r2=1394573&view=diff
==============================================================================
--- sis/branches/JDK6/sis-utility/src/main/java/org/apache/sis/util/collection/WeakEntry.java
(original)
+++ sis/branches/JDK6/sis-utility/src/main/java/org/apache/sis/util/collection/WeakEntry.java
Fri Oct  5 15:12:06 2012
@@ -25,6 +25,7 @@ import java.lang.reflect.Array;
 import org.apache.sis.util.Disposable;
 import org.apache.sis.util.resources.Messages;
 import org.apache.sis.internal.util.ReferenceQueueConsumer;
+import org.apache.sis.math.MathFunctions;
 
 
 /**
@@ -42,6 +43,7 @@ import org.apache.sis.internal.util.Refe
 abstract class WeakEntry<E> extends WeakReference<E> implements Disposable {
     /**
      * Minimal capacity for the internal table of entries.
+     * Must be a prime number.
      */
     static final int MIN_CAPACITY = 7;
 
@@ -133,10 +135,22 @@ abstract class WeakEntry<E> extends Weak
      * @return The new table array, or {@code oldTable} if no rehash were needed.
      */
     static <E> WeakEntry<E>[] rehash(final WeakEntry<E>[] oldTable, final
int count, final String callerMethod) {
-        final int capacity = Math.max(count*2, MIN_CAPACITY);
+        /*
+         * Compute the capacity as twice the expected number of elements, then take
+         * (if possible) the first prime number equals or greater to that value.
+         * This is based on classical books saying that prime values reduce the
+         * risk of key collisions.
+         */
+        int capacity = Math.max(count*2, MIN_CAPACITY);
+        if (capacity < MathFunctions.HIGHEST_SUPPORTED_PRIME_NUMBER) {
+            capacity = MathFunctions.nextPrimeNumber(capacity);
+        }
         if (capacity == oldTable.length) {
             return oldTable;
         }
+        /*
+         * Rehash the table.
+         */
         final Class<?> entryType = oldTable.getClass().getComponentType();
         @SuppressWarnings("unchecked")
         final WeakEntry<E>[] table = (WeakEntry<E>[]) Array.newInstance(entryType,
capacity);
@@ -149,6 +163,9 @@ abstract class WeakEntry<E> extends Weak
                 table[index] = e;
             }
         }
+        /*
+         * We are done. Log the operation if logging is enabled at that level.
+         */
         final Logger logger = Collections.LOGGER;
         if (logger.isLoggable(Level.FINEST)) {
             final LogRecord record = Messages.getResources(null).getLogRecord(Level.FINEST,



Mime
View raw message