[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
This commit is contained in:
Gary D. Gregory 2016-08-04 17:32:17 +00:00
parent 796114ea4a
commit a270ff6285
3 changed files with 77 additions and 0 deletions

View File

@ -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">

View File

@ -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();
}
}
}
}
/**

View File

@ -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