From: Jeff M. <cus...@us...> - 2002-09-05 08:39:27
|
Update of /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects In directory usw-pr-cvs1:/tmp/cvs-serv1044/src/core/com/mockobjects Modified Files: MapEntry.java Log Message: Fixed MapEntry's equality behaviour when working with arrays Index: MapEntry.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/MapEntry.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- MapEntry.java 29 Jul 2001 19:50:24 -0000 1.1 +++ MapEntry.java 5 Sep 2002 08:39:22 -0000 1.2 @@ -1,9 +1,10 @@ package com.mockobjects; -import java.util.*; - import com.mockobjects.util.Null; +import java.lang.reflect.Array; +import java.util.Map; + /** * A public MapEntry data type that can be used where the Map.Entry interface is required * (needed because the Sun implementation is package protected) @@ -24,7 +25,47 @@ return false; } MapEntry other = (MapEntry) o; - return myKey.equals(other.getKey()) && myValue.equals(other.getValue()); + + if (myValue.getClass().isArray() && other.getValue().getClass().isArray()) { + return arrayEquals(other.getValue()); + } else { + return myKey.equals(other.getKey()) && myValue.equals(other.getValue()); + } + } + + private final boolean arrayEquals(Object anArray) { + int i = 0; + boolean endOfThisArray = false; + boolean endOfAnotherArray = false; + + while (true) { + Object valueOfThis = null; + Object valueOfAnother = null; + + try { + valueOfThis = Array.get(myValue, i); + } catch (ArrayIndexOutOfBoundsException e) { + endOfThisArray = true; + } + + try { + valueOfAnother = Array.get(anArray, i); + } catch (ArrayIndexOutOfBoundsException e) { + endOfAnotherArray = true; + } + + if (endOfThisArray && endOfAnotherArray) { + return true; + } + + if (valueOfThis != null || valueOfAnother != null) { + if (valueOfThis == null || !valueOfThis.equals(valueOfAnother)) { + return false; + } + } + + i++; + } } public Object getKey() { @@ -36,7 +77,20 @@ } public int hashCode() { - return myKey.hashCode() ^ myValue.hashCode(); + int hash = myKey.hashCode(); + if (myValue.getClass().isArray()) { + int i = 0; + + try { + while (true) { + hash = hash ^ Array.get(myValue, i++).hashCode(); + } + } catch (ArrayIndexOutOfBoundsException e) { + } + } else { + hash = hash ^ myValue.hashCode(); + } + return hash; } public Object setValue(Object aValue) { |