[COLLECTIONS-586] PatriciaTrie prefixMap clear throws NullPointerException. Applied patch and added an extra test.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/collections/trunk@1755219 13f79535-47bb-0310-9956-ffa450edef68 Project: http://git-wip-us.apache.org/repos/asf/commons-collections/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-collections/commit/a270ff62 Tree: http://git-wip-us.apache.org/repos/asf/commons-collections/tree/a270ff62 Diff: http://git-wip-us.apache.org/repos/asf/commons-collections/diff/a270ff62 Branch: refs/heads/master Commit: a270ff62852e62b5ac0f943a7e57292a72b77271 Parents: 796114e Author: Gary D. Gregory <[email protected]> Authored: Thu Aug 4 17:32:17 2016 +0000 Committer: Gary D. Gregory <[email protected]> Committed: Thu Aug 4 17:32:17 2016 +0000 ---------------------------------------------------------------------- src/changes/changes.xml | 3 + .../collections4/trie/AbstractPatriciaTrie.java | 11 ++++ .../collections4/trie/PatriciaTrieTest.java | 63 ++++++++++++++++++++ 3 files changed, 77 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-collections/blob/a270ff62/src/changes/changes.xml ---------------------------------------------------------------------- diff --git a/src/changes/changes.xml b/src/changes/changes.xml index a3d12e3..3997c3c 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -24,6 +24,9 @@ <action issue="COLLECTIONS-589" dev="ggregory" type="add" due-to="Gary Gregory"> Add null-safe MapUtils.size(Map<?, ?>) 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"> http://git-wip-us.apache.org/repos/asf/commons-collections/blob/a270ff62/src/main/java/org/apache/commons/collections4/trie/AbstractPatriciaTrie.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/collections4/trie/AbstractPatriciaTrie.java b/src/main/java/org/apache/commons/collections4/trie/AbstractPatriciaTrie.java index be779ea..bd11f75 100644 --- a/src/main/java/org/apache/commons/collections4/trie/AbstractPatriciaTrie.java +++ b/src/main/java/org/apache/commons/collections4/trie/AbstractPatriciaTrie.java @@ -2258,6 +2258,17 @@ abstract class AbstractPatriciaTrie<K, V> extends AbstractBitwiseTrie<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(); + } + } + } } /** http://git-wip-us.apache.org/repos/asf/commons-collections/blob/a270ff62/src/test/java/org/apache/commons/collections4/trie/PatriciaTrieTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/collections4/trie/PatriciaTrieTest.java b/src/test/java/org/apache/commons/collections4/trie/PatriciaTrieTest.java index 5a6ba81..92559fb 100755 --- a/src/test/java/org/apache/commons/collections4/trie/PatriciaTrieTest.java +++ b/src/test/java/org/apache/commons/collections4/trie/PatriciaTrieTest.java @@ -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 AbstractSortedMapTest<String, V> { 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
