From cb19f2d73fb6ff6cbad5fa568534d1d05a5d47ff Mon Sep 17 00:00:00 2001 From: Jochen Wiedmann Date: Tue, 8 Mar 2011 23:37:01 +0000 Subject: [PATCH] PR: COLLECTIONS-323 Submitted-By: Maarten Brak Allow to use an initial capacity of 0 on certain maps. git-svn-id: https://svn.apache.org/repos/asf/commons/proper/collections/trunk@1079602 13f79535-47bb-0310-9956-ffa450edef68 --- pom.xml | 5 ++++- .../commons/collections/map/AbstractHashedMap.java | 10 +++++----- .../commons/collections/map/AbstractLinkedMap.java | 4 ++-- .../commons/collections/map/CaseInsensitiveMap.java | 4 ++-- .../org/apache/commons/collections/map/HashedMap.java | 4 ++-- .../apache/commons/collections/map/IdentityMap.java | 4 ++-- .../org/apache/commons/collections/map/LinkedMap.java | 4 ++-- .../collections/map/TestCaseInsensitiveMap.java | 7 +++++++ .../apache/commons/collections/map/TestHashedMap.java | 8 ++++++++ .../commons/collections/map/TestIdentityMap.java | 8 ++++++++ .../apache/commons/collections/map/TestLinkedMap.java | 8 ++++++++ 11 files changed, 50 insertions(+), 16 deletions(-) diff --git a/pom.xml b/pom.xml index 2edbcb455..2f87cca8c 100644 --- a/pom.xml +++ b/pom.xml @@ -129,11 +129,14 @@ Janek Bogucki - Chuck Burdick + Maarten Brak Dave Bryson + + Chuck Burdick + Julien Buret diff --git a/src/java/org/apache/commons/collections/map/AbstractHashedMap.java b/src/java/org/apache/commons/collections/map/AbstractHashedMap.java index 51da6425a..0f10a79dc 100644 --- a/src/java/org/apache/commons/collections/map/AbstractHashedMap.java +++ b/src/java/org/apache/commons/collections/map/AbstractHashedMap.java @@ -124,7 +124,7 @@ public class AbstractHashedMap extends AbstractMap implements Iterab * default load factor. * * @param initialCapacity the initial capacity - * @throws IllegalArgumentException if the initial capacity is less than one + * @throws IllegalArgumentException if the initial capacity is negative */ protected AbstractHashedMap(int initialCapacity) { this(initialCapacity, DEFAULT_LOAD_FACTOR); @@ -136,14 +136,14 @@ public class AbstractHashedMap extends AbstractMap implements Iterab * * @param initialCapacity the initial capacity * @param loadFactor the load factor - * @throws IllegalArgumentException if the initial capacity is less than one - * @throws IllegalArgumentException if the load factor is less than or equal to zero + * @throws IlleagalArgumentException if the initial capacity is negative + * @throws IllegalArgumentException if the load factor is less than or equal to zero */ @SuppressWarnings("unchecked") protected AbstractHashedMap(int initialCapacity, float loadFactor) { super(); - if (initialCapacity < 1) { - throw new IllegalArgumentException("Initial capacity must be greater than 0"); + if (initialCapacity < 0) { + throw new IllegalArgumentException("Initial capacity must be a non negative number"); } if (loadFactor <= 0.0f || Float.isNaN(loadFactor)) { throw new IllegalArgumentException("Load factor must be greater than 0"); diff --git a/src/java/org/apache/commons/collections/map/AbstractLinkedMap.java b/src/java/org/apache/commons/collections/map/AbstractLinkedMap.java index c27906bfc..b928e06ea 100644 --- a/src/java/org/apache/commons/collections/map/AbstractLinkedMap.java +++ b/src/java/org/apache/commons/collections/map/AbstractLinkedMap.java @@ -89,7 +89,7 @@ public abstract class AbstractLinkedMap extends AbstractHashedMap im * Constructs a new, empty map with the specified initial capacity. * * @param initialCapacity the initial capacity - * @throws IllegalArgumentException if the initial capacity is less than one + * @throws IllegalArgumentException if the initial capacity is negative */ protected AbstractLinkedMap(int initialCapacity) { super(initialCapacity); @@ -101,7 +101,7 @@ public abstract class AbstractLinkedMap extends AbstractHashedMap im * * @param initialCapacity the initial capacity * @param loadFactor the load factor - * @throws IllegalArgumentException if the initial capacity is less than one + * @throws IllegalArgumentException if the initial capacity is negative * @throws IllegalArgumentException if the load factor is less than zero */ protected AbstractLinkedMap(int initialCapacity, float loadFactor) { diff --git a/src/java/org/apache/commons/collections/map/CaseInsensitiveMap.java b/src/java/org/apache/commons/collections/map/CaseInsensitiveMap.java index fdb10350d..ab0eaab1f 100644 --- a/src/java/org/apache/commons/collections/map/CaseInsensitiveMap.java +++ b/src/java/org/apache/commons/collections/map/CaseInsensitiveMap.java @@ -78,7 +78,7 @@ public class CaseInsensitiveMap extends AbstractHashedMap implements * Constructs a new, empty map with the specified initial capacity. * * @param initialCapacity the initial capacity - * @throws IllegalArgumentException if the initial capacity is less than one + * @throws IllegalArgumentException if the initial capacity is negative */ public CaseInsensitiveMap(int initialCapacity) { super(initialCapacity); @@ -90,7 +90,7 @@ public class CaseInsensitiveMap extends AbstractHashedMap implements * * @param initialCapacity the initial capacity * @param loadFactor the load factor - * @throws IllegalArgumentException if the initial capacity is less than one + * @throws IllegalArgumentException if the initial capacity is negative * @throws IllegalArgumentException if the load factor is less than zero */ public CaseInsensitiveMap(int initialCapacity, float loadFactor) { diff --git a/src/java/org/apache/commons/collections/map/HashedMap.java b/src/java/org/apache/commons/collections/map/HashedMap.java index a966fd730..56baa6893 100644 --- a/src/java/org/apache/commons/collections/map/HashedMap.java +++ b/src/java/org/apache/commons/collections/map/HashedMap.java @@ -58,7 +58,7 @@ public class HashedMap * Constructs a new, empty map with the specified initial capacity. * * @param initialCapacity the initial capacity - * @throws IllegalArgumentException if the initial capacity is less than one + * @throws IllegalArgumentException if the initial capacity is negative */ public HashedMap(int initialCapacity) { super(initialCapacity); @@ -70,7 +70,7 @@ public class HashedMap * * @param initialCapacity the initial capacity * @param loadFactor the load factor - * @throws IllegalArgumentException if the initial capacity is less than one + * @throws IllegalArgumentException if the initial capacity is negative * @throws IllegalArgumentException if the load factor is less than zero */ public HashedMap(int initialCapacity, float loadFactor) { diff --git a/src/java/org/apache/commons/collections/map/IdentityMap.java b/src/java/org/apache/commons/collections/map/IdentityMap.java index c2be2e255..cd908f882 100644 --- a/src/java/org/apache/commons/collections/map/IdentityMap.java +++ b/src/java/org/apache/commons/collections/map/IdentityMap.java @@ -60,7 +60,7 @@ public class IdentityMap * Constructs a new, empty map with the specified initial capacity. * * @param initialCapacity the initial capacity - * @throws IllegalArgumentException if the initial capacity is less than one + * @throws IllegalArgumentException if the initial capacity is negative */ public IdentityMap(int initialCapacity) { super(initialCapacity); @@ -72,7 +72,7 @@ public class IdentityMap * * @param initialCapacity the initial capacity * @param loadFactor the load factor - * @throws IllegalArgumentException if the initial capacity is less than one + * @throws IllegalArgumentException if the initial capacity is negative * @throws IllegalArgumentException if the load factor is less than zero */ public IdentityMap(int initialCapacity, float loadFactor) { diff --git a/src/java/org/apache/commons/collections/map/LinkedMap.java b/src/java/org/apache/commons/collections/map/LinkedMap.java index 981c037b1..c1ce5aca2 100644 --- a/src/java/org/apache/commons/collections/map/LinkedMap.java +++ b/src/java/org/apache/commons/collections/map/LinkedMap.java @@ -78,7 +78,7 @@ public class LinkedMap extends AbstractLinkedMap implements Serializ * Constructs a new, empty map with the specified initial capacity. * * @param initialCapacity the initial capacity - * @throws IllegalArgumentException if the initial capacity is less than one + * @throws IllegalArgumentException if the initial capacity is negative */ public LinkedMap(int initialCapacity) { super(initialCapacity); @@ -90,7 +90,7 @@ public class LinkedMap extends AbstractLinkedMap implements Serializ * * @param initialCapacity the initial capacity * @param loadFactor the load factor - * @throws IllegalArgumentException if the initial capacity is less than one + * @throws IllegalArgumentException if the initial capacity is negative * @throws IllegalArgumentException if the load factor is less than zero */ public LinkedMap(int initialCapacity, float loadFactor) { diff --git a/src/test/org/apache/commons/collections/map/TestCaseInsensitiveMap.java b/src/test/org/apache/commons/collections/map/TestCaseInsensitiveMap.java index 1e9bf4714..d02662ac6 100644 --- a/src/test/org/apache/commons/collections/map/TestCaseInsensitiveMap.java +++ b/src/test/org/apache/commons/collections/map/TestCaseInsensitiveMap.java @@ -146,4 +146,11 @@ public class TestCaseInsensitiveMap extends AbstractTestIterableMap } } + /** + * Test for COLLECTIONS-323. + */ + public void testInitialCapacityZero() { + final CaseInsensitiveMap map = new CaseInsensitiveMap(0); + assertEquals(1, map.data.length); + } } diff --git a/src/test/org/apache/commons/collections/map/TestHashedMap.java b/src/test/org/apache/commons/collections/map/TestHashedMap.java index 890daea2a..0427a4841 100644 --- a/src/test/org/apache/commons/collections/map/TestHashedMap.java +++ b/src/test/org/apache/commons/collections/map/TestHashedMap.java @@ -70,4 +70,12 @@ public class TestHashedMap extends AbstractTestIterableMap { // resetFull(); // writeExternalFormToDisk((java.io.Serializable) map, "D:/dev/collections/data/test/HashedMap.fullCollection.version3.obj"); // } + + /** + * Test for COLLECTIONS-323. + */ + public void testInitialCapacityZero() { + final HashedMap map = new HashedMap(0); + assertEquals(1, map.data.length); + } } diff --git a/src/test/org/apache/commons/collections/map/TestIdentityMap.java b/src/test/org/apache/commons/collections/map/TestIdentityMap.java index 58a1e9e51..95a023726 100644 --- a/src/test/org/apache/commons/collections/map/TestIdentityMap.java +++ b/src/test/org/apache/commons/collections/map/TestIdentityMap.java @@ -143,4 +143,12 @@ public class TestIdentityMap extends AbstractTestObject { // map.put(I2A, I2B); // writeExternalFormToDisk((java.io.Serializable) map, "D:/dev/collections/data/test/IdentityMap.fullCollection.version3.obj"); // } + + /** + * Test for COLLECTIONS-323. + */ + public void testInitialCapacityZero() { + final IdentityMap map = new IdentityMap(0); + assertEquals(1, map.data.length); + } } diff --git a/src/test/org/apache/commons/collections/map/TestLinkedMap.java b/src/test/org/apache/commons/collections/map/TestLinkedMap.java index 2c25bdc1a..922365d95 100644 --- a/src/test/org/apache/commons/collections/map/TestLinkedMap.java +++ b/src/test/org/apache/commons/collections/map/TestLinkedMap.java @@ -295,4 +295,12 @@ public class TestLinkedMap extends AbstractTestOrderedMap { public LinkedMap getMap() { return (LinkedMap) super.getMap(); } + + /** + * Test for COLLECTIONS-323. + */ + public void testInitialCapacityZero() { + final LinkedMap map = new LinkedMap(0); + assertEquals(1, map.data.length); + } }