KickJava   Java API By Example, From Geeks To Geeks.

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


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.ClassUtils;
7 import org.apache.commons.lang.builder.EqualsBuilder;
8
9 import com.tc.util.Assert;
10
11 /**
12  * A {@link Difference}representing two primitive fields that aren't equal.
13  */

14 public class PrimitiveDifference extends Difference {
15
16   private final Object JavaDoc a;
17   private final Object JavaDoc b;
18
19   public PrimitiveDifference(DifferenceContext where, boolean a, boolean b) {
20     super(where);
21     Assert.eval(a != b);
22
23     this.a = a ? Boolean.TRUE : Boolean.FALSE;
24     this.b = b ? Boolean.TRUE : Boolean.FALSE;
25   }
26
27   public PrimitiveDifference(DifferenceContext where, byte a, byte b) {
28     super(where);
29     Assert.eval(a != b);
30
31     this.a = new Byte JavaDoc(a);
32     this.b = new Byte JavaDoc(b);
33   }
34
35   public PrimitiveDifference(DifferenceContext where, char a, char b) {
36     super(where);
37     Assert.eval(a != b);
38
39     this.a = new Character JavaDoc(a);
40     this.b = new Character JavaDoc(b);
41   }
42
43   public PrimitiveDifference(DifferenceContext where, short a, short b) {
44     super(where);
45     Assert.eval(a != b);
46
47     this.a = new Short JavaDoc(a);
48     this.b = new Short JavaDoc(b);
49   }
50
51   public PrimitiveDifference(DifferenceContext where, int a, int b) {
52     super(where);
53     Assert.eval(a != b);
54
55     this.a = new Integer JavaDoc(a);
56     this.b = new Integer JavaDoc(b);
57   }
58
59   public PrimitiveDifference(DifferenceContext where, long a, long b) {
60     super(where);
61     Assert.eval(a != b);
62
63     this.a = new Long JavaDoc(a);
64     this.b = new Long JavaDoc(b);
65   }
66
67   public PrimitiveDifference(DifferenceContext where, float a, float b) {
68     super(where);
69     Assert.eval(a != b);
70
71     this.a = new Float JavaDoc(a);
72     this.b = new Float JavaDoc(b);
73   }
74
75   public PrimitiveDifference(DifferenceContext where, double a, double b) {
76     super(where);
77     Assert.eval(a != b);
78
79     this.a = new Double JavaDoc(a);
80     this.b = new Double JavaDoc(b);
81   }
82
83   public Object JavaDoc a() {
84     return this.a;
85   }
86
87   public Object JavaDoc b() {
88     return this.b;
89   }
90
91   public String JavaDoc toString() {
92     return where() + ": primitive fields of type " + ClassUtils.getShortClassName(this.a.getClass()) + " differ: "
93            + this.a + " vs. " + this.b;
94   }
95
96   public boolean equals(Object JavaDoc that) {
97     if (!(that instanceof PrimitiveDifference)) return false;
98
99     PrimitiveDifference primThat = (PrimitiveDifference) that;
100
101     return new EqualsBuilder().appendSuper(super.equals(that)).append(this.a, primThat.a).append(this.b, primThat.b)
102         .isEquals();
103   }
104
105 }
Popular Tags