KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > hql > HolderInstantiator


1 //$Id: HolderInstantiator.java,v 1.1 2005/04/22 18:05:56 oneovthafew Exp $
2
package org.hibernate.hql;
3
4 import java.lang.reflect.Constructor JavaDoc;
5 import java.util.Arrays JavaDoc;
6 import java.util.HashMap JavaDoc;
7 import java.util.Map JavaDoc;
8
9 import org.hibernate.QueryException;
10
11 /**
12  * @author Gavin King
13  */

14 public final class HolderInstantiator {
15     
16     //TODO: have an interface and three different subclasses!
17

18     private final Constructor JavaDoc constructor;
19     private final boolean returnMaps;
20     private final boolean returnLists;
21     private final String JavaDoc[] queryReturnAliases;
22     
23     public HolderInstantiator(
24             Constructor JavaDoc constructor,
25             boolean returnMaps,
26             boolean returnLists,
27             String JavaDoc[] queryReturnAliases
28     ) {
29         this.constructor = constructor;
30         this.returnLists = returnLists;
31         this.returnMaps = returnMaps;
32         this.queryReturnAliases = queryReturnAliases;
33     }
34     
35     public boolean isRequired() {
36         return constructor!=null || returnLists || returnMaps;
37     }
38     
39     public Object JavaDoc instantiate(Object JavaDoc[] row) {
40         if ( constructor != null ) {
41                 try {
42                     return constructor.newInstance( row );
43                 }
44                 catch ( Exception JavaDoc e ) {
45                     throw new QueryException(
46                         "could not instantiate: " +
47                         constructor.getDeclaringClass().getName(),
48                         e );
49                 }
50         }
51         else if ( returnMaps ) {
52                 Map JavaDoc map = new HashMap JavaDoc();
53                 for ( int j = 0; j < row.length; j++ ) {
54                     map.put( queryReturnAliases[j], row[j] );
55                 }
56                 return map;
57         }
58         else if ( returnLists ) {
59                 return Arrays.asList(row);
60         }
61         else {
62             return row;
63         }
64     }
65     
66 }
67
Popular Tags