mirror of https://github.com/apache/lucene.git
LUCENE-3590: fix ctor/equals brokenness in BytesRef/IntsRef/CharsRef
git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1206436 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
7f366cd0da
commit
18e1ec2b9e
|
@ -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 {
|
|||
*/
|
||||
@Override
|
||||
public 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 the
|
||||
|
|
|
@ -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];
|
||||
|
|
|
@ -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
|
|||
|
||||
@Override
|
||||
public 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;
|
||||
}
|
||||
|
|
|
@ -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 {
|
|||
|
||||
@Override
|
||||
public 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) {
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue