1 /* Copyright (c) 2000-2003, jMock.org. See bundled LICENSE.txt */
2 package org.jmock.expectation;
3
4 import java.lang.reflect.Array;
5 import java.util.Map;
6
7 /***
8 * A public MapEntry data type that can be used where the Map.Entry interface is required
9 * (needed because the Sun implementation is package protected)
10 */
11
12 public class MapEntry implements Map.Entry {
13 private Object myKey;
14 private Object myValue;
15
16 public MapEntry(Object aKey, Object aValue) {
17 myKey = (aKey == null ? new Null() : aKey);
18 myValue = (aValue == null ? new Null() : aValue);
19 }
20
21 public boolean equals(Object o) {
22 if (!(o instanceof MapEntry)) {
23 return false;
24 }
25 MapEntry other = (MapEntry) o;
26
27 if (myValue.getClass().isArray() && other.getValue().getClass().isArray()) {
28 return arrayEquals(other.getValue());
29 } else {
30 return myKey.equals(other.getKey()) && myValue.equals(other.getValue());
31 }
32 }
33
34 private final boolean arrayEquals(Object anArray) {
35 int i = 0;
36 boolean endOfThisArray = false;
37 boolean endOfAnotherArray = false;
38
39 while (true) {
40 Object valueOfThis = null;
41 Object valueOfAnother = null;
42
43 try {
44 valueOfThis = Array.get(myValue, i);
45 } catch (ArrayIndexOutOfBoundsException e) {
46 endOfThisArray = true;
47 }
48
49 try {
50 valueOfAnother = Array.get(anArray, i);
51 } catch (ArrayIndexOutOfBoundsException e) {
52 endOfAnotherArray = true;
53 }
54
55 if (endOfThisArray && endOfAnotherArray) {
56 return true;
57 }
58
59 if (valueOfThis != null || valueOfAnother != null) {
60 if (valueOfThis == null || !valueOfThis.equals(valueOfAnother)) {
61 return false;
62 }
63 }
64
65 i++;
66 }
67 }
68
69 public Object getKey() {
70 return myKey;
71 }
72
73 public Object getValue() {
74 return myValue;
75 }
76
77 public int hashCode() {
78 int hash = myKey.hashCode();
79 if (myValue.getClass().isArray()) {
80 int i = 0;
81
82 try {
83 while (true) {
84 hash = hash ^ Array.get(myValue, i++).hashCode();
85 }
86 } catch (ArrayIndexOutOfBoundsException e) {
87 }
88 } else {
89 hash = hash ^ myValue.hashCode();
90 }
91 return hash;
92 }
93
94 public Object setValue(Object aValue) {
95 Object oldValue = myValue;
96 myValue = (null == aValue ? new Null() : aValue);
97 return oldValue;
98 }
99
100 public String toString() {
101 return myKey.toString() + "=" + myValue.toString();
102 }
103 }
This page was automatically generated by Maven