LUCENE-1481: implement equals() and hashCode() for Sort & SortField

git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@724379 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael McCandless 2008-12-08 15:28:16 +00:00
parent 62c7438ed4
commit e0efcd52ea
2 changed files with 44 additions and 0 deletions

View File

@ -18,6 +18,7 @@ package org.apache.lucene.search;
*/
import java.io.Serializable;
import java.util.Arrays;
/**
@ -222,4 +223,20 @@ implements Serializable {
return buffer.toString();
}
/** Returns true if <code>o</code> is equal to this. */
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Sort)) return false;
final Sort other = (Sort)o;
return Arrays.equals(this.fields, other.fields);
}
/** Returns a hash code value for this object. */
public int hashCode() {
// TODO in Java 1.5: switch to Arrays.hashCode(). The
// Java 1.4 workaround below calculates the same hashCode
// as Java 1.5's new Arrays.hashCode()
return 0x45aaf665 + Arrays.asList(fields).hashCode();
}
}

View File

@ -244,4 +244,31 @@ implements Serializable {
return buffer.toString();
}
/** Returns true if <code>o</code> is equal to this. If a
* {@link #SortComparatorSource} was provided, it must
* properly implement equals. */
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof SortField)) return false;
final SortField other = (SortField)o;
return (
other.field == this.field // field is always interned
&& other.type == this.type
&& other.reverse == this.reverse
&& (other.locale == null ? this.locale == null : other.locale.equals(this.locale))
&& (other.factory == null ? this.factory == null : other.factory.equals(this.factory))
);
}
/** Returns a hash code value for this object. If a
* {@link #SortComparatorSource} was provided, it must
* properly implement hashCode. */
public int hashCode() {
int hash=type^0x346565dd + Boolean.valueOf(reverse).hashCode()^0xaf5998bb;
if (field != null) hash += field.hashCode()^0xff5685dd;
if (locale != null) hash += locale.hashCode()^0x08150815;
if (factory != null) hash += factory.hashCode()^0x34987555;
return hash;
}
}