commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From t.@apache.org
Subject svn commit: r1682859 - in /commons/proper/collections/trunk/src: changes/changes.xml main/java/org/apache/commons/collections4/map/LRUMap.java test/java/org/apache/commons/collections4/map/LRUMapTest.java
Date Mon, 01 Jun 2015 10:12:13 GMT
Author: tn
Date: Mon Jun  1 10:12:13 2015
New Revision: 1682859

URL: http://svn.apache.org/r1682859
Log:
[COLLECTIONS-557] Added support to specify the initial size of a LRUMap. Thanks to Philippe
Mouawad.

Modified:
    commons/proper/collections/trunk/src/changes/changes.xml
    commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/map/LRUMap.java
    commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/map/LRUMapTest.java

Modified: commons/proper/collections/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/changes/changes.xml?rev=1682859&r1=1682858&r2=1682859&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/changes/changes.xml (original)
+++ commons/proper/collections/trunk/src/changes/changes.xml Mon Jun  1 10:12:13 2015
@@ -22,6 +22,9 @@
   <body>
 
   <release version="4.1" date="TBD" description="">
+    <action issue="COLLECTIONS-557" dev="tn" type="add" due-to="Philippe Mouawad">
+      Added support to specify the initial size of a "LRUMap".
+    </action>
     <action issue="COLLECTIONS-565" dev="tn" type="add">
       Added decorators for "NavigableSet" interface.
     </action>

Modified: commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/map/LRUMap.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/map/LRUMap.java?rev=1682859&r1=1682858&r2=1682859&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/map/LRUMap.java
(original)
+++ commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/map/LRUMap.java
Mon Jun  1 10:12:13 2015
@@ -91,6 +91,19 @@ public class LRUMap<K, V>
      * Constructs a new, empty map with the specified maximum size.
      *
      * @param maxSize  the maximum size of the map
+     * @param initialSize  the initial size of the map
+     * @throws IllegalArgumentException if the maximum size is less than one
+     * @throws IllegalArgumentException if the initial size is negative or larger than the
maximum size
+     * @since 4.1
+     */
+    public LRUMap(final int maxSize, final int initialSize) {
+        this(maxSize, initialSize, DEFAULT_LOAD_FACTOR);
+    }
+
+    /**
+     * Constructs a new, empty map with the specified maximum size.
+     *
+     * @param maxSize  the maximum size of the map
      * @param scanUntilRemovable  scan until a removeable entry is found, default false
      * @throws IllegalArgumentException if the maximum size is less than one
      * @since 3.1
@@ -100,10 +113,11 @@ public class LRUMap<K, V>
     }
 
     /**
-     * Constructs a new, empty map with the specified initial capacity and
+     * Constructs a new, empty map with the specified max capacity and
      * load factor.
      *
      * @param maxSize  the maximum size of the map
+     * @param initialSize  the initial size of the map
      * @param loadFactor  the load factor
      * @throws IllegalArgumentException if the maximum size is less than one
      * @throws IllegalArgumentException if the load factor is less than zero
@@ -113,21 +127,54 @@ public class LRUMap<K, V>
     }
 
     /**
-     * Constructs a new, empty map with the specified initial capacity and
+     * Constructs a new, empty map with the specified max / initial capacity and
      * load factor.
      *
      * @param maxSize  the maximum size of the map
      * @param loadFactor  the load factor
+     * @throws IllegalArgumentException if the maximum size is less than one
+     * @throws IllegalArgumentException if the initial size is negative or larger than the
maximum size
+     * @throws IllegalArgumentException if the load factor is less than zero
+     * @since 4.1
+     */
+    public LRUMap(final int maxSize, final int initialSize, final float loadFactor) {
+        this(maxSize, initialSize, loadFactor, false);
+    }
+
+    /**
+     * Constructs a new, empty map with the specified max capacity and load factor.
+     *
+     * @param maxSize  the maximum size of the map
+     * @param loadFactor  the load factor
      * @param scanUntilRemovable  scan until a removeable entry is found, default false
      * @throws IllegalArgumentException if the maximum size is less than one
      * @throws IllegalArgumentException if the load factor is less than zero
      * @since 3.1
      */
     public LRUMap(final int maxSize, final float loadFactor, final boolean scanUntilRemovable)
{
-        super(maxSize < 1 ? DEFAULT_CAPACITY : maxSize, loadFactor);
+        this(maxSize, maxSize, loadFactor, scanUntilRemovable);
+    }
+
+    /**
+     * Constructs a new, empty map with the specified max / initial capacity and load factor.
+     *
+     * @param maxSize  the maximum size of the map
+     * @param initialSize  the initial size of the map
+     * @param loadFactor  the load factor
+     * @param scanUntilRemovable  scan until a removeable entry is found, default false
+     * @throws IllegalArgumentException if the maximum size is less than one
+     * @throws IllegalArgumentException if the initial size is negative or larger than the
maximum size
+     * @throws IllegalArgumentException if the load factor is less than zero
+     * @since 4.1
+     */
+    public LRUMap(final int maxSize, final int initialSize, final float loadFactor, final
boolean scanUntilRemovable) {
+        super(initialSize, loadFactor);
         if (maxSize < 1) {
             throw new IllegalArgumentException("LRUMap max size must be greater than 0");
         }
+        if (initialSize > maxSize) {
+            throw new IllegalArgumentException("LRUMap initial size must not be greather
than max size");
+        }
         this.maxSize = maxSize;
         this.scanUntilRemovable = scanUntilRemovable;
     }

Modified: commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/map/LRUMapTest.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/map/LRUMapTest.java?rev=1682859&r1=1682858&r2=1682859&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/map/LRUMapTest.java
(original)
+++ commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/map/LRUMapTest.java
Mon Jun  1 10:12:13 2015
@@ -62,12 +62,59 @@ public class LRUMapTest<K, V> extends Ab
         return true;
     }
 
