lucene-solr-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From no...@apache.org
Subject svn commit: r834780 - in /lucene/solr/trunk/src: solrj/org/apache/solr/client/solrj/beans/DocumentObjectBinder.java test/org/apache/solr/client/solrj/beans/TestDocumentObjectBinder.java
Date Wed, 11 Nov 2009 06:33:11 GMT
Author: noble
Date: Wed Nov 11 06:33:11 2009
New Revision: 834780

URL: http://svn.apache.org/viewvc?rev=834780&view=rev
Log:
SOLR-1551 Provide DocumentObjectBinder.getBean() method

Modified:
    lucene/solr/trunk/src/solrj/org/apache/solr/client/solrj/beans/DocumentObjectBinder.java
    lucene/solr/trunk/src/test/org/apache/solr/client/solrj/beans/TestDocumentObjectBinder.java

Modified: lucene/solr/trunk/src/solrj/org/apache/solr/client/solrj/beans/DocumentObjectBinder.java
URL: http://svn.apache.org/viewvc/lucene/solr/trunk/src/solrj/org/apache/solr/client/solrj/beans/DocumentObjectBinder.java?rev=834780&r1=834779&r2=834780&view=diff
==============================================================================
--- lucene/solr/trunk/src/solrj/org/apache/solr/client/solrj/beans/DocumentObjectBinder.java
(original)
+++ lucene/solr/trunk/src/solrj/org/apache/solr/client/solrj/beans/DocumentObjectBinder.java
Wed Nov 11 06:33:11 2009
@@ -43,21 +43,30 @@
 
     for(int j=0;j<solrDocList.size();j++) {
       SolrDocument sdoc = solrDocList.get(j);
-
-      T obj = null;
-      try {
-        obj = clazz.newInstance();
-      } catch (Exception e) {
-        throw new RuntimeException("Could not instantiate object of " + clazz,e);
-      }
-      for (int i = 0; i < fields.size(); i++) {
-        DocField docField = fields.get(i);
-        docField.inject(obj, sdoc);
-      }
-      result.add(obj);
+	  result.add(getBean(clazz, fields, sdoc));
     }
     return result;
   }
+  public <T> T getBean(Class<T> clazz, SolrDocument solrDoc) {
+    return getBean(clazz, null,solrDoc);
+  }
+  
+  private <T> T getBean(Class<T> clazz, List<DocField> fields, SolrDocument
solrDoc) {
+    if (fields == null) {
+      fields = getDocFields(clazz);
+    }
+    T obj = null;
+    try {
+      obj = clazz.newInstance();
+    } catch (Exception e) {
+      throw new RuntimeException("Could not instantiate object of " + clazz, e);
+    }
+    for (int i = 0; i < fields.size(); i++) {
+      DocField docField = fields.get(i);
+      docField.inject(obj, solrDoc);
+    }
+    return obj;
+  }
   
   public SolrInputDocument toSolrInputDocument( Object obj )
   {

Modified: lucene/solr/trunk/src/test/org/apache/solr/client/solrj/beans/TestDocumentObjectBinder.java
URL: http://svn.apache.org/viewvc/lucene/solr/trunk/src/test/org/apache/solr/client/solrj/beans/TestDocumentObjectBinder.java?rev=834780&r1=834779&r2=834780&view=diff
==============================================================================
--- lucene/solr/trunk/src/test/org/apache/solr/client/solrj/beans/TestDocumentObjectBinder.java
(original)
+++ lucene/solr/trunk/src/test/org/apache/solr/client/solrj/beans/TestDocumentObjectBinder.java
Wed Nov 11 06:33:11 2009
@@ -106,12 +106,17 @@
     SolrDocumentList docs = new SolrDocumentList();
     docs.add( ClientUtils.toSolrDocument(doc) );
     Item out = binder.getBeans( Item.class, docs ).get( 0 );
-
+    Item singleOut = binder.getBean(Item.class, ClientUtils.toSolrDocument(doc));
+    
     // make sure it came out the same
     Assert.assertEquals( item.id, out.id );
     Assert.assertEquals( item.inStock, out.inStock );
     Assert.assertEquals( item.categories.length, out.categories.length );
     Assert.assertEquals( item.features, out.features );
+    Assert.assertEquals( item.id, singleOut.id );
+    Assert.assertEquals( item.inStock, singleOut.inStock );
+    Assert.assertEquals( item.categories.length, singleOut.categories.length );
+    Assert.assertEquals( item.features, singleOut.features );
   }
 
   public static class Item {



Mime
View raw message