[COLLECTIONS-495] Fix UnmodifiableTrie.unmodifiableTrie, add tests.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/collections/trunk@1540763 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Thomas Neidhart 2013-11-11 16:30:14 +00:00
parent 15ee56bd0f
commit 72784c46f6
6 changed files with 31 additions and 12 deletions

View File

@ -47,6 +47,8 @@ Major changes since 3.2.1
Changes since 4.0-alpha1 Changes since 4.0-alpha1
------------------------ ------------------------
o [COLLECTIONS-495] "UnmodifiableTrie#unmodifiableTrie(Trie)" will not decorate again an already unmodifiable Trie.
Also the return type has been changed to "Trie" to be consistent with other Unmodifiable decorators.
o [COLLECTIONS-494] Moved "Equator" interface to base package for consistency. Thanks to Emmanuel Bourg. o [COLLECTIONS-494] Moved "Equator" interface to base package for consistency. Thanks to Emmanuel Bourg.
o [COLLECTIONS-488] Added "CollectionsUtils#matchesAll(Iterable, Predicate)" to test if all elements o [COLLECTIONS-488] Added "CollectionsUtils#matchesAll(Iterable, Predicate)" to test if all elements
of a collection match a given predicate. Thanks to Josh Cain. of a collection match a given predicate. Thanks to Josh Cain.
@ -159,6 +161,8 @@ New features
Changed classes / methods Changed classes / methods
------------------------- -------------------------
o [COLLECTIONS-495] "UnmodifiableTrie#unmodifiableTrie(Trie)" will not decorate again an already unmodifiable Trie.
Also the return type has been changed to "Trie" to be consistent with other Unmodifiable decorators.
o [COLLECTIONS-494] Moved "Equator" interface to base package for consistency. Thanks to Emmanuel Bourg. o [COLLECTIONS-494] Moved "Equator" interface to base package for consistency. Thanks to Emmanuel Bourg.
o [COLLECTIONS-485] Accept wildcard input where possible, e.g. in copy-constructors, Unmodifiable* decorators o [COLLECTIONS-485] Accept wildcard input where possible, e.g. in copy-constructors, Unmodifiable* decorators
and iterators. Thanks to Hollis Waite. and iterators. Thanks to Hollis Waite.

View File

@ -38,6 +38,11 @@ Commons Collections is Java 5.
Users are encouraged to upgrade to this version as, in addition to new Users are encouraged to upgrade to this version as, in addition to new
features, this release includes numerous bug fixes. features, this release includes numerous bug fixes.
"> ">
<action issue="COLLECTIONS-495" dev="tn" type="fix">
"UnmodifiableTrie#unmodifiableTrie(Trie)" will not decorate again an already
unmodifiable Trie. Also the return type has been changed to "Trie" to be consistent
with other Unmodifiable decorators.
</action>
<action issue="COLLECTIONS-494" dev="tn" type="update" due-to="Emmanuel Bourg"> <action issue="COLLECTIONS-494" dev="tn" type="update" due-to="Emmanuel Bourg">
Moved "Equator" interface to base package for consistency. Moved "Equator" interface to base package for consistency.
</action> </action>

View File

@ -51,7 +51,12 @@ public class UnmodifiableTrie<K, V> implements Trie<K, V>, Serializable, Unmodif
* @return a new unmodifiable trie * @return a new unmodifiable trie
* @throws IllegalArgumentException if trie is null * @throws IllegalArgumentException if trie is null
*/ */
public static <K, V> UnmodifiableTrie<K, V> unmodifiableTrie(final Trie<K, ? extends V> trie) { public static <K, V> Trie<K, V> unmodifiableTrie(final Trie<K, ? extends V> trie) {
if (trie instanceof Unmodifiable) {
@SuppressWarnings("unchecked") // safe to upcast
final Trie<K, V> tmpTrie = (Trie<K, V>) trie;
return tmpTrie;
}
return new UnmodifiableTrie<K, V>(trie); return new UnmodifiableTrie<K, V>(trie);
} }

View File

