KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > ide > TypeRepository


1 /*
2   Copyright (C) 2002 Renaud Pawlak <renaud@aopsys.com>
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12   GNU Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public
15   License along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17   USA */

18
19 package org.objectweb.jac.ide;
20
21 import java.util.Set JavaDoc;
22 import java.util.HashSet JavaDoc;
23 import java.util.Iterator JavaDoc;
24
25 /**
26  * A repository for all the types that are not user-defined
27  * classes. */

28
29 public class TypeRepository {
30
31     /**
32      * Resolve a type from its name (use for primitive types only).
33      *
34      * @param name the type name (e.g. int) */

35
36     public Type resolveType(String JavaDoc name) {
37         return resolveType(name,"");
38     }
39
40     /**
41      * Resolve a type from its name and path.
42      *
43      * @param name the type name (e.g. Vector)
44      * @param path the type path (e.g. java.util) */

45
46     public Type resolveType(String JavaDoc name,String JavaDoc path) {
47         Iterator JavaDoc it = primitiveTypes.iterator();
48         while(it.hasNext()) {
49             Type type = (Type)it.next();
50             if (type.getName().equals(name)) {
51                 return type;
52             }
53         }
54
55         it = extendedTypes.iterator();
56         while(it.hasNext()) {
57             Type type = (Type)it.next();
58             if (type.getName().equals(name)) {
59                 return type;
60             }
61         }
62
63         it = externalClasses.iterator();
64         while(it.hasNext()) {
65             Type type = (Type)it.next();
66             if (path.equals("") && type.getName().equals(name)) {
67                 return type;
68             } else if (path.equals(type.getPackagePath()) &&
69                        type.getName().equals(name)) {
70                 return type;
71             }
72         }
73         return null;
74     }
75
76     HashSet JavaDoc primitiveTypes = new HashSet JavaDoc();
77    
78     /**
79      * Get all the primitive types.
80      * @return value of primitiveTypes.
81      */

82     public Set JavaDoc getPrimitiveTypes() {
83         return primitiveTypes;
84     }
85
86     /**
87      * Add a primitive type in the repository.
88      *
89      * @param type the primitive type
90      */

91     public void addPrimitiveType(Type type) {
92         if (!containsType(type))
93             primitiveTypes.add(type);
94     }
95     public void removePrimitievType(Type type) {
96         primitiveTypes.remove(type);
97     }
98
99     public Type getVoid() {
100         return resolveType("void");
101     }
102
103     public Type getInt() {
104         return resolveType("int");
105     }
106
107     public Type getLong() {
108         return resolveType("long");
109     }
110
111     public Type getBoolean() {
112         return resolveType("boolean");
113     }
114
115     public Type getDouble() {
116         return resolveType("double");
117     }
118
119     public Type getFloat() {
120         return resolveType("float");
121     }
122
123     HashSet JavaDoc externalClasses = new HashSet JavaDoc();
124    
125     /**
126      * Get the external classes.
127      * @return value of externalClasses.
128      */

129     public Set JavaDoc getExternalClasses() {
130         return externalClasses;
131     }
132
133     /**
134      * Add an external class in the repository.
135      *
136      * @param type the external class's type
137      */

138     public void addExternalClass(Type type) {
139         if (!containsType(type))
140             externalClasses.add(type);
141     }
142
143     /**
144      * Remove an external class from the repository.
145      *
146      * @param type the external class's type
147      */

148     public void removeExternalClass(Type type) {
149         externalClasses.remove(type);
150     }
151
152     HashSet JavaDoc extendedTypes = new HashSet JavaDoc();
153     public Set JavaDoc getExtendedTypes() {
154         return extendedTypes;
155     }
156     public void addExtendedType(ExtendedType type) {
157         if (!containsType(type))
158             extendedTypes.add(type);
159     }
160     public void removeExtendedType(ExtendedType type) {
161         extendedTypes.remove(type);
162     }
163
164     HashSet JavaDoc enumeratedTypes = new HashSet JavaDoc();
165     public Set JavaDoc getEnumeratedTypes() {
166         return enumeratedTypes;
167     }
168     public void addEnumeratedType(EnumeratedType type) {
169         if (!containsType(type))
170             enumeratedTypes.add(type);
171     }
172     public void removeEnumeratedType(EnumeratedType type) {
173         enumeratedTypes.remove(type);
174     }
175
176     /**
177      * Tells wether the repository contains a given type
178      */

179     public boolean containsType(Type type) {
180         return resolveType(type.getName(),type.getPackagePath())!=null;
181     }
182 }
183
Popular Tags