001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc.util.diff;
005:
006: import org.apache.commons.lang.ClassUtils;
007: import org.apache.commons.lang.builder.EqualsBuilder;
008:
009: import com.tc.util.Assert;
010:
011: /**
012: * A {@link Difference}representing two primitive fields that aren't equal.
013: */
014: public class PrimitiveDifference extends Difference {
015:
016: private final Object a;
017: private final Object b;
018:
019: public PrimitiveDifference(DifferenceContext where, boolean a,
020: boolean b) {
021: super (where);
022: Assert.eval(a != b);
023:
024: this .a = a ? Boolean.TRUE : Boolean.FALSE;
025: this .b = b ? Boolean.TRUE : Boolean.FALSE;
026: }
027:
028: public PrimitiveDifference(DifferenceContext where, byte a, byte b) {
029: super (where);
030: Assert.eval(a != b);
031:
032: this .a = new Byte(a);
033: this .b = new Byte(b);
034: }
035:
036: public PrimitiveDifference(DifferenceContext where, char a, char b) {
037: super (where);
038: Assert.eval(a != b);
039:
040: this .a = new Character(a);
041: this .b = new Character(b);
042: }
043:
044: public PrimitiveDifference(DifferenceContext where, short a, short b) {
045: super (where);
046: Assert.eval(a != b);
047:
048: this .a = new Short(a);
049: this .b = new Short(b);
050: }
051:
052: public PrimitiveDifference(DifferenceContext where, int a, int b) {
053: super (where);
054: Assert.eval(a != b);
055:
056: this .a = new Integer(a);
057: this .b = new Integer(b);
058: }
059:
060: public PrimitiveDifference(DifferenceContext where, long a, long b) {
061: super (where);
062: Assert.eval(a != b);
063:
064: this .a = new Long(a);
065: this .b = new Long(b);
066: }
067:
068: public PrimitiveDifference(DifferenceContext where, float a, float b) {
069: super (where);
070: Assert.eval(a != b);
071:
072: this .a = new Float(a);
073: this .b = new Float(b);
074: }
075:
076: public PrimitiveDifference(DifferenceContext where, double a,
077: double b) {
078: super (where);
079: Assert.eval(a != b);
080:
081: this .a = new Double(a);
082: this .b = new Double(b);
083: }
084:
085: public Object a() {
086: return this .a;
087: }
088:
089: public Object b() {
090: return this .b;
091: }
092:
093: public String toString() {
094: return where() + ": primitive fields of type "
095: + ClassUtils.getShortClassName(this .a.getClass())
096: + " differ: " + this .a + " vs. " + this .b;
097: }
098:
099: public boolean equals(Object that) {
100: if (!(that instanceof PrimitiveDifference))
101: return false;
102:
103: PrimitiveDifference primThat = (PrimitiveDifference) that;
104:
105: return new EqualsBuilder().appendSuper(super.equals(that))
106: .append(this.a, primThat.a).append(this.b, primThat.b)
107: .isEquals();
108: }
109:
110: }
|