001    /*
002      Copyright (C) 2002 Renaud Pawlak <renaud@aopsys.com>
003    
004      This program is free software; you can redistribute it and/or modify
005      it under the terms of the GNU Lesser General Public License as
006      published by the Free Software Foundation; either version 2 of the
007      License, or (at your option) any later version.
008    
009      This program is distributed in the hope that it will be useful,
010      but WITHOUT ANY WARRANTY; without even the implied warranty of
011      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012      GNU Lesser General Public License for more details.
013    
014      You should have received a copy of the GNU Lesser General Public
015      License along with this program; if not, write to the Free Software
016      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
017      USA */
018    
019    package org.objectweb.jac.ide;
020    
021    import java.util.Set;
022    import java.util.HashSet;
023    import java.util.Iterator;
024    
025    /**
026     * A repository for all the types that are not user-defined
027     * classes. */
028    
029    public class TypeRepository {
030    
031        /**
032         * Resolve a type from its name (use for primitive types only).
033         *
034         * @param name the type name (e.g. int) */
035    
036        public Type resolveType(String name) {
037            return resolveType(name,"");
038        }
039    
040        /**
041         * Resolve a type from its name and path.
042         *
043         * @param name the type name (e.g. Vector)
044         * @param path the type path (e.g. java.util) */
045    
046        public Type resolveType(String name,String path) {
047            Iterator it = primitiveTypes.iterator();
048            while(it.hasNext()) {
049                Type type = (Type)it.next();
050                if (type.getName().equals(name)) {
051                    return type;
052                }
053            } 
054    
055            it = extendedTypes.iterator();
056            while(it.hasNext()) {
057                Type type = (Type)it.next();
058                if (type.getName().equals(name)) {
059                    return type;
060                }
061            } 
062    
063            it = externalClasses.iterator();
064            while(it.hasNext()) {
065                Type type = (Type)it.next();
066                if (path.equals("") && type.getName().equals(name)) {
067                    return type;
068                } else if (path.equals(type.getPackagePath()) && 
069                           type.getName().equals(name)) {
070                    return type;
071                }
072            }
073            return null;
074        }
075    
076        HashSet primitiveTypes = new HashSet();
077       
078        /**
079         * Get all the primitive types.
080         * @return value of primitiveTypes.
081         */
082        public Set getPrimitiveTypes() {
083            return primitiveTypes;
084        }
085    
086        /**
087         * Add a primitive type in the repository.
088         *
089         * @param type the primitive type 
090         */
091        public void addPrimitiveType(Type type) {
092            if (!containsType(type))
093                primitiveTypes.add(type);
094        }
095        public void removePrimitievType(Type type) {
096            primitiveTypes.remove(type);
097        }
098    
099        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 externalClasses = new HashSet();
124       
125        /**
126         * Get the external classes.
127         * @return value of externalClasses.
128         */
129        public Set 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 extendedTypes = new HashSet();
153        public Set 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 enumeratedTypes = new HashSet();
165        public Set 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    }