You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@empire-db.apache.org by "Rainer Döbele (JIRA)" <em...@incubator.apache.org> on 2009/07/06 22:13:14 UTC

[jira] Updated: (EMPIREDB-42) Extend DBReader to provide query results in a hash set

     [ https://issues.apache.org/jira/browse/EMPIREDB-42?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Rainer Döbele updated EMPIREDB-42:
----------------------------------

          Component/s: Core
    Affects Version/s:     (was: empire-db-2.0.6)
                       empire-db-2.0.5-incubating
        Fix Version/s:     (was: empire-db-2.0.6)
                       empire-db-2.0.5-incubating

I have modified the DBReader getBeanList function according to eike's suggestion, simply because it is more generic and hence more flexible than before. In order to be compatible with previous versions I have two overloads for the current function signatures.

I have however not created another added more helper functions to obtain the result as a HashSet, a LinkedHastSet or whatever other Collection.
Anyone who needs it can call the generic getBeanList(C c, Class<T> t, int maxCount) function him/herself with whatever type of collection he/she likes.

Rainer

> 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
>          Components: Core
>    Affects Versions: empire-db-2.0.5-incubating
>            Reporter: Rainer Döbele
>            Assignee: Rainer Döbele
>            Priority: Minor
>             Fix For: empire-db-2.0.5-incubating
>
>
> 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.