Rename SetUtils.identityHashSet to SetUtils.newIdentityHashSet to better reflect that it returns a new instance.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/collections/trunk@1682416 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Thomas Neidhart 2015-05-29 10:59:39 +00:00
parent 1a3337262c
commit 6ed868d4d0
3 changed files with 68 additions and 4 deletions

View File

@ -34,7 +34,7 @@
Added new decorator "SkippingIterator" and factory methods "IteratorUtils#skippingIterator(...)". Added new decorator "SkippingIterator" and factory methods "IteratorUtils#skippingIterator(...)".
</action> </action>
<action issue="COLLECTIONS-556" dev="tn" type="add"> <action issue="COLLECTIONS-556" dev="tn" type="add">
Added method "SetUtils#identityHashSet()" which returns a new identity HashSet Added method "SetUtils#newIdentityHashSet()" which returns a new identity HashSet
using reference-equality instead of object-equality. using reference-equality instead of object-equality.
</action> </action>
<action issue="COLLECTIONS-562" dev="tn" type="update"> <action issue="COLLECTIONS-562" dev="tn" type="update">

View File

@ -19,15 +19,19 @@ package org.apache.commons.collections4;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.IdentityHashMap; import java.util.IdentityHashMap;
import java.util.NavigableSet;
import java.util.Set; import java.util.Set;
import java.util.SortedSet; import java.util.SortedSet;
import java.util.TreeSet; import java.util.TreeSet;
import org.apache.commons.collections4.set.ListOrderedSet; import org.apache.commons.collections4.set.ListOrderedSet;
import org.apache.commons.collections4.set.PredicatedNavigableSet;
import org.apache.commons.collections4.set.PredicatedSet; import org.apache.commons.collections4.set.PredicatedSet;
import org.apache.commons.collections4.set.PredicatedSortedSet; import org.apache.commons.collections4.set.PredicatedSortedSet;
import org.apache.commons.collections4.set.TransformedNavigableSet;
import org.apache.commons.collections4.set.TransformedSet; import org.apache.commons.collections4.set.TransformedSet;
import org.apache.commons.collections4.set.TransformedSortedSet; import org.apache.commons.collections4.set.TransformedSortedSet;
import org.apache.commons.collections4.set.UnmodifiableNavigableSet;
import org.apache.commons.collections4.set.UnmodifiableSet; import org.apache.commons.collections4.set.UnmodifiableSet;
import org.apache.commons.collections4.set.UnmodifiableSortedSet; import org.apache.commons.collections4.set.UnmodifiableSortedSet;
@ -171,10 +175,11 @@ public class SetUtils {
* @return a new identity hash set * @return a new identity hash set
* @since 4.1 * @since 4.1
*/ */
public static <E> Set<E> identityHashSet() { public static <E> Set<E> newIdentityHashSet() {
return Collections.newSetFromMap(new IdentityHashMap<E, Boolean>()); return Collections.newSetFromMap(new IdentityHashMap<E, Boolean>());
} }
// Set
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
/** /**
* Returns a synchronized set backed by the given set. * Returns a synchronized set backed by the given set.
@ -271,6 +276,7 @@ public class SetUtils {
return ListOrderedSet.listOrderedSet(set); return ListOrderedSet.listOrderedSet(set);
} }
// SortedSet
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
/** /**
* Returns a synchronized sorted set backed by the given sorted set. * Returns a synchronized sorted set backed by the given sorted set.
@ -352,4 +358,62 @@ public class SetUtils {
return TransformedSortedSet.transformingSortedSet(set, transformer); return TransformedSortedSet.transformingSortedSet(set, transformer);
} }
// NavigableSet
//-----------------------------------------------------------------------
/**
* Returns an unmodifiable navigable set backed by the given navigable set.
* <p>
* This method uses the implementation in the decorators subpackage.
*
* @param <E> the element type
* @param set the navigable set to make unmodifiable, must not be null
* @return an unmodifiable set backed by the given set
* @throws IllegalArgumentException if the set is null
* @since 4.1
*/
public static <E> SortedSet<E> unmodifiableNavigableSet(final NavigableSet<E> set) {
return UnmodifiableNavigableSet.unmodifiableNavigableSet(set);
}
/**
* Returns a predicated (validating) navigable set backed by the given navigable set.
* <p>
* Only objects that pass the test in the given predicate can be added to the set.
* Trying to add an invalid object results in an IllegalArgumentException.
* It is important not to use the original set after invoking this method,
* as it is a backdoor for adding invalid objects.
*
* @param <E> the element type
* @param set the navigable set to predicate, must not be null
* @param predicate the predicate for the navigable set, must not be null
* @return a predicated navigable set backed by the given navigable set
* @throws IllegalArgumentException if the Set or Predicate is null
* @since 4.1
*/
public static <E> SortedSet<E> predicatedNavigableSet(final NavigableSet<E> set, final Predicate<? super E> predicate) {
return PredicatedNavigableSet.predicatedNavigableSet(set, predicate);
}
/**
* Returns a transformed navigable set backed by the given navigable set.
* <p>
* Each object is passed through the transformer as it is added to the
* Set. It is important not to use the original set after invoking this
* method, as it is a backdoor for adding untransformed objects.
* <p>
* Existing entries in the specified set will not be transformed.
* If you want that behaviour, see {@link TransformedNavigableSet#transformedNavigableSet}.
*
* @param <E> the element type
* @param set the navigable set to transform, must not be null
* @param transformer the transformer for the set, must not be null
* @return a transformed set backed by the given set
* @throws IllegalArgumentException if the Set or Transformer is null
* @since 4.1
*/
public static <E> SortedSet<E> transformedNavigableSet(final NavigableSet<E> set,
final Transformer<? super E, ? extends E> transformer) {
return TransformedNavigableSet.transformingNavigableSet(set, transformer);
}
} }

View File

@ -102,8 +102,8 @@ public class SetUtilsTest extends BulkTest {
assertEquals(0, SetUtils.hashCodeForSet(null)); assertEquals(0, SetUtils.hashCodeForSet(null));
} }
public void testIdentityHashSet() { public void testNewIdentityHashSet() {
Set<String> set = SetUtils.identityHashSet(); Set<String> set = SetUtils.newIdentityHashSet();
String a = new String("a"); String a = new String("a");
set.add(a); set.add(a);
set.add(new String("b")); set.add(new String("b"));