diff --git a/src/main/java/org/elasticsearch/common/bytes/ByteBufferBytesReference.java b/src/main/java/org/elasticsearch/common/bytes/ByteBufferBytesReference.java index 2739be08522..eb403438bad 100644 --- a/src/main/java/org/elasticsearch/common/bytes/ByteBufferBytesReference.java +++ b/src/main/java/org/elasticsearch/common/bytes/ByteBufferBytesReference.java @@ -125,6 +125,16 @@ public class ByteBufferBytesReference implements BytesReference { return buffer.arrayOffset() + buffer.position(); } + @Override + public int hashCode() { + return Helper.bytesHashCode(this); + } + + @Override + public boolean equals(Object obj) { + return Helper.bytesEqual(this, (BytesReference) obj); + } + @Override public String toUtf8() { if (!buffer.hasRemaining()) { diff --git a/src/main/java/org/elasticsearch/common/bytes/BytesArray.java b/src/main/java/org/elasticsearch/common/bytes/BytesArray.java index a274c70e03f..86f6edea530 100644 --- a/src/main/java/org/elasticsearch/common/bytes/BytesArray.java +++ b/src/main/java/org/elasticsearch/common/bytes/BytesArray.java @@ -148,33 +148,12 @@ public class BytesArray implements BytesReference { } @Override - public boolean equals(Object obj) { - return bytesEquals((BytesArray) obj); - } - - public boolean bytesEquals(BytesArray other) { - if (length == other.length) { - int otherUpto = other.offset; - final byte[] otherBytes = other.bytes; - final int end = offset + length; - for (int upto = offset; upto < end; upto++, otherUpto++) { - if (bytes[upto] != otherBytes[otherUpto]) { - return false; - } - } - return true; - } else { - return false; - } + public int hashCode() { + return Helper.bytesHashCode(this); } @Override - public int hashCode() { - int result = 0; - final int end = offset + length; - for (int i = offset; i < end; i++) { - result = 31 * result + bytes[i]; - } - return result; + public boolean equals(Object obj) { + return Helper.bytesEqual(this, (BytesReference) obj); } } \ No newline at end of file diff --git a/src/main/java/org/elasticsearch/common/bytes/BytesReference.java b/src/main/java/org/elasticsearch/common/bytes/BytesReference.java index 9502caa130f..67c4114ad19 100644 --- a/src/main/java/org/elasticsearch/common/bytes/BytesReference.java +++ b/src/main/java/org/elasticsearch/common/bytes/BytesReference.java @@ -31,6 +31,46 @@ import java.util.Comparator; */ public interface BytesReference { + public static class Helper { + + public static boolean bytesEqual(BytesReference a, BytesReference b) { + if (a == b) { + return true; + } + if (a.length() != b.length()) { + return false; + } + if (!a.hasArray()) { + a = a.toBytesArray(); + } + if (!b.hasArray()) { + b = b.toBytesArray(); + } + int bUpTo = b.arrayOffset(); + final byte[] aArray = a.array(); + final byte[] bArray = b.array(); + final int end = a.arrayOffset() + a.length(); + for (int aUpTo = a.arrayOffset(); aUpTo < end; aUpTo++, bUpTo++) { + if (aArray[aUpTo] != bArray[bUpTo]) { + return false; + } + } + return true; + } + + public static int bytesHashCode(BytesReference a) { + if (!a.hasArray()) { + a = a.toBytesArray(); + } + int result = 0; + final int end = a.arrayOffset() + a.length(); + for (int i = a.arrayOffset(); i < end; i++) { + result = 31 * result + a.array()[i]; + } + return result; + } + } + /** * Returns the byte at the specified index. Need to be between 0 and length. */ @@ -103,7 +143,8 @@ public interface BytesReference { public static class UTF8SortedAsUnicodeComparator implements Comparator { // Only singleton - private UTF8SortedAsUnicodeComparator() {} + private UTF8SortedAsUnicodeComparator() { + } public int compare(BytesReference a, BytesReference b) { if (a.hasArray() && b.hasArray()) { @@ -113,7 +154,7 @@ public interface BytesReference { int bUpto = b.arrayOffset(); final int aStop = aUpto + Math.min(a.length(), b.length()); - while(aUpto < aStop) { + while (aUpto < aStop) { int aByte = aBytes[aUpto++] & 0xff; int bByte = bBytes[bUpto++] & 0xff; @@ -132,7 +173,7 @@ public interface BytesReference { int bUpto = 0; final int aStop = aUpto + Math.min(a.length(), b.length()); - while(aUpto < aStop) { + while (aUpto < aStop) { int aByte = aBytes[aUpto++] & 0xff; int bByte = bBytes[bUpto++] & 0xff; diff --git a/src/main/java/org/elasticsearch/common/bytes/ChannelBufferBytesReference.java b/src/main/java/org/elasticsearch/common/bytes/ChannelBufferBytesReference.java index f6fa8a116e6..b029c43330a 100644 --- a/src/main/java/org/elasticsearch/common/bytes/ChannelBufferBytesReference.java +++ b/src/main/java/org/elasticsearch/common/bytes/ChannelBufferBytesReference.java @@ -106,4 +106,14 @@ public class ChannelBufferBytesReference implements BytesReference { public String toUtf8() { return buffer.toString(Charsets.UTF_8); } + + @Override + public int hashCode() { + return Helper.bytesHashCode(this); + } + + @Override + public boolean equals(Object obj) { + return Helper.bytesEqual(this, (BytesReference) obj); + } } diff --git a/src/main/java/org/elasticsearch/common/bytes/HashedBytesArray.java b/src/main/java/org/elasticsearch/common/bytes/HashedBytesArray.java index 2182ed65a2a..eddacc26981 100644 --- a/src/main/java/org/elasticsearch/common/bytes/HashedBytesArray.java +++ b/src/main/java/org/elasticsearch/common/bytes/HashedBytesArray.java @@ -124,14 +124,12 @@ public class HashedBytesArray implements BytesReference { } @Override - public boolean equals(Object o) { - if (this == o) return true; - HashedBytesArray bytesWrap = (HashedBytesArray) o; - return Arrays.equals(bytes, bytesWrap.bytes); + public int hashCode() { + return Helper.bytesHashCode(this); } @Override - public int hashCode() { - return hashCode; + public boolean equals(Object obj) { + return Helper.bytesEqual(this, (BytesReference) obj); } } diff --git a/src/main/java/org/elasticsearch/common/text/BytesText.java b/src/main/java/org/elasticsearch/common/text/BytesText.java index 79180ba254e..6059ad8a186 100644 --- a/src/main/java/org/elasticsearch/common/text/BytesText.java +++ b/src/main/java/org/elasticsearch/common/text/BytesText.java @@ -61,4 +61,14 @@ public class BytesText implements Text { public String toString() { return string(); } + + @Override + public int hashCode() { + return bytes().hashCode(); + } + + @Override + public boolean equals(Object obj) { + return bytes().equals(((Text) obj).bytes()); + } } diff --git a/src/main/java/org/elasticsearch/common/text/StringAndBytesText.java b/src/main/java/org/elasticsearch/common/text/StringAndBytesText.java index 484449a2b20..e825e8ad846 100644 --- a/src/main/java/org/elasticsearch/common/text/StringAndBytesText.java +++ b/src/main/java/org/elasticsearch/common/text/StringAndBytesText.java @@ -87,4 +87,14 @@ public class StringAndBytesText implements Text { public String toString() { return string(); } + + @Override + public int hashCode() { + return bytes().hashCode(); + } + + @Override + public boolean equals(Object obj) { + return bytes().equals(((Text) obj).bytes()); + } } diff --git a/src/main/java/org/elasticsearch/common/text/StringText.java b/src/main/java/org/elasticsearch/common/text/StringText.java index 69bd67b7426..20183863b71 100644 --- a/src/main/java/org/elasticsearch/common/text/StringText.java +++ b/src/main/java/org/elasticsearch/common/text/StringText.java @@ -71,4 +71,16 @@ public class StringText implements Text { public String toString() { return string(); } + + @Override + public int hashCode() { + // we use bytes here so we can be consistent with other text implementations + return bytes().hashCode(); + } + + @Override + public boolean equals(Object obj) { + // we use bytes here so we can be consistent with other text implementations + return bytes().equals(((Text) obj).bytes()); + } }