1 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 ; 10 import java.util.BitSet ; 11 import java.util.List ; 12 13 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 [] collectionOne, Object [] collectionTwo) { 25 List mismatches = new ArrayList (); 26 27 BitSet oneUsed = new BitSet (collectionOne.length); 28 BitSet twoUsed = new BitSet (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 [] collectionOne, List mismatches, int i) { 70 mismatches.add(new MissingObjectCollectionMismatch(collectionOne[i], true, i, describer())); 71 } 72 73 protected void mismatchedNumbers(Object [] collectionOne, List mismatches, int i, int numberInOne, int numberInTwo) { 74 mismatches 75 .add(new UnequalObjectCountCollectionMismatch(collectionOne[i], i, numberInOne, numberInTwo, describer())); 76 } 77 78 } | Popular Tags |