KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jorm > metainfo > lib > BasicCompositeName


1 /**
2  * JORM: an implementation of a generic mapping system for persistent Java
3  * objects. Two mapping are supported: to RDBMS and to binary files.
4  * Copyright (C) 2001-2003 France Telecom R&D - INRIA
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * Contact: jorm-team@objectweb.org
21  *
22  */

23
24 package org.objectweb.jorm.metainfo.lib;
25
26 import org.objectweb.jorm.metainfo.api.CompositeName;
27 import org.objectweb.jorm.metainfo.api.MetaObject;
28 import org.objectweb.jorm.metainfo.api.ScalarField;
29 import org.objectweb.jorm.metainfo.api.Package;
30 import org.objectweb.jorm.type.api.PType;
31 import org.objectweb.util.monolog.api.BasicLevel;
32
33 import java.util.ArrayList JavaDoc;
34 import java.util.HashMap JavaDoc;
35 import java.util.Iterator JavaDoc;
36 import java.util.Map JavaDoc;
37 import java.util.Collection JavaDoc;
38 import java.util.Collections JavaDoc;
39
40 /**
41  * The BasicCompositeName provides an implementation of the composite name.
42  * Composite Name are used to define composite pname.
43  * Composite Name defines the multiple fields describing the structure os a name.
44  * @author N. De Palma
45  */

