SOLR-5837 Added .equals method for SolrDocument, SolrInputDocument and SolrInputField

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1575886 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Noble Paul 2014-03-10 10:18:42 +00:00
parent f72bcd8eb4
commit babbd07ed1
4 changed files with 195 additions and 2 deletions

View File

@ -23,7 +23,9 @@ import org.apache.lucene.index.AtomicReader;
import org.apache.lucene.index.NumericDocValues;
import org.apache.lucene.index.SlowCompositeReaderWrapper;
import org.apache.lucene.search.similarities.DefaultSimilarity;
import org.apache.lucene.util.TestUtil;
import org.apache.solr.SolrTestCaseJ4;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.params.CommonParams;
import org.apache.solr.common.SolrException;
import org.apache.solr.common.SolrInputDocument;
@ -394,4 +396,106 @@ public class DocumentBuilderTest extends SolrTestCaseJ4 {
assertNull(h.validateUpdate(add(xml, new String[0])));
}
public void testSolrDocumentEquals() {
String randomString = TestUtil.randomSimpleString(random());
SolrDocument doc1 = new SolrDocument();
doc1.addField("foo", randomString);
SolrDocument doc2 = new SolrDocument();
doc2.addField("foo", randomString);
assertTrue(doc1.equals(doc2));
doc1.addField("foo", "bar");
assertFalse(doc1.equals(doc2));
doc1 = new SolrDocument();
doc1.addField("bar", randomString);
assertFalse(doc1.equals(doc2));
int randomInt = random().nextInt();
doc1 = new SolrDocument();
doc1.addField("foo", randomInt);
doc2 = new SolrDocument();
doc2.addField("foo", randomInt);
assertTrue(doc1.equals(doc2));
doc2 = new SolrDocument();
doc2.addField("bar", randomInt);
assertFalse(doc1.equals(doc2));
}
public void testSolrInputDocumentEquality() {
String randomString = TestUtil.randomSimpleString(random());
SolrInputDocument doc1 = new SolrInputDocument();
doc1.addField("foo", randomString);
SolrInputDocument doc2 = new SolrInputDocument();
doc2.addField("foo", randomString);
assertTrue(doc1.equals(doc2));
doc1.setDocumentBoost(1.1f);
assertFalse(doc1.equals(doc2));
doc2.setDocumentBoost(1.1f);
assertTrue(doc1.equals(doc2));
doc2.setDocumentBoost(20f);
assertFalse(doc1.equals(doc2));
doc1 = new SolrInputDocument();
doc1.addField("foo", randomString);
doc2 = new SolrInputDocument();
doc2.addField("foo", randomString);
SolrInputDocument childDoc = new SolrInputDocument();
childDoc.addField("foo", "bar");
doc1.addChildDocument(childDoc);
assertFalse(doc1.equals(doc2));
doc2.addChildDocument(childDoc);
assertTrue(doc1.equals(doc2));
SolrInputDocument childDoc1 = new SolrInputDocument();
childDoc.addField(TestUtil.randomSimpleString(random()), TestUtil.randomSimpleString(random()));
doc2.addChildDocument(childDoc1);
assertFalse(doc1.equals(doc2));
}
public void testSolrInputFieldEquality() {
String randomString = TestUtil.randomSimpleString(random(), 10, 20);
int val = random().nextInt();
SolrInputField sif1 = new SolrInputField(randomString);
sif1.setValue(val, 1.0f);
SolrInputField sif2 = new SolrInputField(randomString);
sif2.setValue(val, 1.0f);
assertTrue(sif1.equals(sif2));
sif1.setBoost(2.1f);
sif2.setBoost(2.1f);
assertTrue(sif1.equals(sif2));
sif2.setBoost(2.0f);
assertFalse(sif1.equals(sif2));
sif2.setName("foo");
assertFalse(sif1.equals(sif2));
}
}

View File

@ -213,7 +213,30 @@ public class SolrDocument implements Map<String,Object>, Iterable<Map.Entry<Stri
public Iterator<Entry<String, Object>> iterator() {
return _fields.entrySet().iterator();
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof SolrDocument)) {
return false;
}
SolrDocument solrDocument = (SolrDocument) o;
if (!_fields.equals(solrDocument._fields)) {
return false;
}
return true;
}
@Override
public int hashCode() {
return _fields.hashCode();
}
//-----------------------------------------------------------------------------------------
// JSTL Helpers
//-----------------------------------------------------------------------------------------

View File

@ -275,7 +275,39 @@ public class SolrInputDocument implements Map<String,SolrInputField>, Iterable<S
public Collection<SolrInputField> values() {
return _fields.values();
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof SolrInputDocument)) {
return false;
}
SolrInputDocument sdoc = (SolrInputDocument) o;
if (!_fields.equals(sdoc._fields)) {
return false;
}
if (Float.compare(sdoc._documentBoost, _documentBoost) != 0) {
return false;
}
if (_childDocuments != null ? !_childDocuments.equals(sdoc._childDocuments) : sdoc._childDocuments != null) {
return false;
}
return true;
}
@Override
public int hashCode() {
int result = _fields.hashCode();
result = 31 * result + (_documentBoost != +0.0f ? Float.floatToIntBits(_documentBoost) : 0);
result = 31 * result + (_childDocuments != null ? _childDocuments.hashCode() : 0);
return result;
}
public void addChildDocument(SolrInputDocument child) {
if (_childDocuments == null) {
_childDocuments = new ArrayList<SolrInputDocument>();

View File

@ -229,4 +229,38 @@ public class SolrInputField implements Iterable<Object>, Serializable
}
return clone;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof SolrInputField)) {
return false;
}
SolrInputField sif = (SolrInputField) o;
if (!name.equals(sif.name)) {
return false;
}
if (!value.equals(sif.value)) {
return false;
}
if (Float.compare(sif.boost, boost) != 0) {
return false;
}
return true;
}
@Override
public int hashCode() {
int result = name.hashCode();
result = 31 * result + value.hashCode();
result = 31 * result + (boost != +0.0f ? Float.floatToIntBits(boost) : 0);
return result;
}
}