KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > triactive > jdo > store > RequestUsingFields


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: RequestUsingFields.java,v 1.2 2004/02/01 18:22:42 jackknifebarber Exp $
9  */

10
11 package com.triactive.jdo.store;
12
13 import com.triactive.jdo.model.ClassMetaData;
14 import com.triactive.jdo.util.IntArrayList;
15 import javax.jdo.JDOFatalInternalException;
16
17
18 /**
19  * A storage request involving specific fields of a persistence-capable
20  * class.
21  *
22  * @author <a HREF="mailto:mmartin5@austin.rr.com">Mike Martin</a>
23  * @version $Revision: 1.2 $
24  */

25
26 abstract class RequestUsingFields extends Request
27 {
28     /**
29      * The fields involved in this request that use ColumnMappings.
30      * Null if there are no such fields.
31      */

32     protected final int[] colFields;
33
34     /**
35      * The fields involved in this request that use ComplexMappings.
36      * Null if there are no such fields.
37      */

38     protected final int[] cpxFields;
39
40     /**
41      * The ColumnMappings used by fields involved in this request, indexed by
42      * absolute field number.
43      * Null if {@link #colFields} is null.
44      */

45     protected final ColumnMapping[] colFieldMappings;
46
47     /**
48      * The ComplexMappings used by fields involved in this request, indexed by
49      * absolute field number.
50      * Null if {@link #cpxFields} is null.
51      */

52     protected final ComplexMapping[] cpxFieldMappings;
53
54
55     /**
56      * Constructs a request involving all fields of the class backed by the
57      * given table.
58      *
59      * @param table
60      * The table on which the request will operate.
61      */

62
63     protected RequestUsingFields(final ClassBaseTable table)
64     {
65         this(table, new FieldIterator()
66         {
67         private ClassMetaData cmd = table.getClassMetaData();
68         private int fn = cmd.getInheritedFieldCount();
69         private int end = fn + cmd.getFieldCount();
70
71             public int nextFieldNumber()
72             {
73                 return fn < end ? fn++ : -1;
74             }
75         });
76     }
77
78
79     /**
80      * Constructs a request involving the specified fields of the class backed
81      * by the given table.
82      *
83      * @param table
84      * The table on which the request will operate.
85      * @param fieldNumbers
86      * The fields that will be involved in the request.
87      */

88
89     protected RequestUsingFields(final ClassBaseTable table, final int[] fieldNumbers)
90     {
91         this(table, new FieldIterator()
92     {
93         private ClassMetaData cmd = table.getClassMetaData();
94         private int start = cmd.getInheritedFieldCount();
95         private int end = start + cmd.getFieldCount();
96         private int idx = 0;
97
98         public int nextFieldNumber()
99         {
100                 while (idx < fieldNumbers.length)
101                 {
102                     int fn = fieldNumbers[idx++];
103
104                     if (fn >= start && fn < end)
105                         return fn;
106                 }
107
108         return -1;
109         }
110     });
111     }
112
113
114     /**
115      * Internal constructor that takes the field number list from a
116      * FieldIterator.
117      */

118
119     private RequestUsingFields(ClassBaseTable table, FieldIterator fieldIterator)
120     {
121         super(table);
122
123         ClassMetaData cmd = table.getClassMetaData();
124         int declaredFieldCount = cmd.getFieldCount();
125         int inheritedFieldCount = cmd.getInheritedFieldCount();
126         int totalFieldCount = inheritedFieldCount + declaredFieldCount;
127
128         IntArrayList colfn = new IntArrayList(declaredFieldCount);
129         IntArrayList cpxfn = new IntArrayList(declaredFieldCount);
130         ColumnMapping[] colfm = new ColumnMapping[totalFieldCount];
131         ComplexMapping[] cpxfm = new ComplexMapping[totalFieldCount];
132
133         int field;
134
135         while ((field = fieldIterator.nextFieldNumber()) >= 0)
136         {
137             if (table.isFieldPersistent(field))
138             {
139                 Mapping m = table.getFieldMapping(field);
140
141                 if (m instanceof ColumnMapping)
142                 {
143                     colfn.add(field);
144                     colfm[field] = (ColumnMapping)m;
145                 }
146                 else if (m instanceof ComplexMapping)
147                 {
148                     cpxfn.add(field);
149                     cpxfm[field] = (ComplexMapping)m;
150                 }
151                 else
152                     throw new JDOFatalInternalException("Unknown mapping: " + m);
153             }
154         }
155
156         if (colfn.isEmpty())
157         {
158             colFields = null;
159             colFieldMappings = null;
160         }
161         else
162         {
163             colFields = colfn.toArray();
164             colFieldMappings = colfm;
165         }
166
167         if (cpxfn.isEmpty())
168         {
169             cpxFields = null;
170             cpxFieldMappings = null;
171         }
172         else
173         {
174             cpxFields = cpxfn.toArray();
175             cpxFieldMappings = cpxfm;
176         }
177     }
178
179
180     /**
181      * An object that iterates over a set of field numbers.
182      */

183     private interface FieldIterator
184     {
185         /**
186          * Returns the next field number.
187          *
188          * @return The next field number, or -1 if there are no more fields.
189          */

190         int nextFieldNumber();
191     }
192 }
193
Popular Tags