KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > triactive > jdo > sco > SCOProcessor


1 /*
2  * Copyright 2004 (C) TJDO.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the TJDO License version 1.0.
6  * See the terms of the TJDO License in the documentation provided with this software.
7  *
8  * $Id: SCOProcessor.java,v 1.3 2004/02/07 22:42:50 jackknifebarber Exp $
9  */

10
11 package com.triactive.jdo.sco;
12
13 import com.triactive.jdo.PersistenceManager;
14 import com.triactive.jdo.SCO;
15 import com.triactive.jdo.store.Mapping;
16 import com.triactive.jdo.store.MapMapping;
17 import com.triactive.jdo.store.MapStore;
18 import com.triactive.jdo.store.SetMapping;
19 import com.triactive.jdo.store.SetStore;
20 import javax.jdo.JDOHelper;
21 import javax.jdo.JDOUserException;
22
23
24 /**
25  * An object that helps manage SCO fields of a particular declared type.
26  * Currently, an SCO processor's only function is to serve as a factory for SCO
27  * insatnces.
28  *
29  * @author <a HREF="mailto:mmartin5@austin.rr.com">Mike Martin</a>
30  * @version $Revision: 1.3 $
31  */

32
33 public abstract class SCOProcessor
34 {
35     private static final java.util.HashMap JavaDoc processors = new java.util.HashMap JavaDoc();
36
37     static
38     {
39         SCOProcessor hashMapProcessor = new HashMapProcessor();
40         SCOProcessor hashSetProcessor = new HashSetProcessor();
41
42         processors.put(java.util.Collection JavaDoc.class, hashSetProcessor);
43         processors.put(java.util.Date JavaDoc.class, new DateProcessor());
44         processors.put(java.util.HashMap JavaDoc.class, hashMapProcessor);
45         processors.put(java.util.HashSet JavaDoc.class, hashSetProcessor);
46         processors.put(java.util.Hashtable JavaDoc.class, new HashtableProcessor());
47         processors.put(java.util.Map JavaDoc.class, hashMapProcessor);
48         processors.put(java.util.Set JavaDoc.class, hashSetProcessor);
49         processors.put(java.sql.Date JavaDoc.class, new SqlDateProcessor());
50         processors.put(java.sql.Timestamp JavaDoc.class, new SqlTimestampProcessor());
51     }
52
53
54     /**
55      * Tests whether a given type represents a supported second-class mutable
56      * type.
57      *
58      * @param c
59      * The class to be tested.
60      * @return
61      * <code>true</code> if fields declared as type <var>c</var> are
62      * supported as second-class mutable objects, <code>false</code>
63      * otherwise.
64      */

65
66     public static boolean isSecondClassMutableType(Class JavaDoc c)
67     {
68         return processors.containsKey(c);
69     }
70
71
72     /**
73      * Returns the SCO processor for fields of the given type.
74      *
75      * @param c
76      * The type of field.
77      * @return
78      * The corresponding SCO processor.
79      */

80
81     public static SCOProcessor forFieldType(Class JavaDoc c)
82     {
83         return (SCOProcessor)processors.get(c);
84     }
85
86
87     /**
88      * Returns a new SCO instance.
89      * The returned instance is owned by the specified owner but has
90      * <em>not</em> been assigned to the corresponding field.
91      *
92      * @param owner
93      * The first-class object that will own the new second-class object.
94      * @param fieldName
95      * The name of the field in the owning object.
96      * @param value
97      * The initial value of the new object.
98      *
99      * @return
100      * A new SCO instance.
101      */

102
103     public abstract SCO newSCOInstance(Object JavaDoc owner, String JavaDoc fieldName, Object JavaDoc value);
104
105
106     private static Mapping getMapping(Object JavaDoc owner, String JavaDoc fieldName)
107     {
108         return ((PersistenceManager)JDOHelper.getPersistenceManager(owner))
109                 .getStoreManager()
110                 .getClassBaseTable(owner.getClass())
111                 .getFieldMapping(fieldName);
112     }
113
114
115     /**
116      * An SCO processor for Map fields.
117      */

118
119     public static abstract class MapProcessor extends SCOProcessor
120     {
121         /**
122          * Returns the backing store for the specified Map field.
123          */

124         protected MapStore getMapStore(Object JavaDoc owner, String JavaDoc fieldName)
125         {
126             return ((MapMapping)getMapping(owner, fieldName)).getMapStore();
127         }
128
129         /**
130          * Returns a new SCO Map instance that is unloaded.
131          * The returned instance is owned by the specified owner but has
132          * <em>not</em> been assigned to the corresponding field.
133          * <p>
134          * Initially, the contents of the map are not necessarily loaded in
135          * memory. Its contents are partially or fully loaded from the backing
136          * store in response to application method calls on the map.
137          *
138          * @param owner
139          * The first-class object that will own the new second-class object.
140          * @param fieldName
141          * The name of the field in the owning object.
142          * @param mapStore
143          * The backing store for the map.
144          *
145          * @return
146          * A new SCO instance.
147          */

148         public abstract SCO newSCOInstance(Object JavaDoc owner, String JavaDoc fieldName, MapStore mapStore);
149     }
150
151
152     /**
153      * An SCO processor for Set fields.
154      */

155
156     public static abstract class SetProcessor extends SCOProcessor
157     {
158         /**
159          * Returns the backing store for the specified Collection or Set field.
160          */

161         protected SetStore getSetStore(Object JavaDoc owner, String JavaDoc fieldName)
162         {
163             return ((SetMapping)getMapping(owner, fieldName)).getSetStore();
164         }
165
166         /**
167          * Returns a new SCO Set instance that is unloaded.
168          * The returned instance is owned by the specified owner but has
169          * <em>not</em> been assigned to the corresponding field.
170          * <p>
171          * Initially, the contents of the set are not necessarily loaded in
172          * memory. Its contents are partially or fully loaded from the backing
173          * store in response to application method calls on the set.
174          *
175          * @param owner
176          * The first-class object that will own the new second-class object.
177          * @param fieldName
178          * The name of the field in the owning object.
179          * @param setStore
180          * The backing store for the set.
181          *
182          * @return
183          * A new SCO instance.
184          */

185         public abstract SCO newSCOInstance(Object JavaDoc owner, String JavaDoc fieldName, SetStore setStore);
186     }
187
188
189     /**
190      * The SCO processor for java.util.Date fields.
191      */

192     public static class DateProcessor extends SCOProcessor
193     {
194         public SCO newSCOInstance(Object JavaDoc owner, String JavaDoc fieldName, Object JavaDoc value)
195         {
196             return new com.triactive.jdo.sco.Date(owner, fieldName, (java.util.Date JavaDoc)value);
197         }
198     }
199
200     /**
201      * The SCO processor for java.util.HashMap fields.
202      */

203     public static class HashMapProcessor extends MapProcessor
204     {
205         public SCO newSCOInstance(Object JavaDoc owner, String JavaDoc fieldName, MapStore mapStore)
206         {
207             return new com.triactive.jdo.sco.HashMap(owner, fieldName, mapStore);
208         }
209
210         public SCO newSCOInstance(Object JavaDoc owner, String JavaDoc fieldName, Object JavaDoc value)
211         {
212             return new com.triactive.jdo.sco.HashMap(owner,
213                                                      fieldName,
214                                                      getMapStore(owner, fieldName),
215                                                      (java.util.Map JavaDoc)value);
216         }
217     }
218
219     /**
220      * The SCO processor for java.util.HashSet fields.
221      */

222     public static class HashSetProcessor extends SetProcessor
223     {
224         public SCO newSCOInstance(Object JavaDoc owner, String JavaDoc fieldName, SetStore setStore)
225         {
226             return new com.triactive.jdo.sco.HashSet(owner, fieldName, setStore);
227         }
228
229         public SCO newSCOInstance(Object JavaDoc owner, String JavaDoc fieldName, Object JavaDoc value)
230         {
231             return new com.triactive.jdo.sco.HashSet(owner,
232                                                      fieldName,
233                                                      getSetStore(owner, fieldName),
234                                                      (java.util.Collection JavaDoc)value);
235         }
236     }
237
238     /**
239      * The SCO processor for java.util.Hashtable fields.
240      */

241     public static class HashtableProcessor extends MapProcessor
242     {
243         public SCO newSCOInstance(Object JavaDoc owner, String JavaDoc fieldName, MapStore mapStore)
244         {
245             return new com.triactive.jdo.sco.Hashtable(owner, fieldName, mapStore);
246         }
247
248         public SCO newSCOInstance(Object JavaDoc owner, String JavaDoc fieldName, Object JavaDoc value)
249         {
250             return new com.triactive.jdo.sco.Hashtable(owner,
251                                                        fieldName,
252                                                        getMapStore(owner, fieldName),
253                                                        (java.util.Map JavaDoc)value);
254         }
255     }
256
257     /**
258      * The SCO processor for java.sql.Date fields.
259      */

260     public static class SqlDateProcessor extends SCOProcessor
261     {
262         public SCO newSCOInstance(Object JavaDoc owner, String JavaDoc fieldName, Object JavaDoc value)
263         {
264             return new com.triactive.jdo.sco.SqlDate(owner, fieldName, (java.sql.Date JavaDoc)value);
265         }
266     }
267
268     /**
269      * The SCO processor for java.sql.Timestamp fields.
270      */

271     public static class SqlTimestampProcessor extends SCOProcessor
272     {
273         public SCO newSCOInstance(Object JavaDoc owner, String JavaDoc fieldName, Object JavaDoc value)
274         {
275             return new com.triactive.jdo.sco.SqlTimestamp(owner, fieldName, (java.sql.Timestamp JavaDoc)value);
276         }
277     }
278 }
279
Popular Tags