Apache Solr open-source search software
LUCENE-3590: fix ctor/equals brokenness in BytesRef/IntsRef/CharsRef
Robert Muir18e1ec2b9e57
diff --git a/lucene/src/java/org/apache/lucene/util/BytesRef.java b/lucene/src/java/org/apache/lucene/util/BytesRef.javaindex 5cbb610b566..19829bca509 100644--- a/lucene/src/java/org/apache/lucene/util/BytesRef.java+++ b/lucene/src/java/org/apache/lucene/util/BytesRef.java@@ -25,8 +25,6 @@ import java.util.Comparator;** @lucene.experimental */public final class BytesRef implements Comparable<BytesRef>,Cloneable {-- static final int HASH_PRIME = 31;public static final byte[] EMPTY_BYTES = new byte[0];/** The contents of the BytesRef. Should never be {@code null}. */@@ -142,12 +140,12 @@ public final class BytesRef implements Comparable<BytesRef>,Cloneable {*/@Overridepublic int hashCode() {- int result = 0;+ int hash = 0;final int end = offset + length;for(int i=offset;i<end;i++) {- result = HASH_PRIME * result + bytes[i];+ hash = 31 * hash + bytes[i];}- return result;+ return hash;}@Override@@ -155,7 +153,10 @@ public final class BytesRef implements Comparable<BytesRef>,Cloneable {if (other == null) {return false;}- return this.bytesEquals((BytesRef) other);+ if (other instanceof BytesRef) {+ return this.bytesEquals((BytesRef) other);+ }+ return false;}/** Interprets stored bytes as UTF8 bytes, returning thediff --git a/lucene/src/java/org/apache/lucene/util/BytesRefHash.java b/lucene/src/java/org/apache/lucene/util/BytesRefHash.javaindex 72b013d0187..6088605df72 100644--- a/lucene/src/java/org/apache/lucene/util/BytesRefHash.java+++ b/lucene/src/java/org/apache/lucene/util/BytesRefHash.java@@ -432,7 +432,7 @@ public final class BytesRefHash {final int endPos = pos + len;while (pos < endPos) {- code = BytesRef.HASH_PRIME * code + bytes[pos++];+ code = 31 * code + bytes[pos++];}} else {code = bytesStart[e0];diff --git a/lucene/src/java/org/apache/lucene/util/CharsRef.java b/lucene/src/java/org/apache/lucene/util/CharsRef.javaindex 6a8c853165f..4d2a9c5389a 100644--- a/lucene/src/java/org/apache/lucene/util/CharsRef.java+++ b/lucene/src/java/org/apache/lucene/util/CharsRef.java@@ -26,7 +26,7 @@ import java.util.Comparator;* @lucene.internal*/public final class CharsRef implements Comparable<CharsRef>, CharSequence, Cloneable {- private static final char[] EMPTY_ARRAY = new char[0];+ private static final char[] EMPTY_CHARS = new char[0];public char[] chars;public int offset;public int length;@@ -35,7 +35,7 @@ public final class CharsRef implements Comparable<CharsRef>, CharSequence, Clone* Creates a new {@link CharsRef} initialized an empty array zero-length*/public CharsRef() {- this(EMPTY_ARRAY, 0, 0);+ this(EMPTY_CHARS, 0, 0);}/**@@ -86,26 +86,11 @@ public final class CharsRef implements Comparable<CharsRef>, CharSequence, Clone@Overridepublic boolean equals(Object other) {- if (this == other) {- return true;+ if (other == null) {+ return false;}-if (other instanceof CharsRef) {- return charsEquals((CharsRef) other);- }-- if (other instanceof CharSequence) {- final CharSequence seq = (CharSequence) other;- if (length == seq.length()) {- int n = length;- int i = offset;- int j = 0;- while (n-- != 0) {- if (chars[i++] != seq.charAt(j++))- return false;- }- return true;- }+ return this.charsEquals((CharsRef) other);}return false;}diff --git a/lucene/src/java/org/apache/lucene/util/IntsRef.java b/lucene/src/java/org/apache/lucene/util/IntsRef.javaindex 982df20fcdf..e2a4dc9d5f8 100644--- a/lucene/src/java/org/apache/lucene/util/IntsRef.java+++ b/lucene/src/java/org/apache/lucene/util/IntsRef.java@@ -31,6 +31,7 @@ public final class IntsRef implements Comparable<IntsRef>, Cloneable {public int length;public IntsRef() {+ ints = EMPTY_INTS;}public IntsRef(int capacity) {@@ -38,6 +39,7 @@ public final class IntsRef implements Comparable<IntsRef>, Cloneable {}public IntsRef(int[] ints, int offset, int length) {+ assert ints != null;this.ints = ints;this.offset = offset;this.length = length;@@ -61,7 +63,13 @@ public final class IntsRef implements Comparable<IntsRef>, Cloneable {@Overridepublic boolean equals(Object other) {- return this.intsEquals((IntsRef) other);+ if (other == null) {+ return false;+ }+ if (other instanceof IntsRef) {+ return this.intsEquals((IntsRef) other);+ }+ return false;}public boolean intsEquals(IntsRef other) {diff --git a/lucene/src/test/org/apache/lucene/util/TestUnicodeUtil.java b/lucene/src/test/org/apache/lucene/util/TestUnicodeUtil.javaindex 55a4f4da5c7..32d39c091f3 100644--- a/lucene/src/test/org/apache/lucene/util/TestUnicodeUtil.java+++ b/lucene/src/test/org/apache/lucene/util/TestUnicodeUtil.java@@ -178,8 +178,6 @@ public class TestUnicodeUtil extends LuceneTestCase {CharsRef cRef = new CharsRef(arr, offset, len);UnicodeUtil.UTF8toUTF16(ref, cRef);assertEquals(cRef.toString(), unicode);- assertEquals(cRef, unicode); // CharSeq- assertEquals(cRef, ref.utf8ToString()); // CharSeq}}}