Skip to content

Apache Lucene open-source search software

SOLR-928: SolrDocument and SolrInputDocument now implement the Map<String,?>

Ryan McKinleyf8224321531c
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<String,?>
+ 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<Map.Entry<String, Object>>
+public class SolrDocument implements Map<String,Object>, Iterable<Map.Entry<String, Object>>, Serializable
{
- private Map<String,Object> _fields = null;
+ private final Map<String,Object> _fields;
public SolrDocument()
{
- _fields = new HashMap<String,Object>();
+ _fields = new LinkedHashMap<String,Object>();
}
/**
@@ -250,4 +249,63 @@ public class SolrDocument implements Serializable, Iterable<Map.Entry<String, Ob
public Collection<Object> 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<Entry<String, Object>> entrySet() {
+ return _fields.entrySet();
+ }
+
+ @Override
+ public Object get(Object key) {
+ return _fields.get(key);
+ }
+
+ @Override
+ public boolean isEmpty() {
+ return _fields.isEmpty();
+ }
+
+ @Override
+ public Set<String> keySet() {
+ return _fields.keySet();
+ }
+
+ @Override
+ public Object put(String key, Object value) {
+ return _fields.put(key, value);
+ }
+
+ @Override
+ public void putAll(Map<? extends String, ? extends Object> t) {
+ _fields.putAll( t );
+ }
+
+ @Override
+ public Object remove(Object key) {
+ return _fields.remove(key);
+ }
+
+ @Override
+ public int size() {
+ return _fields.size();
+ }
+
+ @Override
+ public Collection<Object> 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<SolrInputField>, Serializable
+public class SolrInputDocument implements Map<String,SolrInputField>, Iterable<SolrInputField>, Serializable
{
private final Map<String,SolrInputField> _fields;
private float _documentBoost = 1.0f;
- public SolrInputDocument()
- {
+ public SolrInputDocument() {
_fields = new LinkedHashMap<String,SolrInputField>();
}
@@ -173,4 +172,64 @@ public class SolrInputDocument implements Iterable<SolrInputField>, 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<Entry<String, SolrInputField>> entrySet() {
+ return _fields.entrySet();
+ }
+
+ @Override
+ public SolrInputField get(Object key) {
+ return _fields.get(key);
+ }
+
+ @Override
+ public boolean isEmpty() {
+ return _fields.isEmpty();
+ }
+
+ @Override
+ public Set<String> keySet() {
+ return _fields.keySet();
+ }
+
+ @Override
+ public SolrInputField put(String key, SolrInputField value) {
+ return _fields.put(key, value);
+ }
+
+ @Override
+ public void putAll(Map<? extends String, ? extends SolrInputField> t) {
+ _fields.putAll( t );
+ }
+
+ @Override
+ public SolrInputField remove(Object key) {
+ return _fields.remove(key);
+ }
+
+ @Override
+ public int size() {
+ return _fields.size();
+ }
+
+ @Override
+ public Collection<SolrInputField> 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() ) );
+ }
}