View Javadoc

1   /*
2    * Copyright 2005 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at 
7    * 
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software 
11   * distributed under the License is distributed on an "AS IS" BASIS, 
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
13   * See the License for the specific language governing permissions and 
14   * limitations under the License.
15   */
16  
17  package org.apache.jdo.impl.enhancer.meta.prop;
18  
19  import java.lang.reflect.Modifier;
20  
21  import java.util.Comparator;
22  import java.util.List;
23  import java.util.Collections;
24  import java.util.ArrayList;
25  
26  
27  /***
28   * A class to hold all parsed attributes of a class.
29   */
30  final class JDOClass
31  {
32      /***
33       * The name of the class.
34       */
35      private final String name;
36      
37      /***
38       * The name of the superclass.
39       */
40      private String superClassName = null;
41      
42      /***
43       * The name of the oid class.
44       */
45      private String oidClassName = null;
46      
47      /***
48       * The access modifier of the class.
49       */
50      private int modifiers = Modifier.PUBLIC;
51      
52      /***
53       * The persistence modifier of the class.
54       */
55      private boolean isPersistent = true;
56      
57      /***
58       * Flag indicating whether this class is serializable.
59       */
60      private boolean isSerializable = true;
61      
62      /***
63       * A list of all parsed fields.
64       */
65      private final List fields = new ArrayList();
66      
67      /***
68       * The names of all managed fields this class.
69       */
70      private String[] managedFieldNames = null;
71      
72      /***
73       * The names of all fields this class.
74       */
75      private String[] fieldNames = null;
76      
77      /***
78       * Constructs a new object with the given name.
79       *
80       * @param  name  The name of the class.
81       */
82      JDOClass(String name)
83      {
84          this.name = name;
85      }
86      
87      /***
88       * Returns the name of the class.
89       *
90       * @return  The name of the class.
91       */
92      public String getName()
93      {
94          return name;
95      }
96      
97      /***
98       * Returns the modifiers of the class.
99       *
100      * @param modifiers  The modifiers of the class.
101      */
102     public void setModifiers(int modifiers)
103     {
104         this.modifiers = modifiers;
105     }
106     
107     /***
108      * Returns the modifiers of the class.
109      *
110      * @return  The modifiers of the class.
111      */
112     public int getModifiers()
113     {
114         return modifiers;
115     }
116     
117     /***
118      * Sets the superclassname. The given classname should have a canonical
119      * form (with dots). It is converted to the CM-similar notation
120      * (with slashes).
121      *
122      * @param  classname  The superclassname.
123      */
124     public void setSuperClassName(String classname)
125     {
126         superClassName = NameHelper.fromCanonicalClassName(classname);
127     }
128     
129     /***
130      * Returns the superclassname.
131      *
132      * @return  The superclassname.
133      */
134     public String getSuperClassName()
135     {
136         return superClassName;
137     }
138     
139     /***
140      * Sets the oid classname. The given classname should have a canonical
141      * form (with dots). It is converted to the CM-similar notation
142      * (with slashes).
143      *
144      * @param  classname  The oid classname
145      */
146     public void setOidClassName(String classname)
147     {
148         oidClassName = NameHelper.fromCanonicalClassName(classname);
149     }
150     
151     /***
152      * Returns the oid classname.
153      *
154      * @return  The oid classname
155      */
156     public String getOidClassName()
157     {
158         return oidClassName;
159     }
160     
161     /***
162      * Sets the persistence modifier of the class.
163      *
164      * @param persistent  the persistence modifer
165      * @see  #isPersistent
166      */
167     public void setPersistent(boolean persistent)
168     {
169         this.isPersistent = persistent;
170     }
171     
172     /***
173      * Returns whether the class is persistent.
174      *
175      * @return  true if persistent class.
176      * @see  #isPersistent
177      */
178     public boolean isPersistent()
179     {
180         return isPersistent;
181     }
182     
183     /***
184      * Returns whether the class is transient.
185      *
186      * @return  true if transient class.
187      * @see  #isPersistent
188      */
189     public boolean isTransient()
190     {
191         return !isPersistent();
192     }
193     
194     /***
195      * Returns whether the class is serializable.
196      *
197      * @return  true if serializable class.
198      * @see  #isSerializable
199      */
200     public boolean isSerializable()
201     {
202         return isSerializable;
203     }
204     
205     /***
206      * Sets the serializable flag of the class.
207      *
208      * @param serializable  the serializable flag
209      * @see  #isSerializable
210      */
211     public void setSerializable(boolean serializable)
212     {
213         this.isSerializable = serializable;
214     }
215     /***
216      * Adds a new field.
217      *
218      * @param  field  The new field.
219      */
220     public void addField(JDOField field)
221     {
222         fields.add(field);
223     }
224     
225     /***
226      * Returns the field with the given name.
227      *
228      * @param  name  The name of the requested field.
229      * @return  The field or <code>null</code> if not found.
230      */
231     public JDOField getField(String name)
232     {
233         int idx = getIndexOfField(name);
234         return (idx > -1  ? (JDOField) fields.get(idx)  :  null);
235     }
236     
237     /***
238      * Returns the index of the field with the given name.
239      *
240      * @param  name  The name of the field.
241      * @return  The index or <code>-1</code> if the field was not found.
242      */
243     public int getIndexOfField(String name)
244     {
245         for (int i = 0; i < fields.size(); i++) {
246             JDOField field = (JDOField)fields.get(i);
247             if (field.getName().equals(name)) {
248                 return i;
249             }
250         }
251         
252         return -1;
253     }
254     
255     /***
256      * Returns all fields of the class.
257      *
258      * @return  The fields
259      */
260     public List getFields()
261     {
262         return fields;
263     }
264     
265     /***
266      * Returns the names of all fields of the class.
267      *
268      * @return  The field names
269      */
270     public String[] getFieldNames()
271     {
272         if (fieldNames == null) {
273             final int n = fields.size();
274             String[] fields = new String[n];
275             for (int i = 0; i < n; i++) {
276                 fields[i] = ((JDOField)this.fields.get(i)).getName();
277             }
278             fieldNames = fields;
279         }
280         
281         return fieldNames;
282     }
283     
284     /***
285      * Sorts the fields of this class according to the names. This method
286      * should be called if all fields are added. It is necessary to
287      * establish an order on the fields.
288      *
289      */
290     final void sortFields()
291     {
292         Collections.sort(
293             fields,
294             new Comparator() {
295                     public int compare(Object f1, Object f2)
296                     {
297                         JDOField field1 = (JDOField)f1;
298                         JDOField field2 = (JDOField)f2;
299                         //if we dont have managed fields we dont care
300                         if (!(field1.isManaged() && field2.isManaged()))
301                         {
302                             return (field1.isManaged() ? -1 : 1);
303                         }
304                         return field1.getName().compareTo(field2.getName());
305                     }
306                 });
307     }
308     
309     /***
310      * Returns the names of all managed fields this class.
311      *
312      * @return  The persistent fieldnames.
313      */
314     public String[] getManagedFieldNames()
315     {
316         if (managedFieldNames == null) {
317             final int n = fields.size();
318             List tmp = new ArrayList(n);
319             for (int i = 0; i < n; i++) {
320                 JDOField field = (JDOField)fields.get(i);
321                 if (field.isManaged()) {
322                     tmp.add(field.getName());
323                 }
324             }
325             managedFieldNames
326                 = (String[])tmp.toArray(new String[tmp.size()]);
327         }
328         
329         return managedFieldNames;
330     }
331     
332     /***
333      * Creates a string-representation for this object.
334      *
335      * @return  The string-representation of this object.
336      */
337     public String toString()
338     {
339         return ('<' + MetaDataProperties.PROPERTY_SUPER_CLASSNAME
340                 + ':' + superClassName
341                 + MetaDataProperties.PROPERTY_OID_CLASSNAME
342                 + ':' + oidClassName
343                 + ',' + MetaDataProperties.PROPERTY_ACCESS_MODIFIER
344                 + ':' + Modifier.toString(modifiers)
345                 + ',' + MetaDataProperties.PROPERTY_JDO_MODIFIER
346                 + ':' + isPersistent
347                 + ',' + "fields:" + fields + '>');
348     }
349 }