+    /**
+     * {@inheritDoc}
+     */
     @Override
-    public String getCompatibilityVersion() {
-        return "4";
+    public LRUMap<K, V> getMap() {
+        return (LRUMap<K, V>) super.getMap();
     }
 
     //-----------------------------------------------------------------------
+    public void testCtors() {
+        try {
+            new LRUMap<K, V>(0);
+            fail("maxSize must be positive");
+        } catch(IllegalArgumentException ex) {
+            // expected
+        }
+
+        try {
+            new LRUMap<K, V>(-1, 12, 0.75f, false);
+            fail("maxSize must be positive");
+        } catch(IllegalArgumentException ex) {
+            // expected
+        }
+
+        try {
+            new LRUMap<K, V>(10, -1);
+            fail("initialSize must not be negative");
+        } catch(IllegalArgumentException ex) {
+            // expected
+        }
+
+        try {
+            new LRUMap<K, V>(10, 12);
+            fail("initialSize must not be larger than maxSize");
+        } catch(IllegalArgumentException ex) {
+            // expected
+        }
+
+        try {
+            new LRUMap<K, V>(10, -1, 0.75f, false);
+            fail("initialSize must not be negative");
+        } catch(IllegalArgumentException ex) {
+            // expected
+        }
+
+        try {
+            new LRUMap<K, V>(10, 12, 0.75f, false);
+            fail("initialSize must not be larger than maxSize");
+        } catch(IllegalArgumentException ex) {
+            // expected
+        }
+    }
+
     public void testLRU() {
         if (!isPutAddSupported() || !isPutChangeSupported()) {
             return;
@@ -873,6 +920,11 @@ public class LRUMapTest<K, V> extends Ab
                 + counter[0] + " did succeed", counter[0] >= threads.length);
     }
 
+    @Override
+    public String getCompatibilityVersion() {
+        return "4";
+    }
+
 //    public void testCreate() throws Exception {
 //        resetEmpty();
 //        writeExternalFormToDisk((java.io.Serializable) map, "src/test/resources/data/test/LRUMap.emptyCollection.version4.obj");
@@ -880,11 +932,4 @@ public class LRUMapTest<K, V> extends Ab
 //        writeExternalFormToDisk((java.io.Serializable) map, "src/test/resources/data/test/LRUMap.fullCollection.version4.obj");
 //    }
 
-    /**
-     * {@inheritDoc}
-     */
-    @Override
-    public LRUMap<K, V> getMap() {
-        return (LRUMap<K, V>) super.getMap();
-    }
 }



Mime
View raw message