KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > test > collections > UnorderedCollectionComparer


1 /**
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.test.collections;
5
6 import com.tc.util.EqualityComparator;
7 import com.tc.util.Stringifier;
8
9 import java.util.ArrayList JavaDoc;
10 import java.util.BitSet JavaDoc;
11 import java.util.List JavaDoc;
12
13 /**
14  * A {@link CollectionComparer}that doesn't require objects to be in the same order. However, if multiple instances of
15  * a single object are present in at least one of the collections, then both collections must have the same number of
16  * instances of that object.
17  */

18 public class UnorderedCollectionComparer extends CollectionComparer {
19
20   public UnorderedCollectionComparer(EqualityComparator comparator, Stringifier describer) {
21     super(comparator, describer);
22   }
23
24   protected CollectionMismatch[] doComparison(Object JavaDoc[] collectionOne, Object JavaDoc[] collectionTwo) {
25     List JavaDoc mismatches = new ArrayList JavaDoc();
26
27     BitSet JavaDoc oneUsed = new BitSet JavaDoc(collectionOne.length);
28     BitSet JavaDoc twoUsed = new BitSet JavaDoc(collectionTwo.length);
29
30     for (int i = 0; i < collectionOne.length; ++i) {
31       if (!oneUsed.get(i)) {
32         int numberInOne = 1;
33
34         for (int j = i + 1; j < collectionOne.length; ++j) {
35           if ((!oneUsed.get(j)) && isEqual(collectionOne[i], true, collectionOne[j], true, i, j)) {
36             ++numberInOne;
37             oneUsed.set(j);
38           }
39         }
40
41         int numberInTwo = 0;
42
43         for (int j = 0; j < collectionTwo.length; ++j) {
44           if ((!twoUsed.get(j)) && isEqual(collectionOne[i], true, collectionTwo[j], false, i, j)) {
45             ++numberInTwo;
46             twoUsed.set(j);
47           }
48         }
49
50         if (numberInOne != numberInTwo) {
51           if (numberInTwo > 0) {
52             mismatchedNumbers(collectionOne, mismatches, i, numberInOne, numberInTwo);
53           } else {
54             missingObject(collectionOne, mismatches, i);
55           }
56         }
57       }
58     }
59
60     for (int i = 0; i < collectionTwo.length; ++i) {
61       if (!twoUsed.get(i)) {
62         mismatches.add(new MissingObjectCollectionMismatch(collectionTwo[i], false, i, describer()));
63       }
64     }
65
66     return (CollectionMismatch[]) mismatches.toArray(new CollectionMismatch[mismatches.size()]);
67   }
68
69   private void missingObject(Object JavaDoc[] collectionOne, List JavaDoc mismatches, int i) {
70     mismatches.add(new MissingObjectCollectionMismatch(collectionOne[i], true, i, describer()));
71   }
72
73   protected void mismatchedNumbers(Object JavaDoc[] collectionOne, List JavaDoc mismatches, int i, int numberInOne, int numberInTwo) {
74     mismatches
75         .add(new UnequalObjectCountCollectionMismatch(collectionOne[i], i, numberInOne, numberInTwo, describer()));
76   }
77
78 }
Popular Tags