KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jmock > expectation > MapEntry


1 /* Copyright (c) 2000-2004 jMock.org
2  */

3 package org.jmock.expectation;
4
5 import java.lang.reflect.Array JavaDoc;
6 import java.util.Map JavaDoc;
7
8
9 /**
10  * A public MapEntry data type that can be used where the Map.Entry interface is required
11  * (needed because the Sun implementation is package protected)
12  */

13
14 public class MapEntry implements Map.Entry JavaDoc
15 {
16     private Object JavaDoc myKey;
17     private Object JavaDoc myValue;
18
19     public MapEntry( Object JavaDoc aKey, Object JavaDoc aValue ) {
20         myKey = (aKey == null ? new Null() : aKey);
21         myValue = (aValue == null ? new Null() : aValue);
22     }
23
24     public boolean equals( Object JavaDoc o ) {
25         if (!(o instanceof MapEntry)) {
26             return false;
27         }
28         MapEntry other = (MapEntry)o;
29
30         if (myValue.getClass().isArray() && other.getValue().getClass().isArray()) {
31             return arrayEquals(other.getValue());
32         }
33         return myKey.equals(other.getKey()) && myValue.equals(other.getValue());
34     }
35
36     private final boolean arrayEquals( Object JavaDoc anArray ) {
37         int i = 0;
38         boolean endOfThisArray = false;
39         boolean endOfAnotherArray = false;
40
41         while (true) {
42             Object JavaDoc valueOfThis = null;
43             Object JavaDoc valueOfAnother = null;
44
45             try {
46                 valueOfThis = Array.get(myValue, i);
47             }
48             catch (ArrayIndexOutOfBoundsException JavaDoc e) {
49                 endOfThisArray = true;
50             }
51
52             try {
53                 valueOfAnother = Array.get(anArray, i);
54             }
55             catch (ArrayIndexOutOfBoundsException JavaDoc e) {
56                 endOfAnotherArray = true;
57             }
58
59             if (endOfThisArray && endOfAnotherArray) {
60                 return true;
61             }
62
63             if (valueOfThis != null || valueOfAnother != null) {
64                 if (valueOfThis == null || !valueOfThis.equals(valueOfAnother)) {
65                     return false;
66                 }
67             }
68
69             i++;
70         }
71     }
72
73     public Object JavaDoc getKey() {
74         return myKey;
75     }
76
77     public Object JavaDoc getValue() {
78         return myValue;
79     }
80
81     public int hashCode() {
82         int hash = myKey.hashCode();
83
84         if (myValue.getClass().isArray()) {
85             for (int i = 0; i < Array.getLength(myValue); i++) {
86                 hash = hash ^ Array.get(myValue, i).hashCode();
87             }
88         } else {
89             hash = hash ^ myValue.hashCode();
90         }
91
92         return hash;
93     }
94
95     public Object JavaDoc setValue( Object JavaDoc aValue ) {
96         Object JavaDoc oldValue = myValue;
97         myValue = (null == aValue ? new Null() : aValue);
98         return oldValue;
99     }
100
101     public String JavaDoc toString() {
102         return myKey.toString() + "=" + myValue.toString();
103     }
104 }
105
Popular Tags