KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.commons.lang.builder.EqualsBuilder;
7
8 import com.tc.util.Assert;
9 import com.tc.util.Stringifier;
10
11 /**
12  * Represents a single mismatch among collections.
13  */

14 public abstract class CollectionMismatch {
15   private final Object JavaDoc originating;
16   private final Object JavaDoc comparedAgainst;
17   private final boolean originatingIsInCollectionOne;
18
19   private final int originatingIndex;
20   private final int comparedAgainstIndex;
21
22   private final Stringifier describer;
23
24   protected CollectionMismatch(Object JavaDoc originating, Object JavaDoc comparedAgainst, boolean originatingIsInCollectionOne,
25                                int originatingIndex, int comparedAgainstIndex, Stringifier describer) {
26     Assert.eval(originatingIndex >= 0);
27     Assert.assertNotNull(describer);
28
29     this.originating = originating;
30     this.comparedAgainst = comparedAgainst;
31     this.originatingIsInCollectionOne = originatingIsInCollectionOne;
32
33     this.originatingIndex = originatingIndex;
34     this.comparedAgainstIndex = comparedAgainstIndex;
35
36     this.describer = describer;
37   }
38
39   protected final Object JavaDoc originating() {
40     return this.originating;
41   }
42
43   protected final Object JavaDoc comparedAgainst() {
44     return this.comparedAgainst;
45   }
46   
47   protected final Stringifier describer() {
48     return this.describer;
49   }
50
51   public boolean originatingIsInCollectionOne() {
52     return this.originatingIsInCollectionOne;
53   }
54
55   protected final String JavaDoc originatingCollection() {
56     return "collection " + (this.originatingIsInCollectionOne ? "one" : "two");
57   }
58
59   protected final String JavaDoc originatingString() {
60     return "(" + originatingCollection() + ", index " + this.originatingIndex + "): " + describeOriginating();
61   }
62
63   protected final String JavaDoc describeOriginating() {
64     return this.describer.toString(this.originating);
65   }
66
67   protected final String JavaDoc comparedAgainstCollection() {
68     return "collection " + (this.originatingIsInCollectionOne ? "two" : "one");
69   }
70
71   protected final String JavaDoc comparedAgainstString() {
72     return "(" + comparedAgainstCollection() + ", index " + this.comparedAgainstIndex + "): "
73            + describeComparedAgainst();
74   }
75
76   protected final String JavaDoc describeComparedAgainst() {
77     return this.describer.toString(this.comparedAgainst);
78   }
79
80   public abstract String JavaDoc toString();
81
82   public boolean equals(Object JavaDoc that) {
83     if (!(that instanceof CollectionMismatch)) return false;
84
85     CollectionMismatch misThat = (CollectionMismatch) that;
86
87     return new EqualsBuilder().append(this.originating, misThat.originating).append(this.comparedAgainst,
88                                                                                     misThat.comparedAgainst)
89         .append(this.originatingIsInCollectionOne, misThat.originatingIsInCollectionOne)
90         .append(this.originatingIndex, misThat.originatingIndex).append(this.comparedAgainstIndex,
91                                                                         misThat.comparedAgainstIndex).isEquals();
92   }
93 }
Popular Tags