KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > core > ext > typeinfo > JPackage


1 /*
2  * Copyright 2006 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16 package com.google.gwt.core.ext.typeinfo;
17
18 import java.util.HashMap JavaDoc;
19 import java.util.Map JavaDoc;
20
21 /**
22  * Represents a logical package.
23  */

24 public class JPackage {
25
26   private final String JavaDoc name;
27
28   private final Map JavaDoc types = new HashMap JavaDoc();
29
30   JPackage(String JavaDoc name) {
31     this.name = name;
32   }
33
34   public JClassType findType(String JavaDoc typeName) {
35     String JavaDoc[] parts = typeName.split("\\.");
36     return findType(parts);
37   }
38
39   public JClassType findType(String JavaDoc[] typeName) {
40     return findTypeImpl(typeName, 0);
41   }
42
43   public String JavaDoc getName() {
44     return name;
45   }
46
47   public JClassType getType(String JavaDoc typeName) throws NotFoundException {
48     JClassType result = findType(typeName);
49     if (result == null) {
50       throw new NotFoundException();
51     }
52     return result;
53   }
54
55   public JClassType[] getTypes() {
56     return (JClassType[]) types.values().toArray(TypeOracle.NO_JCLASSES);
57   }
58
59   public boolean isDefault() {
60     return "".equals(name);
61   }
62
63   public String JavaDoc toString() {
64     return "package " + name;
65   }
66
67   void addType(JClassType type) {
68     types.put(type.getSimpleSourceName(), type);
69   }
70
71   JClassType findTypeImpl(String JavaDoc[] typeName, int index) {
72     JClassType found = (JClassType) types.get(typeName[index]);
73     if (found == null) {
74       return null;
75     } else if (index < typeName.length - 1) {
76       return found.findNestedTypeImpl(typeName, index + 1);
77     } else {
78       return found;
79     }
80   }
81
82   void remove(JClassType type) {
83     Object JavaDoc removed = types.remove(type.getSimpleSourceName());
84     // JDT will occasionally remove non-existent items, such as packages.
85
}
86 }
87
Popular Tags