57 * Stores all classes for each package
58 */
59 private final Map<PackageElement, SortedSet<TypeElement>> allClasses;
60
61 /**
62 * Stores ordinary classes (excluding Exceptions and Errors) for each package
63 */
64 private final Map<PackageElement, SortedSet<TypeElement>> ordinaryClasses;
65
66 /**
67 * Stores exceptions for each package
68 */
69 private final Map<PackageElement, SortedSet<TypeElement>> exceptions;
70
71 /**
72 * Stores enums for each package.
73 */
74 private final Map<PackageElement, SortedSet<TypeElement>> enums;
75
76 /**
77 * Stores annotation types for each package.
78 */
79 private final Map<PackageElement, SortedSet<TypeElement>> annotationTypes;
80
81 /**
82 * Stores errors for each package
83 */
84 private final Map<PackageElement, SortedSet<TypeElement>> errors;
85
86 /**
87 * Stores interfaces for each package
88 */
89 private final Map<PackageElement, SortedSet<TypeElement>> interfaces;
90
91 private final BaseConfiguration configuration;
92 private final Utils utils;
93 private final Comparator<Element> comparator;
94
95 /**
96 * Construct a new TypeElementCatalog.
99 */
100 public TypeElementCatalog(Iterable<TypeElement> typeElements, BaseConfiguration config) {
101 this(config);
102 for (TypeElement typeElement : typeElements) {
103 addTypeElement(typeElement);
104 }
105 }
106
107 /**
108 * Construct a new TypeElementCatalog.
109 *
110 */
111 public TypeElementCatalog(BaseConfiguration config) {
112 this.configuration = config;
113 this.utils = config.utils;
114 comparator = utils.makeGeneralPurposeComparator();
115 allClasses = new HashMap<>();
116 ordinaryClasses = new HashMap<>();
117 exceptions = new HashMap<>();
118 enums = new HashMap<>();
119 annotationTypes = new HashMap<>();
120 errors = new HashMap<>();
121 interfaces = new HashMap<>();
122 packageSet = new TreeSet<>(comparator);
123 }
124
125 /**
126 * Add the given class to the catalog.
127 *
128 * @param typeElement the TypeElement to add to the catalog.
129 */
130 public final void addTypeElement(TypeElement typeElement) {
131 if (typeElement == null) {
132 return;
133 }
134 addTypeElement(typeElement, allClasses);
135 if (utils.isOrdinaryClass(typeElement)) {
136 addTypeElement(typeElement, ordinaryClasses);
137 } else if (utils.isException(typeElement)) {
138 addTypeElement(typeElement, exceptions);
139 } else if (utils.isEnum(typeElement)) {
140 addTypeElement(typeElement, enums);
141 } else if (utils.isAnnotationType(typeElement)) {
142 addTypeElement(typeElement, annotationTypes);
143 } else if (utils.isError(typeElement)) {
144 addTypeElement(typeElement, errors);
145 } else if (utils.isInterface(typeElement)) {
146 addTypeElement(typeElement, interfaces);
147 }
148 }
149
150 /**
151 * Add the given class to the given map.
152 *
153 * @param typeElement the class to add to the catalog.
154 * @param map the Map to add the TypeElement to.
155 */
156 private void addTypeElement(TypeElement typeElement, Map<PackageElement, SortedSet<TypeElement>> map) {
157
158 PackageElement pkg = utils.containingPackage(typeElement);
159 if (utils.isSpecified(pkg) || configuration.nodeprecated && utils.isDeprecated(pkg)) {
160 // No need to catalog this class if it's package is
174
175 private SortedSet<TypeElement> getSet(Map<PackageElement, SortedSet<TypeElement>> m, PackageElement key) {
176 SortedSet<TypeElement> s = m.get(key);
177 if (s != null) {
178 return s;
179 }
180 return new TreeSet<>(comparator);
181 }
182 /**
183 * Return all of the classes specified on the command-line that belong to the given package.
184 *
185 * @param packageElement the package to return the classes for.
186 */
187 public SortedSet<TypeElement> allClasses(PackageElement packageElement) {
188 return utils.isSpecified(packageElement)
189 ? utils.getTypeElementsAsSortedSet(utils.getEnclosedTypeElements(packageElement))
190 : getSet(allClasses, packageElement);
191 }
192
193 /**
194 * Return all of the classes specified on the command-line that belong to the given package.
195 *
196 * @param packageName the name of the package specified on the command-line.
197 */
198 public SortedSet<TypeElement> allUnnamedClasses() {
199 for (PackageElement pkg : allClasses.keySet()) {
200 if (pkg.isUnnamed()) {
201 return allClasses.get(pkg);
202 }
203 }
204 return new TreeSet<>(comparator);
205 }
206
207 /**
208 * Return a SortedSet of packages that this catalog stores.
209 */
210 public SortedSet<PackageElement> packages() {
211 return packageSet;
212 }
213
214 /**
215 * Return all of the errors specified on the command-line that belong to the given package.
216 *
217 * @param packageName the name of the package specified on the command-line.
218 */
219 public SortedSet<TypeElement> errors(PackageElement pkg) {
220 return getSet(errors, pkg);
221 }
222
223 /**
224 * Return all of the exceptions specified on the command-line that belong to the given package.
225 *
226 * @param packageName the name of the package specified on the command-line.
227 */
228 public SortedSet<TypeElement> exceptions(PackageElement pkg) {
229 return getSet(exceptions, pkg);
230 }
231
232 /**
233 * Return all of the enums specified on the command-line that belong to the given package.
234 *
235 * @param packageName the name of the package specified on the command-line.
236 */
237 public SortedSet<TypeElement> enums(PackageElement pkg) {
238 return getSet(enums, pkg);
239 }
240
241 /**
242 * Return all of the annotation types specified on the command-line that belong to the given
243 * package.
244 *
245 * @param packageName the name of the package specified on the command-line.
246 */
247 public SortedSet<TypeElement> annotationTypes(PackageElement pkg) {
248 return getSet(annotationTypes, pkg);
249 }
250
251 /**
252 * Return all of the interfaces specified on the command-line that belong to the given package.
253 *
254 * @param packageName the name of the package specified on the command-line.
255 */
256 public SortedSet<TypeElement> interfaces(PackageElement pkg) {
257 return getSet(interfaces, pkg);
258 }
259
260 /**
261 * Return all of the ordinary classes specified on the command-line that belong to the given
262 * package.
263 *
264 * @param packageName the name of the package specified on the command-line.
265 */
266 public SortedSet<TypeElement> ordinaryClasses(PackageElement pkg) {
267 return getSet(ordinaryClasses, pkg);
268 }
269 }
|
57 * Stores all classes for each package
58 */
59 private final Map<PackageElement, SortedSet<TypeElement>> allClasses;
60
61 /**
62 * Stores ordinary classes (excluding Exceptions and Errors) for each package
63 */
64 private final Map<PackageElement, SortedSet<TypeElement>> ordinaryClasses;
65
66 /**
67 * Stores exceptions for each package
68 */
69 private final Map<PackageElement, SortedSet<TypeElement>> exceptions;
70
71 /**
72 * Stores enums for each package.
73 */
74 private final Map<PackageElement, SortedSet<TypeElement>> enums;
75
76 /**
77 * Stores records for each package.
78 */
79 private final Map<PackageElement, SortedSet<TypeElement>> records;
80
81 /**
82 * Stores annotation types for each package.
83 */
84 private final Map<PackageElement, SortedSet<TypeElement>> annotationTypes;
85
86 /**
87 * Stores errors for each package
88 */
89 private final Map<PackageElement, SortedSet<TypeElement>> errors;
90
91 /**
92 * Stores interfaces for each package
93 */
94 private final Map<PackageElement, SortedSet<TypeElement>> interfaces;
95
96 private final BaseConfiguration configuration;
97 private final Utils utils;
98 private final Comparator<Element> comparator;
99
100 /**
101 * Construct a new TypeElementCatalog.
104 */
105 public TypeElementCatalog(Iterable<TypeElement> typeElements, BaseConfiguration config) {
106 this(config);
107 for (TypeElement typeElement : typeElements) {
108 addTypeElement(typeElement);
109 }
110 }
111
112 /**
113 * Construct a new TypeElementCatalog.
114 *
115 */
116 public TypeElementCatalog(BaseConfiguration config) {
117 this.configuration = config;
118 this.utils = config.utils;
119 comparator = utils.makeGeneralPurposeComparator();
120 allClasses = new HashMap<>();
121 ordinaryClasses = new HashMap<>();
122 exceptions = new HashMap<>();
123 enums = new HashMap<>();
124 records = new HashMap<>();
125 annotationTypes = new HashMap<>();
126 errors = new HashMap<>();
127 interfaces = new HashMap<>();
128 packageSet = new TreeSet<>(comparator);
129 }
130
131 /**
132 * Add the given class to the catalog.
133 *
134 * @param typeElement the TypeElement to add to the catalog.
135 */
136 public final void addTypeElement(TypeElement typeElement) {
137 if (typeElement == null) {
138 return;
139 }
140 addTypeElement(typeElement, allClasses);
141 if (utils.isOrdinaryClass(typeElement)) {
142 addTypeElement(typeElement, ordinaryClasses);
143 } else if (utils.isException(typeElement)) {
144 addTypeElement(typeElement, exceptions);
145 } else if (utils.isEnum(typeElement)) {
146 addTypeElement(typeElement, enums);
147 } else if (utils.isRecord(typeElement)) {
148 addTypeElement(typeElement, records);
149 } else if (utils.isAnnotationType(typeElement)) {
150 addTypeElement(typeElement, annotationTypes);
151 } else if (utils.isError(typeElement)) {
152 addTypeElement(typeElement, errors);
153 } else if (utils.isInterface(typeElement)) {
154 addTypeElement(typeElement, interfaces);
155 }
156 }
157
158 /**
159 * Add the given class to the given map.
160 *
161 * @param typeElement the class to add to the catalog.
162 * @param map the Map to add the TypeElement to.
163 */
164 private void addTypeElement(TypeElement typeElement, Map<PackageElement, SortedSet<TypeElement>> map) {
165
166 PackageElement pkg = utils.containingPackage(typeElement);
167 if (utils.isSpecified(pkg) || configuration.nodeprecated && utils.isDeprecated(pkg)) {
168 // No need to catalog this class if it's package is
182
183 private SortedSet<TypeElement> getSet(Map<PackageElement, SortedSet<TypeElement>> m, PackageElement key) {
184 SortedSet<TypeElement> s = m.get(key);
185 if (s != null) {
186 return s;
187 }
188 return new TreeSet<>(comparator);
189 }
190 /**
191 * Return all of the classes specified on the command-line that belong to the given package.
192 *
193 * @param packageElement the package to return the classes for.
194 */
195 public SortedSet<TypeElement> allClasses(PackageElement packageElement) {
196 return utils.isSpecified(packageElement)
197 ? utils.getTypeElementsAsSortedSet(utils.getEnclosedTypeElements(packageElement))
198 : getSet(allClasses, packageElement);
199 }
200
201 /**
202 * Return all of the classes specified on the command-line that belong to the unnamed package.
203 */
204 public SortedSet<TypeElement> allUnnamedClasses() {
205 for (PackageElement pkg : allClasses.keySet()) {
206 if (pkg.isUnnamed()) {
207 return allClasses.get(pkg);
208 }
209 }
210 return new TreeSet<>(comparator);
211 }
212
213 /**
214 * Return a SortedSet of packages that this catalog stores.
215 */
216 public SortedSet<PackageElement> packages() {
217 return packageSet;
218 }
219
220 /**
221 * Return all of the errors specified on the command-line that belong to the given package.
222 *
223 * @param pkg the name of the package specified on the command-line.
224 */
225 public SortedSet<TypeElement> errors(PackageElement pkg) {
226 return getSet(errors, pkg);
227 }
228
229 /**
230 * Return all of the exceptions specified on the command-line that belong to the given package.
231 *
232 * @param pkg the name of the package specified on the command-line.
233 */
234 public SortedSet<TypeElement> exceptions(PackageElement pkg) {
235 return getSet(exceptions, pkg);
236 }
237
238 /**
239 * Return all of the enums specified on the command-line that belong to the given package.
240 *
241 * @param pkg the name of the package specified on the command-line.
242 */
243 public SortedSet<TypeElement> enums(PackageElement pkg) {
244 return getSet(enums, pkg);
245 }
246
247 /**
248 * Return all of the records specified on the command-line that belong to the given package.
249 *
250 * @param pkg the name of the package specified on the command-line.
251 */
252 public SortedSet<TypeElement> records(PackageElement pkg) {
253 return getSet(records, pkg);
254 }
255
256 /**
257 * Return all of the annotation types specified on the command-line that belong to the given
258 * package.
259 *
260 * @param pkg the name of the package specified on the command-line.
261 */
262 public SortedSet<TypeElement> annotationTypes(PackageElement pkg) {
263 return getSet(annotationTypes, pkg);
264 }
265
266 /**
267 * Return all of the interfaces specified on the command-line that belong to the given package.
268 *
269 * @param pkg the name of the package specified on the command-line.
270 */
271 public SortedSet<TypeElement> interfaces(PackageElement pkg) {
272 return getSet(interfaces, pkg);
273 }
274
275 /**
276 * Return all of the ordinary classes specified on the command-line that belong to the given
277 * package.
278 *
279 * @param pkg the name of the package specified on the command-line.
280 */
281 public SortedSet<TypeElement> ordinaryClasses(PackageElement pkg) {
282 return getSet(ordinaryClasses, pkg);
283 }
284 }
|