KickJava   Java API By Example, From Geeks To Geeks.

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


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.List JavaDoc;
11
12 /**
13  * A {@link CollectionComparer}that requires the collections to be in the same order.
14  */

15 public class OrderedCollectionComparer extends CollectionComparer {
16
17   public OrderedCollectionComparer(EqualityComparator comparator, Stringifier describer) {
18     super(comparator, describer);
19   }
20
21   protected CollectionMismatch[] doComparison(Object JavaDoc[] collectionOne, Object JavaDoc[] collectionTwo) {
22     List JavaDoc mismatches = new ArrayList JavaDoc();
23
24     for (int i = 0; i < collectionOne.length; ++i) {
25       Object JavaDoc objectOne = collectionOne[i];
26
27       if (i >= collectionTwo.length) {
28         mismatches.add(new MissingObjectCollectionMismatch(objectOne, true, i, describer()));
29       } else {
30         Object JavaDoc objectTwo = collectionTwo[i];
31         boolean isEqual = isEqual(objectOne, true, objectTwo, true, i, i);
32
33         if (!isEqual) {
34           mismatches.add(new UnequalObjectCollectionMismatch(objectOne, objectTwo, true, i, i, describer()));
35         }
36       }
37     }
38
39     for (int i = collectionOne.length; i < collectionTwo.length; ++i) {
40       mismatches.add(new MissingObjectCollectionMismatch(collectionTwo[i], false, i, describer()));
41     }
42
43     return (CollectionMismatch[]) mismatches.toArray(new CollectionMismatch[mismatches.size()]);
44   }
45
46 }
Popular Tags