You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by tn...@apache.org on 2015/05/25 21:18:51 UTC
svn commit: r1681635 - in /commons/proper/collections/trunk/src:
changes/changes.xml main/java/org/apache/commons/collections4/SetUtils.java
test/java/org/apache/commons/collections4/SetUtilsTest.java
Author: tn
Date: Mon May 25 19:18:50 2015
New Revision: 1681635
URL: http://svn.apache.org/r1681635
Log:
[COLLECTIONS-556] Added method SetUtils.identityHashSet.
Modified:
commons/proper/collections/trunk/src/changes/changes.xml
commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/SetUtils.java
commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/SetUtilsTest.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=1681635&r1=1681634&r2=1681635&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/changes/changes.xml (original)
+++ commons/proper/collections/trunk/src/changes/changes.xml Mon May 25 19:18:50 2015
@@ -22,6 +22,10 @@
<body>
<release version="4.1" date="TBD" description="">
+ <action issue="COLLECTIONS-556" dev="tn" type="add">
+ Added method "SetUtils#identityHashSet()" which returns a new identity HashSet
+ using reference-equality instead of object-equality.
+ </action>
<action issue="COLLECTIONS-562" dev="tn" type="update">
Upgraded minimum java requirement to Java 6 (up from Java 5).
</action>
Modified: commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/SetUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/SetUtils.java?rev=1681635&r1=1681634&r2=1681635&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/SetUtils.java (original)
+++ commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/SetUtils.java Mon May 25 19:18:50 2015
@@ -18,6 +18,7 @@ package org.apache.commons.collections4;
import java.util.Collection;
import java.util.Collections;
+import java.util.IdentityHashMap;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
@@ -151,6 +152,28 @@ public class SetUtils {
return hashCode;
}
+ /**
+ * Returns a new hash set that matches elements based on <code>==</code> not
+ * <code>equals()</code>.
+ * <p>
+ * <strong>This set will violate the detail of various Set contracts.</note>
+ * As a general rule, don't compare this set to other sets. In particular, you can't
+ * use decorators like {@link ListOrderedSet} on it, which silently assume that these
+ * contracts are fulfilled.</strong>
+ * <p>
+ * <strong>Note that the returned set is not synchronized and is not thread-safe.</strong>
+ * If you wish to use this set from multiple threads concurrently, you must use
+ * appropriate synchronization. The simplest approach is to wrap this map
+ * using {@link java.util.Collections#synchronizedSet(Set)}. This class may throw
+ * exceptions when accessed by concurrent threads without synchronization.
+ *
+ * @return a new identity hash set
+ * @since 4.1
+ */
+ public static <E> Set<E> identityHashSet() {
+ return Collections.newSetFromMap(new IdentityHashMap<E, Boolean>());
+ }
+
//-----------------------------------------------------------------------
/**
* Returns a synchronized set backed by the given set.
@@ -255,7 +278,7 @@ public class SetUtils {
* avoid non-deterministic behavior:
*
* <pre>
- * Set s = SetUtils.synchronizedSet(mySet);
+ * Set s = SetUtils.synchronizedSortedSet(mySet);
* synchronized (s) {
* Iterator i = s.iterator();
* while (i.hasNext()) {
Modified: commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/SetUtilsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/SetUtilsTest.java?rev=1681635&r1=1681634&r2=1681635&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/SetUtilsTest.java (original)
+++ commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/SetUtilsTest.java Mon May 25 19:18:50 2015
@@ -102,4 +102,19 @@ public class SetUtilsTest extends BulkTe
assertEquals(0, SetUtils.hashCodeForSet(null));
}
+ public void testIdentityHashSet() {
+ Set<String> set = SetUtils.identityHashSet();
+ String a = new String("a");
+ set.add(a);
+ set.add(new String("b"));
+ set.add(a);
+
+ assertEquals(2, set.size());
+
+ set.add(new String("a"));
+ assertEquals(3, set.size());
+
+ set.remove(a);
+ assertEquals(2, set.size());
+ }
}