1 3 package org.jmock.expectation; 4 5 import java.lang.reflect.Array ; 6 import java.util.Map ; 7 8 9 13 14 public class MapEntry implements Map.Entry 15 { 16 private Object myKey; 17 private Object myValue; 18 19 public MapEntry( Object aKey, Object aValue ) { 20 myKey = (aKey == null ? new Null() : aKey); 21 myValue = (aValue == null ? new Null() : aValue); 22 } 23 24 public boolean equals( Object 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 anArray ) { 37 int i = 0; 38 boolean endOfThisArray = false; 39 boolean endOfAnotherArray = false; 40 41 while (true) { 42 Object valueOfThis = null; 43 Object valueOfAnother = null; 44 45 try { 46 valueOfThis = Array.get(myValue, i); 47 } 48 catch (ArrayIndexOutOfBoundsException e) { 49 endOfThisArray = true; 50 } 51 52 try { 53 valueOfAnother = Array.get(anArray, i); 54 } 55 catch (ArrayIndexOutOfBoundsException 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 getKey() { 74 return myKey; 75 } 76 77 public Object 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 setValue( Object aValue ) { 96 Object oldValue = myValue; 97 myValue = (null == aValue ? new Null() : aValue); 98 return oldValue; 99 } 100 101 public String toString() { 102 return myKey.toString() + "=" + myValue.toString(); 103 } 104 } 105 | Popular Tags |