You are viewing a plain text version of this content. The canonical link for it is here.
Posted to solr-commits@lucene.apache.org by ry...@apache.org on 2008/12/19 19:50:12 UTC

svn commit: r728104 - in /lucene/solr/trunk: CHANGES.txt src/common/org/apache/solr/common/SolrDocument.java src/common/org/apache/solr/common/SolrInputDocument.java src/test/org/apache/solr/common/SolrDocumentTest.java

Author: ryan
Date: Fri Dec 19 10:50:12 2008
New Revision: 728104

URL: http://svn.apache.org/viewvc?rev=728104&view=rev
Log:
SOLR-928: SolrDocument and SolrInputDocument now implement the Map<String,?>

Modified:
    lucene/solr/trunk/CHANGES.txt
    lucene/solr/trunk/src/common/org/apache/solr/common/SolrDocument.java
    lucene/solr/trunk/src/common/org/apache/solr/common/SolrInputDocument.java
    lucene/solr/trunk/src/test/org/apache/solr/common/SolrDocumentTest.java

Modified: lucene/solr/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/lucene/solr/trunk/CHANGES.txt?rev=728104&r1=728103&r2=728104&view=diff
==============================================================================
--- lucene/solr/trunk/CHANGES.txt (original)
+++ lucene/solr/trunk/CHANGES.txt Fri Dec 19 10:50:12 2008
@@ -122,6 +122,8 @@
     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

Modified: lucene/solr/trunk/src/common/org/apache/solr/common/SolrDocument.java
URL: http://svn.apache.org/viewvc/lucene/solr/trunk/src/common/org/apache/solr/common/SolrDocument.java?rev=728104&r1=728103&r2=728104&view=diff
==============================================================================
--- lucene/solr/trunk/src/common/org/apache/solr/common/SolrDocument.java (original)
+++ lucene/solr/trunk/src/common/org/apache/solr/common/SolrDocument.java Fri Dec 19 10:50:12 2008
@@ -21,11 +21,10 @@
 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 @@
  * @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 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();
+  }
 }

Modified: lucene/solr/trunk/src/common/org/apache/solr/common/SolrInputDocument.java
URL: http://svn.apache.org/viewvc/lucene/solr/trunk/src/common/org/apache/solr/common/SolrInputDocument.java?rev=728104&r1=728103&r2=728104&view=diff
==============================================================================
--- lucene/solr/trunk/src/common/org/apache/solr/common/SolrInputDocument.java (original)
+++ lucene/solr/trunk/src/common/org/apache/solr/common/SolrInputDocument.java Fri Dec 19 10:50:12 2008
@@ -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 @@
  * @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 @@
   {
     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();
+  }
 }

Modified: lucene/solr/trunk/src/test/org/apache/solr/common/SolrDocumentTest.java
URL: http://svn.apache.org/viewvc/lucene/solr/trunk/src/test/org/apache/solr/common/SolrDocumentTest.java?rev=728104&r1=728103&r2=728104&view=diff
==============================================================================
--- lucene/solr/trunk/src/test/org/apache/solr/common/SolrDocumentTest.java (original)
+++ lucene/solr/trunk/src/test/org/apache/solr/common/SolrDocumentTest.java Fri Dec 19 10:50:12 2008
@@ -22,6 +22,7 @@
 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 @@
     }
     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() ) );
+  }
 }