KickJava   Java API By Example, From Geeks To Geeks.

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


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.Class;
27 import org.objectweb.jorm.metainfo.api.*;
28 import org.objectweb.jorm.type.api.PType;
29 import org.objectweb.util.monolog.api.BasicLevel;
30
31 import java.util.ArrayList JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.Collection JavaDoc;
34
35 /**
36  * BasicClassRef defines a reference to a class which is class field.
37  * This object is created by the Class object to define its fields.
38  * @author X. Spengler
39  */

40 public class BasicClassRef extends BasicMetaObject
41         implements ClassRef, Comparable JavaDoc {
42     /**
43      * the name of the reference for the class field name
44      */

45     private String JavaDoc fieldName;
46
47     /**
48      * the name of the class reference
49      */

50     private Class JavaDoc currentClass;
51     /**
52      * the list of the name definition declared by the user in the
53      * current reference to the class.
54      * value: a NameDef object
55      */

56     private ArrayList JavaDoc nameDefs;
57
58
59     /**
60      * Builds a new BasicClassRef object.
61      * This object is defined by its name, the reference to the class and
62      * its parent.
63      *
64      * @param fieldName the string representation of the field name
65      * @param currentClass the reference to the class
66      * @param parent the parent of the current object
67      */

68     public BasicClassRef(String JavaDoc fieldName, Class JavaDoc currentClass, MetaObject parent) {
69         super(parent);
70         this.fieldName = fieldName;
71         this.currentClass = currentClass;
72         nameDefs = new ArrayList JavaDoc();
73     }
74
75     public int compareTo(Object JavaDoc o) {
76         return fieldName.compareTo(((TypedElement) o).getName());
77     }
78
79     ///////////////////////////////////////////////////////////////////
80
// from ClassRef interface
81
///////////////////////////////////////////////////////////////////
82

83     /**
84      * Returns the Class object which describes the ClassRef entity.
85      * This object is used as the type of a field.
86      *
87      * @return the Class Object contained by the current class ref object
88      */

89     public Class JavaDoc getMOClass() {
90         return currentClass;
91     }
92
93     /**
94      * Returns the name of the class ref.
95      *
96      * @return the name of the class reference meta object
97      */

98     public String JavaDoc getClassName() {
99         return currentClass.getName();
100     }
101
102     ///////////////////////////////////////////////////////////////////
103
// from TypedElement interface
104
///////////////////////////////////////////////////////////////////
105

106     /**
107      * Returns the name of the current typed element.
108      *
109      * @return the string representation of the typed element
110      */

111     public String JavaDoc getName() {
112         return fieldName;
113     }
114
115     /**
116      * Allows to know the type of the field.
117      *
118      * @return a PType object which defines the type of the current field
119      */

120     public PType getType() {
121         return currentClass.getPType();
122     }
123
124     ///////////////////////////////////////////////////////////////////
125
// from Reference interface
126
///////////////////////////////////////////////////////////////////
127

128     /**
129      * Creates a new NameDef object for the current reference class object.
130      * If this NameDef already exists for the current object, it is returned,
131      * in other case, a new NameDef object is created and returned.
132      *
133      * @return a new object used to describe the name projection for
134      * the current reference class, or an existing one if it
135      * already exists
136      */

137
138     public NameDef createRefNameDef() {
139         if (debug)
140             logger.log(BasicLevel.DEBUG,
141                        "create a new NameDef () for the current ClassRef (" + fieldName + ")");
142         // in the case it does not exist, we create it.
143
BasicNameDef newNameDef = new BasicNameDef(this);
144         setLoggingOnChild(newNameDef);
145         nameDefs.add(newNameDef);
146         return newNameDef;
147     }
148
149     /**
150      * Returns a NameDef object.
151      * If the namedef does not exist, null is returned.
152      *
153      * @param name the name of the NameDef object.
154      * @return an existing namedef object, else null is returned.
155      */

156     public NameDef getRefNameDef(String JavaDoc name) {
157         for (Iterator JavaDoc iter = nameDefs.iterator(); iter.hasNext();) {
158             NameDef nameDef = (NameDef) iter.next();
159             if (nameDef.getName().equals(name)) {
160                 return nameDef;
161             }
162         }
163         return null;
164     }
165
166     /**
167      * Returns an iterator on existing NameDef for the current Class.
168      * This method returns an empty iterator if no namedef exists in this Class.
169      *
170      * @return an iterator on NameDef, or an empty iterator if no namedef is
171      * defined
172      */

173
174     public Collection JavaDoc getRefNameDef() {
175         return nameDefs;
176     }
177
178     protected Collection JavaDoc getChildren() {
179         return nameDefs;
180     }
181 }
182
Popular Tags