diff --git a/CHANGES.txt b/CHANGES.txt index 284a5d16444..8e3b977ff0b 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -122,6 +122,8 @@ New Features option, as well as a list of protected terms. (Dan Rosher via hossman) +25. SOLR-928: SolrDocument and SolrInputDocument now implement the Map + interface. This should make plugging into other standard tools easier. (ryan) Optimizations diff --git a/src/common/org/apache/solr/common/SolrDocument.java b/src/common/org/apache/solr/common/SolrDocument.java index 2900a7fac88..483ffa5f69f 100644 --- a/src/common/org/apache/solr/common/SolrDocument.java +++ b/src/common/org/apache/solr/common/SolrDocument.java @@ -21,11 +21,10 @@ import java.io.Serializable; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; -import java.util.HashMap; import java.util.Iterator; +import java.util.LinkedHashMap; import java.util.Map; import java.util.Set; -import java.util.Map.Entry; /** @@ -39,13 +38,13 @@ import java.util.Map.Entry; * @version $Id$ * @since solr 1.3 */ -public class SolrDocument implements Serializable, Iterable> +public class SolrDocument implements Map, Iterable>, Serializable { - private Map _fields = null; + private final Map _fields; public SolrDocument() { - _fields = new HashMap(); + _fields = new LinkedHashMap(); } /** @@ -250,4 +249,63 @@ public class SolrDocument implements Serializable, Iterable remove(Object key) {throw new UnsupportedOperationException();} }; } + + //--------------------------------------------------- + // MAP interface + //--------------------------------------------------- + + @Override + public boolean containsKey(Object key) { + return _fields.containsKey(key); + } + + @Override + public boolean containsValue(Object value) { + return _fields.containsValue(value); + } + + @Override + public Set> entrySet() { + return _fields.entrySet(); + } + + @Override + public Object get(Object key) { + return _fields.get(key); + } + + @Override + public boolean isEmpty() { + return _fields.isEmpty(); + } + + @Override + public Set keySet() { + return _fields.keySet(); + } + + @Override + public Object put(String key, Object value) { + return _fields.put(key, value); + } + + @Override + public void putAll(Map t) { + _fields.putAll( t ); + } + + @Override + public Object remove(Object key) { + return _fields.remove(key); + } + + @Override + public int size() { + return _fields.size(); + } + + @Override + public Collection values() { + return _fields.values(); + } } diff --git a/src/common/org/apache/solr/common/SolrInputDocument.java b/src/common/org/apache/solr/common/SolrInputDocument.java index b0abf53b0b1..54b2310d420 100644 --- a/src/common/org/apache/solr/common/SolrInputDocument.java +++ b/src/common/org/apache/solr/common/SolrInputDocument.java @@ -18,11 +18,11 @@ package org.apache.solr.common; import java.io.Serializable; -import java.util.HashMap; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.Map; import java.util.Collection; +import java.util.Set; /** * Represent the field and boost information needed to construct and index @@ -32,13 +32,12 @@ import java.util.Collection; * @version $Id$ * @since solr 1.3 */ -public class SolrInputDocument implements Iterable, Serializable +public class SolrInputDocument implements Map, Iterable, Serializable { private final Map _fields; private float _documentBoost = 1.0f; - public SolrInputDocument() - { + public SolrInputDocument() { _fields = new LinkedHashMap(); } @@ -173,4 +172,64 @@ public class SolrInputDocument implements Iterable, Serializable { return "SolrInputDocument["+_fields+"]"; } + + + //--------------------------------------------------- + // MAP interface + //--------------------------------------------------- + + @Override + public boolean containsKey(Object key) { + return _fields.containsKey(key); + } + + @Override + public boolean containsValue(Object value) { + return _fields.containsValue(value); + } + + @Override + public Set> entrySet() { + return _fields.entrySet(); + } + + @Override + public SolrInputField get(Object key) { + return _fields.get(key); + } + + @Override + public boolean isEmpty() { + return _fields.isEmpty(); + } + + @Override + public Set keySet() { + return _fields.keySet(); + } + + @Override + public SolrInputField put(String key, SolrInputField value) { + return _fields.put(key, value); + } + + @Override + public void putAll(Map t) { + _fields.putAll( t ); + } + + @Override + public SolrInputField remove(Object key) { + return _fields.remove(key); + } + + @Override + public int size() { + return _fields.size(); + } + + @Override + public Collection values() { + return _fields.values(); + } } diff --git a/src/test/org/apache/solr/common/SolrDocumentTest.java b/src/test/org/apache/solr/common/SolrDocumentTest.java index 108e13a5bd6..77cf8d51853 100644 --- a/src/test/org/apache/solr/common/SolrDocumentTest.java +++ b/src/test/org/apache/solr/common/SolrDocumentTest.java @@ -22,6 +22,7 @@ import java.util.Collection; import java.util.Collections; import java.util.Iterator; import java.util.List; +import java.util.Map; import org.apache.solr.common.SolrDocument; import org.apache.solr.common.SolrInputDocument; @@ -175,6 +176,17 @@ public class SolrDocumentTest extends TestCase } assertEquals( (3*5), doc.getField("f").getValueCount() ); } + + public void testMapInterface() + { + SolrDocument doc = new SolrDocument(); + assertTrue( doc instanceof Map ); + assertTrue( Map.class.isAssignableFrom( SolrDocument.class ) ); + + SolrInputDocument indoc = new SolrInputDocument(); + assertTrue( indoc instanceof Map ); + assertTrue( Map.class.isAssignableFrom( indoc.getClass() ) ); + } }