@ -155,6 +155,8 @@ have changed.
<center><h3>Changed classes / methods</h3></center> <center><h3>Changed classes / methods</h3></center>
<ul> <ul>
<li>"UnmodifiableTrie#unmodifiableTrie(Trie)" will not decorate again an already unmodifiable Trie.
Also the return type has been changed to "Trie" to be consistent with other Unmodifiable decorators.</li>
<li>Moved "Equator" interface to base package for consistency.</li> <li>Moved "Equator" interface to base package for consistency.</li>
<li>Accept wildcard input where possible, e.g. in copy-constructors, Unmodifiable* decorators and iterators.</li> <li>Accept wildcard input where possible, e.g. in copy-constructors, Unmodifiable* decorators and iterators.</li>
<li>Narrow return type of "BidiMap#values()" to Set as the values are required to be unique.</li> <li>Narrow return type of "BidiMap#values()" to Set as the values are required to be unique.</li>

View File

@ -49,6 +49,8 @@ public class TrieUtilsTest extends BulkTest {
} catch (final IllegalArgumentException ex) { } catch (final IllegalArgumentException ex) {
// expected // expected
} }
assertSame("UnmodifiableTrie shall not be decorated", trie, TrieUtils.unmodifiableTrie(trie));
} }
} }

View File

@ -16,14 +16,12 @@
*/ */
package org.apache.commons.collections4.trie; package org.apache.commons.collections4.trie;
import java.util.SortedMap;
import junit.framework.Test; import junit.framework.Test;
import org.apache.commons.collections4.BulkTest; import org.apache.commons.collections4.BulkTest;
import org.apache.commons.collections4.Trie;
import org.apache.commons.collections4.Unmodifiable; import org.apache.commons.collections4.Unmodifiable;
import org.apache.commons.collections4.map.AbstractSortedMapTest; import org.apache.commons.collections4.map.AbstractSortedMapTest;
import org.apache.commons.collections4.map.UnmodifiableSortedMap;
/** /**
* Extension of {@link AbstractSortedMapTest} for exercising the * Extension of {@link AbstractSortedMapTest} for exercising the
@ -45,8 +43,8 @@ public class UnmodifiableTrieTest<V> extends AbstractSortedMapTest<String, V> {
//------------------------------------------------------------------- //-------------------------------------------------------------------
@Override @Override
public SortedMap<String, V> makeObject() { public Trie<String, V> makeObject() {
return UnmodifiableSortedMap.unmodifiableSortedMap(new PatriciaTrie<V>()); return UnmodifiableTrie.unmodifiableTrie(new PatriciaTrie<V>());
} }
@Override @Override
@ -65,28 +63,31 @@ public class UnmodifiableTrieTest<V> extends AbstractSortedMapTest<String, V> {
} }
@Override @Override
public SortedMap<String, V> makeFullMap() { public Trie<String, V> makeFullMap() {
final SortedMap<String, V> m = new PatriciaTrie<V>(); final Trie<String, V> m = new PatriciaTrie<V>();
addSampleMappings(m); addSampleMappings(m);
return UnmodifiableSortedMap.unmodifiableSortedMap(m); return UnmodifiableTrie.unmodifiableTrie(m);
} }
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
public void testUnmodifiable() { public void testUnmodifiable() {
assertTrue(makeObject() instanceof Unmodifiable); assertTrue(makeObject() instanceof Unmodifiable);
assertTrue(makeFullMap() instanceof Unmodifiable); assertTrue(makeFullMap() instanceof Unmodifiable);
} }
public void testDecorateFactory() { public void testDecorateFactory() {
final SortedMap<String, V> map = makeFullMap(); final Trie<String, V> trie = makeFullMap();
assertSame(map, UnmodifiableSortedMap.unmodifiableSortedMap(map)); assertSame(trie, UnmodifiableTrie.unmodifiableTrie(trie));
try { try {
UnmodifiableSortedMap.unmodifiableSortedMap(null); UnmodifiableTrie.unmodifiableTrie(null);
fail(); fail();
} catch (final IllegalArgumentException ex) {} } catch (final IllegalArgumentException ex) {}
} }
//-----------------------------------------------------------------------
@Override @Override
public String getCompatibilityVersion() { public String getCompatibilityVersion() {
return "4"; return "4";