empire-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Rainer Döbele (JIRA) <empire-db-...@incubator.apache.org>
Subject [jira] Created: (EMPIREDB-42) Extend DBReader to provide query results in a hash set
Date Sun, 05 Jul 2009 09:58:14 GMT
Extend DBReader to provide query results in a hash set
------------------------------------------------------

                 Key: EMPIREDB-42
                 URL: https://issues.apache.org/jira/browse/EMPIREDB-42
             Project: Empire-DB
          Issue Type: Improvement
    Affects Versions: empire-db-2.0.6
            Reporter: Rainer Döbele
            Priority: Minor
             Fix For: empire-db-2.0.6


Suggestion made by eike on July 4th 2009:

===================================================================
--- src/main/java/org/apache/empire/db/DBReader.java	(revision 791026)
+++ src/main/java/org/apache/empire/db/DBReader.java	(working copy)
@@ -24,7 +24,9 @@
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.Iterator;
 import java.util.Map;
 
@@ -623,17 +625,19 @@
     }
 
     /**
-     * Returns the result of a query as a list of objects resticted
-     * to a maximum number of objects (unless maxCount is -1).
+     * Returns the result of a query as a collection of objects 
+     * restricted to a maximum number of objects (unless 
+     * maxCount is -1).
      * 
      * @param c the class type of the objects in the list
      * @param maxCount the maximum number of objects
-     * @param <T> the type of the objects in the list
+     * @param <T> the implementation type of {@link Collection}
+     * @param <S> the type of the objects in the list
      * 
      * @return the list of <T>
      */
     @SuppressWarnings("unchecked")
-    public <T> ArrayList<T> getBeanList(Class<T> c, int maxCount)
+    public <T extends Collection<S>, S> T getBeanList(T list, Class<S>
c, int maxCount)
     {
         // Check Recordset
         if (rset == null)
@@ -653,19 +657,19 @@
             Object[] args = (ctor!=null) ? new Object[getFieldCount()] : null; 
             
             // Create a list of beans
-            ArrayList<T> list = new ArrayList<T>();
+            
             while (moveNext() && maxCount != 0)
             { // Create bean an init
                 if (ctor!=null)
                 {   // Use Constructor
                     for (int i = 0; i < getFieldCount(); i++)
                         args[i] = getValue(i);
-                    T bean = (T)ctor.newInstance(args);
+                    S bean = (S)ctor.newInstance(args);
                     list.add(bean);
                 }
                 else
                 {   // Use Property Setters
-                    T bean = c.newInstance();
+                    S bean = c.newInstance();
                     if (getBeanProperties(bean)==false)
                         return null;
                     list.add(bean);
@@ -692,6 +696,33 @@
     }
 
     /**
+     * Returns the result of a query as a list of objects restricted
+     * to a maximum number of objects (unless maxCount is -1).
+     * 
+     * @param c the class type of the objects in the list
+     * @param maxCount the maximum number of objects
+     * @param <T> the type of the objects in the list
+     * 
+     * @return the list of <T>
+     */
+    public <T> ArrayList<T> getBeanList(Class<T> c, int maxCount) {
+    	return getBeanList(new ArrayList<T>(), c, maxCount);
+    }
+    
+    /**
+     * Returns the result of a query as a set of objects restricted
+     * to a maximum number of objects (unless maxCount=-1)
+     * 
+     * @param <T> the type of the objects in the set
+     * @param c the class type of the objects in the set
+     * @param maxCount the maximum size of the list
+     * @return
+     */
+    public <T> HashSet<T> getBeanSet(Class<T> c, int maxCount) {
+    	return getBeanList(new HashSet<T>(), c, maxCount);
+    }
+    
+    /**
      * Returns the result of a query as a list of objects.
      * 
      * @param c the class type of the objects in the list
@@ -701,7 +732,7 @@
      */
     public <T> ArrayList<T> getBeanList(Class<T> c)
     {
-        return getBeanList(c, -1);
+        return getBeanList(new ArrayList<T>(), c, -1);
     }
 
     /**


-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


Mime
View raw message