01: /*
02: * Copyright 2006-2007, Unitils.org
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package org.unitils.reflectionassert;
17:
18: import java.util.Date;
19: import java.util.Map;
20: import java.util.Stack;
21:
22: /**
23: * todo javadoc
24: *
25: * @author Tim Ducheyne
26: * @author Filip Neven
27: */
28: public class SimpleCasesComparator extends ReflectionComparator {
29:
30: // todo javadoc
31: public SimpleCasesComparator(ReflectionComparator chainedComparator) {
32: super (chainedComparator);
33: }
34:
35: // todo javadoc
36: public boolean canHandle(Object left, Object right) {
37: return left == right
38: || left == null
39: || right == null
40: || left.getClass().getName().startsWith("java.lang")
41: || (left instanceof Enum)
42: && (right instanceof Enum)
43: || (left instanceof Date)
44: && (right instanceof Date)
45: || ((left instanceof Character || left instanceof Number) && (right instanceof Character || right instanceof Number));
46: }
47:
48: // todo javadoc
49: @Override
50: protected Difference doGetDifference(Object left, Object right,
51: Stack<String> fieldStack,
52: Map<TraversedInstancePair, Boolean> traversedInstancePairs) {
53: // check if the same instance is referenced
54: if (left == right) {
55: return null;
56: }
57: // check if the left value is null
58: if (left == null) {
59: return new Difference("Left value null.", left, right,
60: fieldStack);
61: }
62: // check if the right value is null
63: if (right == null) {
64: return new Difference("Right value null.", left, right,
65: fieldStack);
66: }
67: // check if right and left have same number value (including NaN and Infinity)
68: if ((left instanceof Character || left instanceof Number)
69: && (right instanceof Character || right instanceof Number)) {
70: Double leftDouble = getDoubleValue(left);
71: Double rightDouble = getDoubleValue(right);
72: if (leftDouble.equals(rightDouble)) {
73: return null;
74: }
75: return new Difference("Different primitive values.", left,
76: right, fieldStack);
77: }
78: // check if objects are equal
79: if (!left.equals(right)) {
80: return new Difference("Different object values.", left,
81: right, fieldStack);
82: }
83: return null;
84: }
85:
86: /**
87: * Gets the double value for the given left Character or Number instance.
88: *
89: * @param object the Character or Number, not null
90: * @return the value as a Double (this way NaN and infinity can be compared)
91: */
92: private Double getDoubleValue(Object object) {
93: if (object instanceof Number) {
94: return ((Number) object).doubleValue();
95: }
96: return (double) ((Character) object).charValue();
97: }
98:
99: }
|