KickJava   Java API By Example, From Geeks To Geeks.

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


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.GenClass;
27 import org.objectweb.jorm.metainfo.api.MetaObject;
28 import org.objectweb.jorm.metainfo.api.PrimitiveElement;
29 import org.objectweb.jorm.type.api.PType;
30 import org.objectweb.jorm.util.api.Loggable;
31 import org.objectweb.util.monolog.api.BasicLevel;
32 import org.objectweb.util.monolog.api.Logger;
33 import org.objectweb.util.monolog.api.LoggerFactory;
34
35 import java.util.Iterator JavaDoc;
36 import java.util.HashMap JavaDoc;
37 import java.util.Map JavaDoc;
38 import java.util.Collection JavaDoc;
39
40 /**
41  * BasicGenClass is the implementation of the GenClass
42  * interface defined in the Meta information system.
43  * This object is defines by a name, a list of index fields,
44  * and its parent.
45  * @author X. Spengler
46  */

47 public class BasicGenClass extends BasicMetaObject implements GenClass {
48     /**
49      * name is the string representation of the name of the Generic Class
50      */

51     private String JavaDoc name;
52
53     /**
54      * the isAbstract property defines if the current generic
55      * class is abstract or not.
56      */

57     private boolean isAbstract;
58
59
60     /**
61      * indexFields is a list of index fields for the generic class.
62      * key: the name of the index field
63      * value: a PrimitiveElement
64      */

65     private Map JavaDoc indexFields;
66
67     /**
68      * Builds a new BasicGenClass object.
69      * This object is built with its name, and its parent.
70      *
71      * @param name the name of the generic class
72      * @param isAbstract true, the generic class is an abstract class, else
73      * false, the generic class is not an abstract class
74      * @param parent the parent meta-object of the current GenClass
75      */

76     public BasicGenClass(String JavaDoc name, boolean isAbstract, MetaObject parent) {
77         super(parent);
78         this.name = name;
79         this.isAbstract = isAbstract;
80         indexFields = new HashMap JavaDoc();
81     }
82
83     ///////////////////////////////////////////////////////////////////
84
// from GenClass interface
85
///////////////////////////////////////////////////////////////////
86

87     /**
88      * Allows to know the name of the current generic class object.
89      *
90      * @return the string representation of the generic class name
91      */

92     public String JavaDoc getName() {
93         return name;
94     }
95
96     /**
97      * Returns a new PrimitiveElement object, created with its name
98      * and its type (PType).
99      * As the current Class is generic, the field of the class is an
100      * index field. If the field already exists, it is returned, else a new
101      * one is created.
102      *
103      * @param indexFieldName the name of the index field
104      * @param type the type of the index field
105      * @return a new PrimitiveField for the current generic class
106      */

107     public PrimitiveElement createIndexField(String JavaDoc indexFieldName,
108                                              PType type) {
109         if (debug)
110             logger.log(BasicLevel.DEBUG,
111                        "create a new PrimitiveElement (index field (" +
112                        indexFieldName + ")) for the current " + "GenClass (" +
113                        this.name + ")");
114
115         // verify if the object already exists in the system
116
PrimitiveElement result = (PrimitiveElement)
117                 indexFields.get(indexFieldName);
118         if (result != null) {
119             if (debug) {
120                 logger.log(BasicLevel.WARN,
121                        "attention, try to create an existing PrimitiveElement (" +
122                        name + "), return existing one");
123             }
124         } else {
125             // else create it
126
result = new BasicPrimitiveElement(
127                 indexFieldName, type, PType.NOSIZE, PType.NOSIZE, this);
128             setLoggingOnChild(result);
129             indexFields.put(indexFieldName, result);
130         }
131         return result;
132     }
133
134     /**
135      * Returns the PrimitiveElement corresponding to its name.
136      * If no field corresponds to this name, null is returned.
137      *
138      * @param indexFieldName the name of the index field
139      * @return an existing index field. If the index field does not exist,
140      * null is returned.
141      */

142     public PrimitiveElement getIndexField(String JavaDoc indexFieldName) {
143         return (PrimitiveElement) indexFields.get(indexFieldName);
144     }
145
146     /**
147      * Allows to know all the existing index field for the current generic
148      * class.
149      * This iterator contains PrimitiveElement objects. If no index field
150      * exists, an empty iterator is returned.
151      *
152      * @return an Iterator on index field (PrimitiveElement). If there is no
153      * index field to return, an empty iterator is returned.
154      */

155     public Iterator JavaDoc iterateIndexField() {
156         return indexFields.values().iterator();
157     }
158
159     /**
160      * Returns the number of index fields for the current generic class.
161      *
162      * @return the integer number of the index fields for the generic class
163      */

164     public int getIndexFieldNumber() {
165         return indexFields.size();
166     }
167
168     protected Collection JavaDoc getChildren() {
169         return indexFields.values();
170     }
171 }
172
173
Popular Tags