46 public class BasicCompositeName extends BasicMetaObject implements CompositeName {
47
48     /**
49      * inheritedCompositeNames contains the list of inherited composited name for this composite name.
50      */

51     private ArrayList JavaDoc inheritedCompositeNames;
52
53     /**
54      * scalarFields defines a list of field defining the structure of this pname.
55      * key: the name of the field
56      * value: a scalarfield
57      */

58     private Map JavaDoc scalarFields;
59
60     /**
61      * the name of this compositename.
62      */

63     private String JavaDoc name = null;
64
65     /**
66      * Builds a new CompositeName object.
67      * This object is mainly defined by its name.
68      *
69      * @param className the name of the current class
70      * @param parent the parent of the current object
71      */

72     public BasicCompositeName(String JavaDoc className,
73                               MetaObject parent) {
74         super(parent);
75         this.name = className;
76         scalarFields = new HashMap JavaDoc(5);
77         inheritedCompositeNames = new ArrayList JavaDoc();
78     }
79
80     /**
81      * Allows to know the name of the current CompositeName object.
82      * @return the string representation of the name of the CompositeName
83      */

84     public String JavaDoc getName() {
85         return name;
86     }
87
88     /**
89      * Allows to know the fully qualifed name of the current CompositeName object.
90      * @return the string representation of the name of the CompositeName
91      */

92     public String JavaDoc getFQName() {
93         String JavaDoc res = ((Package JavaDoc) parent).getName();
94         if (res == null || res.length() == 0)
95             return name;
96         else
97             return res + "." + name;
98     }
99
100     /**
101      * Add an inherited composite name to the current composite name.
102      * @param cn the name of the composite name to be inherited.
103      */

104     public void addInheritedCompositeName(CompositeName cn) {
105         if (debug && cn != null)
106             logger.log(BasicLevel.DEBUG,
107                        "The current compositename (" + name + ") inherits from the composite name (" + cn.getName() + ")");
108         if (cn != null)
109             inheritedCompositeNames.add(cn);
110     }
111
112     public Iterator JavaDoc iterateInheritedCompositeName() {
113         return getSuperCompositeNames().iterator();
114     }
115
116     public Collection JavaDoc getSuperCompositeNames() {
117         return inheritedCompositeNames;
118     }
119
120     /**
121      * Retrieve an inherited composite name form its name.
122      * @param name the name of the composite name
123      * @return the composite name
124      */

125     public CompositeName getInheritedCompositeName(String JavaDoc name) {
126
127         CompositeName res = null;
128         boolean found = false;
129
130         Iterator JavaDoc iter = iterateInheritedCompositeName();
131         while (iter.hasNext() && !found) {
132             res = (CompositeName) iter.next();
133             if (res.getName().equals(name)) found = true;
134         }
135         if (found) return res;
136         return null;
137
138     }
139
140     /**
141      * Retrieve a scalarfield describing the field identified by it name in the current compositename.
142      * @param fieldname the name of the field.
143      * @return the scalarfield describing the field.
144      */

145     public ScalarField getScalarField(String JavaDoc fieldname) {
146         if (debug) {
147             logger.log(BasicLevel.DEBUG,
148                    "try to return field (" + fieldname + ")");
149         }
150         // this method finds in the 3 structures where is the field
151
// fieldName could be a PrimitiveElement
152
ScalarField res = (ScalarField) scalarFields.get(fieldname);
153         if (debug && res == null) {
154             // attention, the fieldname does not exist. where is it ?
155
logger.log(BasicLevel.DEBUG, "the field (" + fieldname
156                 + ") is not defined in the current class");
157         }
158         return res;
159     }
160
161     /**
162      * Returns the number of inherited composite name for the current composite name object.
163      * @return the number of inherited compositename.
164      */

165     public int getInheritedCompositeNameNumber() {
166         return inheritedCompositeNames.size();
167     }
168
169     /**
170      * Build a new scalarfield that describes a field in the composite name
171      * @param fieldname the name of the field
172      * @param type the ptype of the field
173      * @return the scalarfield
174      */

175     public ScalarField createCompositeNameField(String JavaDoc fieldname, PType type, int size, int scale) {
176         if (debug)
177             logger.log(BasicLevel.DEBUG,
178                        "create a new NameField (" + fieldname + ") for the current CompositeName (" + name + ")");
179
180         // verify if the field is not yet in the database
181
ScalarField result = (ScalarField) scalarFields.get(fieldname);
182         if (result != null) {
183             if (logger != null) {
184                 logger.log(BasicLevel.WARN,
185                        "attention, try to create an existing ScalarField (" +
186                        fieldname + "), return existing one");
187             }
188         } else {
189             // else create a new one
190
result = new BasicScalarField(fieldname, type, size, scale, this);
191             setLoggingOnChild(result);
192             scalarFields.put(fieldname, result);
193         }
194         return result;
195     }
196
197     /**
198      * Give the number of fields composing the current composite name.
199      * @return the fields number.
200      */

201     public int getFieldNumber() {
202         return scalarFields.size();
203     }
204
205     /**
206      * Provides an iterator over the field of the current composite name.
207      * @return the iterator embeding scalarfield object
208      */

209     public Iterator JavaDoc iterateField() {
210         return scalarFields.values().iterator();
211     }
212
213     public Collection JavaDoc getFields() {
214         return scalarFields.values();
215     }
216
217     public Collection JavaDoc getAllFields() {
218         ArrayList JavaDoc allFields = new ArrayList JavaDoc();
219         allFields.addAll(scalarFields.values());
220
221         // and add the fields from the inherited classes
222
for (Iterator JavaDoc iter = inheritedCompositeNames.iterator(); iter.hasNext();) {
223             CompositeName aCN = (CompositeName) iter.next();
224             allFields.addAll(aCN.getAllFields());
225         }
226         Collections.sort(allFields, FieldComparator.instance);
227         return allFields;
228     }
229
230     /**
231      * Provides an iterator over the field of the current composite name including the inherited fields.
232      * @return the iterator embeding scalarfield object
233      */

234     public Iterator JavaDoc iterateAllField() {
235         return getAllFields().iterator();
236     }
237
238     /**
239      * @deprecated use getAllFields()
240      */

241     public Collection JavaDoc getAllField() {
242         return getAllFields();
243     }
244
245     protected Collection JavaDoc getChildren() {
246         return getAllFields();
247     }
248 }
249
Popular Tags