Author: skestle
Date: Fri Aug 3 01:15:35 2007
New Revision: 562381
URL: http://svn.apache.org/viewvc?view=rev&rev=562381
Log:
Extracted private CollectionUtils.CardinalityHelper classes to allow easier
generic refactoring/maintenance and code readability.
Modified:
commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/CollectionUtils.java
Modified:
commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/CollectionUtils.java
URL:
http://svn.apache.org/viewvc/commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/CollectionUtils.java?view=diff&rev=562381&r1=562380&r2=562381
==============================================================================
---
commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/CollectionUtils.java
(original)
+++
commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/CollectionUtils.java
Fri Aug 3 01:15:35 2007
@@ -53,9 +53,71 @@
* @author Jon Schewe
* @author Neil O'Toole
* @author Stephen Smith
+ * @author Stephen Kestle
*/
public class CollectionUtils {
+ private static class CardinalityHelper {
+ final Map<?, Integer> cardinalityA, cardinalityB;
+
+ public CardinalityHelper(Collection a, Collection b) {
+ cardinalityA = getCardinalityMap(a);
+ cardinalityB = getCardinalityMap(b);
+ }
+
+ public final int max(Object obj) {
+ return Math.max(freqA(obj), freqB(obj));
+ }
+
+ public final int min(Object obj) {
+ return Math.min(freqA(obj), freqB(obj));
+ }
+
+ public int freqA(Object obj) {
+ return getFreq(obj, cardinalityA);
+ }
+
+ public int freqB(Object obj) {
+ return getFreq(obj, cardinalityB);
+ }
+
+ private final int getFreq(final Object obj, final Map<?, Integer>
freqMap) {
+ Integer count = freqMap.get(obj);
+ if (count != null) {
+ return count;
+ }
+ return 0;
+ }
+ }
+
+ private static class SetOperationCardinalityHelper extends
CardinalityHelper implements Iterable {
+ private final Set elements;
+ private final List newList;
+
+ public SetOperationCardinalityHelper(Collection a, Collection b) {
+ super(a, b);
+ elements = new HashSet(a);
+ elements.addAll(b);
+ newList = new ArrayList();
+ }
+
+ public Iterator iterator() {
+ return elements.iterator();
+ }
+
+ public void setCardinality(Object obj, int count) {
+ for (int i = 0; i < count; i++) {
+ newList.add(obj);
+ }
+ }
+
+ public Collection list() {
+ return newList;
+ }
+
+
+ }
+
/** Constant to avoid repeated object creation */
private static Integer INTEGER_ONE = new Integer(1);
@@ -87,19 +149,11 @@
* @see Collection#addAll
*/
public static Collection union(final Collection a, final Collection b) {
- ArrayList list = new ArrayList();
- Map mapa = getCardinalityMap(a);
- Map mapb = getCardinalityMap(b);
- Set elts = new HashSet(a);
- elts.addAll(b);
- Iterator it = elts.iterator();
- while(it.hasNext()) {
- Object obj = it.next();
- for(int
i=0,m=Math.max(getFreq(obj,mapa),getFreq(obj,mapb));i<m;i++) {
- list.add(obj);
- }
+ SetOperationCardinalityHelper helper = new
SetOperationCardinalityHelper(a, b);
+ for (Object obj : helper) {
+ helper.setCardinality(obj, helper.max(obj));
}
- return list;
+ return helper.list();
}
/**
@@ -117,19 +171,11 @@
* @see #containsAny
*/
public static Collection intersection(final Collection a, final Collection
b) {
- ArrayList list = new ArrayList();
- Map mapa = getCardinalityMap(a);
- Map mapb = getCardinalityMap(b);
- Set elts = new HashSet(a);
- elts.addAll(b);
- Iterator it = elts.iterator();
- while(it.hasNext()) {
- Object obj = it.next();
- for(int
i=0,m=Math.min(getFreq(obj,mapa),getFreq(obj,mapb));i<m;i++) {
- list.add(obj);
- }
+ SetOperationCardinalityHelper helper = new
SetOperationCardinalityHelper(a, b);
+ for (Object obj : helper) {
+ helper.setCardinality(obj, helper.min(obj));
}
- return list;
+ return helper.list();
}
/**
@@ -150,19 +196,11 @@
* @return the symmetric difference of the two collections
*/
public static Collection disjunction(final Collection a, final Collection
b) {
- ArrayList list = new ArrayList();
- Map mapa = getCardinalityMap(a);
- Map mapb = getCardinalityMap(b);
- Set elts = new HashSet(a);
- elts.addAll(b);
- Iterator it = elts.iterator();
- while(it.hasNext()) {
- Object obj = it.next();
- for(int
i=0,m=((Math.max(getFreq(obj,mapa),getFreq(obj,mapb)))-(Math.min(getFreq(obj,mapa),getFreq(obj,mapb))));i<m;i++)
{
- list.add(obj);
- }
+ SetOperationCardinalityHelper helper = new
SetOperationCardinalityHelper(a, b);
+ for (Object obj : helper) {
+ helper.setCardinality(obj, helper.max(obj) - helper.min(obj));
}
- return list;
+ return helper.list();
}
/**
@@ -251,12 +289,9 @@
* @see Collection#containsAll
*/
public static boolean isSubCollection(final Collection a, final Collection
b) {
- Map mapa = getCardinalityMap(a);
- Map mapb = getCardinalityMap(b);
- Iterator it = a.iterator();
- while (it.hasNext()) {
- Object obj = it.next();
- if (getFreq(obj, mapa) > getFreq(obj, mapb)) {
+ CardinalityHelper helper = new CardinalityHelper(a, b);
+ for (Object obj : a) {
+ if (helper.freqA(obj) > helper.freqB(obj)) {
return false;
}
}
@@ -303,22 +338,17 @@
public static boolean isEqualCollection(final Collection a, final
Collection b) {
if(a.size() != b.size()) {
return false;
- } else {
- Map mapa = getCardinalityMap(a);
- Map mapb = getCardinalityMap(b);
- if(mapa.size() != mapb.size()) {
+ }
+ final CardinalityHelper helper = new CardinalityHelper(a, b);
+ if(helper.cardinalityA.size() != helper.cardinalityB.size()) {
+ return false;
+ }
+ for( Object obj : helper.cardinalityA.keySet()) {
+ if(helper.freqA(obj) != helper.freqB(obj)) {
return false;
- } else {
- Iterator it = mapa.keySet().iterator();
- while(it.hasNext()) {
- Object obj = it.next();
- if(getFreq(obj,mapa) != getFreq(obj,mapb)) {
- return false;
- }
- }
- return true;
}
}
+ return true;
}
/**
@@ -902,14 +932,6 @@
j--;
i++;
}
- }
-
- private static final int getFreq(final Object obj, final Map freqMap) {
- Integer count = (Integer) freqMap.get(obj);
- if (count != null) {
- return count.intValue();
- }
- return 0;
}
/**