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:
Robert Muir 2011-11-26 13:56:23 +00:00
parent 7f366cd0da
commit 18e1ec2b9e
5 changed files with 22 additions and 30 deletions

View File

@ -25,8 +25,6 @@ import java.util.Comparator;
* *
* @lucene.experimental */ * @lucene.experimental */
public final class BytesRef implements Comparable<BytesRef>,Cloneable { public final class BytesRef implements Comparable<BytesRef>,Cloneable {
static final int HASH_PRIME = 31;
public static final byte[] EMPTY_BYTES = new byte[0]; public static final byte[] EMPTY_BYTES = new byte[0];
/** The contents of the BytesRef. Should never be {@code null}. */ /** The contents of the BytesRef. Should never be {@code null}. */
@ -142,12 +140,12 @@ public final class BytesRef implements Comparable<BytesRef>,Cloneable {
*/ */
@Override @Override
public int hashCode() { public int hashCode() {
int result = 0; int hash = 0;
final int end = offset + length; final int end = offset + length;
for(int i=offset;i<end;i++) { for(int i=offset;i<end;i++) {
result = HASH_PRIME * result + bytes[i]; hash = 31 * hash + bytes[i];
} }
return result; return hash;
} }
@Override @Override
@ -155,7 +153,10 @@ public final class BytesRef implements Comparable<BytesRef>,Cloneable {
if (other == null) { if (other == null) {
return false; 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 /** Interprets stored bytes as UTF8 bytes, returning the

View File

@ -432,7 +432,7 @@ public final class BytesRefHash {
final int endPos = pos + len; final int endPos = pos + len;
while (pos < endPos) { while (pos < endPos) {
code = BytesRef.HASH_PRIME * code + bytes[pos++]; code = 31 * code + bytes[pos++];
} }
} else { } else {
code = bytesStart[e0]; code = bytesStart[e0];

View File

@ -26,7 +26,7 @@ import java.util.Comparator;
* @lucene.internal * @lucene.internal
*/ */
public final class CharsRef implements Comparable<CharsRef>, CharSequence, Cloneable { 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 char[] chars;
public int offset; public int offset;
public int length; 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 * Creates a new {@link CharsRef} initialized an empty array zero-length
*/ */
public CharsRef() { 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 @Override
public boolean equals(Object other) { public boolean equals(Object other) {
if (this == other) { if (other == null) {
return true; return false;
} }
if (other instanceof CharsRef) { if (other instanceof CharsRef) {
return charsEquals((CharsRef) other); return this.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 false; return false;
} }

View File

@ -31,6 +31,7 @@ public final class IntsRef implements Comparable<IntsRef>, Cloneable {
public int length; public int length;
public IntsRef() { public IntsRef() {
ints = EMPTY_INTS;
} }
public IntsRef(int capacity) { public IntsRef(int capacity) {
@ -38,6 +39,7 @@ public final class IntsRef implements Comparable<IntsRef>, Cloneable {
} }
public IntsRef(int[] ints, int offset, int length) { public IntsRef(int[] ints, int offset, int length) {
assert ints != null;
this.ints = ints; this.ints = ints;
this.offset = offset; this.offset = offset;
this.length = length; this.length = length;
@ -61,7 +63,13 @@ public final class IntsRef implements Comparable<IntsRef>, Cloneable {
@Override @Override
public boolean equals(Object other) { 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) { public boolean intsEquals(IntsRef other) {

View File

@ -178,8 +178,6 @@ public class TestUnicodeUtil extends LuceneTestCase {
CharsRef cRef = new CharsRef(arr, offset, len); CharsRef cRef = new CharsRef(arr, offset, len);
UnicodeUtil.UTF8toUTF16(ref, cRef); UnicodeUtil.UTF8toUTF16(ref, cRef);
assertEquals(cRef.toString(), unicode); assertEquals(cRef.toString(), unicode);
assertEquals(cRef, unicode); // CharSeq
assertEquals(cRef, ref.utf8ToString()); // CharSeq
} }
} }
} }