01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */
04: package com.tc.util.diff;
05:
06: import org.apache.commons.lang.builder.EqualsBuilder;
07:
08: import com.tc.util.Assert;
09:
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 a;
17: private final Object b;
18:
19: public BasicObjectDifference(DifferenceContext where, Object a,
20: Object b) {
21: super (where);
22:
23: Assert
24: .eval((a != null && b != null && ((a instanceof Differenceable)
25: && (b instanceof Differenceable) && (!a
26: .getClass().equals(b))))
27: || (a == null)
28: || (b == null)
29: || ((!(a instanceof Differenceable)) || (!(b instanceof Differenceable))));
30:
31: Assert.eval(!(a == null && b == null));
32: Assert.eval((a == null) || (b == null)
33: || (!a.getClass().equals(b.getClass()))
34: || (!(a.equals(b))));
35:
36: this .a = a;
37: this .b = b;
38: }
39:
40: public Object a() {
41: return this .a;
42: }
43:
44: public Object b() {
45: return this .b;
46: }
47:
48: public String toString() {
49: return where() + ": object fields differ: " + describe(a)
50: + " vs. " + describe(b);
51: }
52:
53: private String describe(Object o) {
54: return where().describe(o);
55: }
56:
57: public boolean equals(Object that) {
58: if (!(that instanceof BasicObjectDifference))
59: return false;
60:
61: BasicObjectDifference basicThat = (BasicObjectDifference) that;
62:
63: return new EqualsBuilder().appendSuper(super.equals(that))
64: .append(this.a, basicThat.a)
65: .append(this.b, basicThat.b).isEquals();
66: }
67:
68: }
|