KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > util > diff > BasicObjectDifference


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.util.diff;
5
6 import org.apache.commons.lang.builder.EqualsBuilder;
7
8 import com.tc.util.Assert;
9
10 /**
11  * A {@link Difference}that represents two object references that aren't equal. (These references cannot be
12  * {@link Differenceable}s themselves, because otherwise we'd just look for <em>their</em> differences,
13  */

14 public class BasicObjectDifference extends Difference {
15
16   private final Object JavaDoc a;
17   private final Object JavaDoc b;
18
19   public BasicObjectDifference(DifferenceContext where, Object JavaDoc a, Object JavaDoc b) {
20     super(where);
21
22     Assert
23         .eval((a != null && b != null && ((a instanceof Differenceable) && (b instanceof Differenceable) && (!a
24             .getClass().equals(b))))
25               || (a == null) || (b == null) || ((!(a instanceof Differenceable)) || (!(b instanceof Differenceable))));
26
27     Assert.eval(!(a == null && b == null));
28     Assert.eval((a == null) || (b == null) || (! a.getClass().equals(b.getClass())) || (!(a.equals(b))));
29
30     this.a = a;
31     this.b = b;
32   }
33
34   public Object JavaDoc a() {
35     return this.a;
36   }
37
38   public Object JavaDoc b() {
39     return this.b;
40   }
41
42   public String JavaDoc toString() {
43     return where() + ": object fields differ: " + describe(a) + " vs. " + describe(b);
44   }
45
46   private String JavaDoc describe(Object JavaDoc o) {
47     return where().describe(o);
48   }
49
50   public boolean equals(Object JavaDoc that) {
51     if (!(that instanceof BasicObjectDifference)) return false;
52
53     BasicObjectDifference basicThat = (BasicObjectDifference) that;
54
55     return new EqualsBuilder().appendSuper(super.equals(that)).append(this.a, basicThat.a).append(this.b, basicThat.b)
56         .isEquals();
57   }
58
59 }
Popular Tags