Author: ggregory
Date: Thu Aug  4 17:32:17 2016
New Revision: 1755219

URL: http://svn.apache.org/viewvc?rev=1755219&view=rev
Log:
[COLLECTIONS-586] PatriciaTrie prefixMap clear throws NullPointerException. 
Applied patch and added an extra test.

Modified:
    commons/proper/collections/trunk/src/changes/changes.xml
    
commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/trie/AbstractPatriciaTrie.java
    
commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/trie/PatriciaTrieTest.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=1755219&r1=1755218&r2=1755219&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/changes/changes.xml (original)
+++ commons/proper/collections/trunk/src/changes/changes.xml Thu Aug  4 
17:32:17 2016
@@ -24,6 +24,9 @@
     <action issue="COLLECTIONS-589" dev="ggregory" type="add" due-to="Gary 
Gregory">
       Add null-safe MapUtils.size(Map&lt;?, ?>) method. 
     </action>
+    <action issue="COLLECTIONS-586" dev="ggregory" type="add" 
due-to="Shailender Bathula, Gary Gregory">
+      PatriciaTrie prefixMap clear throws NullPointerException. 
+    </action>
   </release>
   <release version="4.1" date="2015-11-28" description="This is a security and 
minor release.">
     <action issue="COLLECTIONS-508" dev="tn" type="add">

Modified: 
commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/trie/AbstractPatriciaTrie.java
URL: 
http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/trie/AbstractPatriciaTrie.java?rev=1755219&r1=1755218&r2=1755219&view=diff
==============================================================================
--- 
commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/trie/AbstractPatriciaTrie.java
 (original)
+++ 
commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/trie/AbstractPatriciaTrie.java
 Thu Aug  4 17:32:17 2016
@@ -2258,6 +2258,17 @@ abstract class AbstractPatriciaTrie<K, V
                                                  final K toKey, final boolean 
toInclusive) {
             return new RangeEntryMap(fromKey, fromInclusive, toKey, 
toInclusive);
         }
+
+        @Override
+        public void clear() {
+            Iterator<Map.Entry<K, V>> it = 
AbstractPatriciaTrie.this.entrySet().iterator();
+            Set<K> currentKeys = keySet();
+            while (it.hasNext()) {
+                if (currentKeys.contains(it.next().getKey())) {
+                    it.remove();
+                }
+            }
+        }
     }
 
     /**

Modified: 
commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/trie/PatriciaTrieTest.java
URL: 
http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/trie/PatriciaTrieTest.java?rev=1755219&r1=1755218&r2=1755219&view=diff
==============================================================================
--- 
commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/trie/PatriciaTrieTest.java
 (original)
+++ 
commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/trie/PatriciaTrieTest.java
 Thu Aug  4 17:32:17 2016
@@ -16,15 +16,20 @@
  */
 package org.apache.commons.collections4.trie;
 
+import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.ConcurrentModificationException;
+import java.util.HashSet;
 import java.util.Iterator;
 import java.util.Map;
 import java.util.NoSuchElementException;
+import java.util.Set;
 import java.util.SortedMap;
 
 import junit.framework.Test;
 
 import org.apache.commons.collections4.BulkTest;
+import org.apache.commons.collections4.Trie;
 import org.apache.commons.collections4.map.AbstractSortedMapTest;
 import org.junit.Assert;
 
@@ -365,6 +370,64 @@ public class PatriciaTrieTest<V> extends
         assertTrue(trie.prefixMap(prefixString).containsKey(longerString));
     }
 
+    public void testPrefixMapClear() {
+        Trie<String, Integer> trie = new PatriciaTrie<Integer>();
+        trie.put("Anna", 1);
+        trie.put("Anael", 2);
+        trie.put("Analu", 3);
+        trie.put("Andreas", 4);
+        trie.put("Andrea", 5);
+        trie.put("Andres", 6);
+        trie.put("Anatole", 7);
+        SortedMap<String, Integer> prefixMap = trie.prefixMap("And");
+        assertEquals(new HashSet<String>(Arrays.asList("Andrea", "Andreas", 
"Andres")), prefixMap.keySet());
+        assertEquals(Arrays.asList(5, 4, 6), new 
ArrayList<Integer>(prefixMap.values()));
+
+        prefixMap.clear();
+        assertTrue(prefixMap.isEmpty());
+        assertTrue(prefixMap.keySet().isEmpty());
+        assertTrue(prefixMap.values().isEmpty());
+        assertEquals(new HashSet<String>(Arrays.asList("Anael", "Analu", 
"Anatole", "Anna")), trie.keySet());
+        assertEquals(Arrays.asList(2, 3, 7, 1), new 
ArrayList<Integer>(trie.values()));
+    }
+
+    public void testPrefixMapClearNothing() {
+        Trie<String, Integer> trie = new PatriciaTrie<Integer>();
+        SortedMap<String, Integer> prefixMap = trie.prefixMap("And");
+        assertEquals(new HashSet<String>(), prefixMap.keySet());
+        assertEquals(new ArrayList<Integer>(0), new 
ArrayList<Integer>(prefixMap.values()));
+
+        prefixMap.clear();
+        assertTrue(prefixMap.isEmpty());
+        assertTrue(prefixMap.keySet().isEmpty());
+        assertTrue(prefixMap.values().isEmpty());
+        assertEquals(new HashSet<String>(), trie.keySet());
+        assertEquals(new ArrayList<Integer>(0), new 
ArrayList<Integer>(trie.values()));
+    }
+
+    public void testPrefixMapClearUsingRemove() {
+        Trie<String, Integer> trie = new PatriciaTrie<Integer>();
+        trie.put("Anna", 1);
+        trie.put("Anael", 2);
+        trie.put("Analu", 3);
+        trie.put("Andreas", 4);
+        trie.put("Andrea", 5);
+        trie.put("Andres", 6);
+        trie.put("Anatole", 7);
+        SortedMap<String, Integer> prefixMap = trie.prefixMap("And");
+        assertEquals(new HashSet<String>(Arrays.asList("Andrea", "Andreas", 
"Andres")), prefixMap.keySet());
+        assertEquals(Arrays.asList(5, 4, 6), new 
ArrayList<Integer>(prefixMap.values()));
+
+        Set<String> keys = new HashSet<String>(prefixMap.keySet());
+        for (final String key : keys) {
+            prefixMap.remove(key);
+        }
+        assertTrue(prefixMap.keySet().isEmpty());
+        assertTrue(prefixMap.values().isEmpty());
+        assertEquals(new HashSet<String>(Arrays.asList("Anael", "Analu", 
"Anatole", "Anna")), trie.keySet());
+        assertEquals(Arrays.asList(2, 3, 7, 1), new 
ArrayList<Integer>(trie.values()));
+    }
+
     //-----------------------------------------------------------------------
 
     @Override


Reply via email to