Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openj9
Path: blob/master/sourcetools/objectmodel/com/ibm/j9tools/om/FeatureDefinition.java
6004 views
1
/*******************************************************************************
2
* Copyright (c) 2007, 2019 IBM Corp. and others
3
*
4
* This program and the accompanying materials are made available under
5
* the terms of the Eclipse Public License 2.0 which accompanies this
6
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
7
* or the Apache License, Version 2.0 which accompanies this distribution and
8
* is available at https://www.apache.org/licenses/LICENSE-2.0.
9
*
10
* This Source Code may also be made available under the following
11
* Secondary Licenses when the conditions for such availability set
12
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
13
* General Public License, version 2 with the GNU Classpath
14
* Exception [1] and GNU General Public License, version 2 with the
15
* OpenJDK Assembly Exception [2].
16
*
17
* [1] https://www.gnu.org/software/classpath/license.html
18
* [2] http://openjdk.java.net/legal/assembly-exception.html
19
*
20
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception
21
*******************************************************************************/
22
package com.ibm.j9tools.om;
23
24
import java.io.PrintStream;
25
import java.util.Collection;
26
import java.util.Collections;
27
import java.util.HashSet;
28
import java.util.Map;
29
import java.util.Set;
30
import java.util.TreeMap;
31
32
/**
33
* The Feature class defines the elements of a J9 Build Spec Feature. It encompasses flags and sources
34
* which are common among multiple build specifications and should be re-used.
35
*
36
* @author Gabriel Castro
37
*/
38
public class FeatureDefinition extends OMObject implements IFlagContainer, ISourceContainer {
39
protected String id;
40
protected String name;
41
protected String description;
42
private String runtime;
43
44
protected Map<String, Property> properties;
45
protected Map<String, Flag> flags;
46
protected Map<String, Source> sources;
47
48
protected boolean complete = false;
49
50
/**
51
* Retrieves the specification ID from the given file name. The file name must have the
52
* specification file extension {@link ConfigDirectory#BUILD_SPEC_FILE_EXTENSION}.
53
*
54
* @param filename the specification file
55
* @return the spec ID or <code>null</code> if the filename is not identified as a spec file
56
*/
57
public static String getIDFromFileName(String filename) {
58
if (filename != null && filename.endsWith(ConfigDirectory.FEATURE_FILE_EXTENSION)) {
59
return filename.substring(0, filename.length() - (ConfigDirectory.FEATURE_FILE_EXTENSION.length()));
60
}
61
62
return null;
63
}
64
65
/**
66
* Default constructor
67
*/
68
public FeatureDefinition() {
69
this.properties = new TreeMap<String, Property>();
70
this.flags = new TreeMap<String, Flag>();
71
this.sources = new TreeMap<String, Source>();
72
}
73
74
/**
75
* Constructor
76
*
77
* @param featureId the feature ID
78
*/
79
public FeatureDefinition(String featureId) {
80
this();
81
this.id = featureId;
82
}
83
84
/**
85
* Retrieves this feature's ID.
86
*
87
* @return the feature ID
88
*/
89
public String getId() {
90
return id;
91
}
92
93
/**
94
* Sets this feature's ID.
95
*
96
* @param id the feature ID
97
*/
98
public void setId(String id) {
99
this.id = id;
100
}
101
102
/**
103
* Retrieves this feature's description.
104
*
105
* @return the feature description
106
*/
107
public String getDescription() {
108
return description;
109
}
110
111
/**
112
* Sets this feature's description.
113
*
114
* @param description the description
115
*/
116
public void setDescription(String description) {
117
this.description = description;
118
}
119
120
/**
121
* Retrieves this feature's name.
122
*
123
* @return the feature name
124
*/
125
public String getName() {
126
return name;
127
}
128
129
/**
130
* Sets this feature's name.
131
*
132
* @param name the feature name
133
*/
134
public void setName(String name) {
135
this.name = name;
136
complete = true;
137
}
138
139
/**
140
* Sets the runtime to which this feature definition belongs.
141
*
142
* @param runtime the parent runtime
143
*/
144
public void setRuntime(String runtime) {
145
this.runtime = runtime;
146
}
147
148
/**
149
* Returns the runtime to which this feature definition belongs.
150
*
151
* @return the runtime
152
*/
153
public String getRuntime() {
154
return runtime;
155
}
156
157
/**
158
* Adds the given {@link Property} to this {@link FeatureDefinition}.
159
*
160
* @param property new property.
161
*/
162
public void addProperty(Property property) {
163
properties.put(property.getName(), property);
164
}
165
166
/**
167
* Adds the given properties to this {@link FeatureDefinition}.
168
*
169
* @param propertyNames new property names
170
*/
171
public void addAllProperties(Set<String> propertyNames) {
172
for (String name : propertyNames) {
173
properties.put(name, new Property(name, null));
174
}
175
}
176
177
/**
178
* Removes the given property from this {@link FeatureDefinition}.
179
*
180
* @param property the property to be removed
181
*/
182
public void removeProperty(Property property) {
183
properties.remove(property.getName());
184
}
185
186
/**
187
* Removes the property with the given name from this {@link FeatureDefinition}.
188
*
189
* @param propertyName the name of the property to be removed.
190
*/
191
public void removeProperty(String propertyName) {
192
properties.remove(propertyName);
193
}
194
195
/**
196
* Determines if this {@link FeatureDefinition} has a property with the given name.
197
*
198
* @param propertyName the name of the property to check for
199
* @return <code>true</code> if this definitions has a property with the given name, <code>false</code> otherwise
200
*/
201
public boolean hasProperty(String propertyName) {
202
return properties.containsKey(propertyName);
203
}
204
205
/**
206
* Retrieves the property with the given name.
207
*
208
* @param propertyName the name of the property to be retrieved
209
* @return the property retrieves or null if no property with that name exists
210
*/
211
public Property getProperty(String propertyName) {
212
return properties.get(propertyName);
213
}
214
215
/**
216
* Retrieves all the properties from this {@link FeatureDefinition}.
217
*
218
* @return the properties
219
*/
220
public Map<String, Property> getProperties() {
221
return Collections.unmodifiableMap(properties);
222
}
223
224
/**
225
* @see com.ibm.j9tools.om.IFlagContainer#addFlag(com.ibm.j9tools.om.Flag)
226
*/
227
public void addFlag(Flag flag) {
228
flags.put(flag.getId(), flag);
229
}
230
231
/**
232
* Adds the given {@link Flag} map to this {@link FeatureDefinition}.
233
*
234
* @param flags the flags to be added
235
*/
236
public void addAllFlags(Map<String, Flag> flags) {
237
this.flags.putAll(flags);
238
}
239
240
/**
241
* @see com.ibm.j9tools.om.IFlagContainer#removeFlag(com.ibm.j9tools.om.Flag)
242
*/
243
public void removeFlag(Flag flag) {
244
flags.remove(flag.getId());
245
}
246
247
/**
248
* @see com.ibm.j9tools.om.IFlagContainer#removeFlag(java.lang.String)
249
*/
250
public void removeFlag(String flagId) {
251
flags.remove(flagId);
252
}
253
254
/**
255
* @see com.ibm.j9tools.om.IFlagContainer#hasFlag(java.lang.String)
256
*/
257
public boolean hasFlag(String flagId) {
258
return hasLocalFlag(flagId);
259
}
260
261
/**
262
* @see com.ibm.j9tools.om.IFlagContainer#hasFlag(java.lang.String)
263
*/
264
public boolean hasLocalFlag(String flagId) {
265
return flags.containsKey(flagId);
266
}
267
268
/**
269
* @see com.ibm.j9tools.om.IFlagContainer#getFlag(java.lang.String)
270
*/
271
public Flag getFlag(String flagId) {
272
return getLocalFlag(flagId);
273
}
274
275
/**
276
* @see com.ibm.j9tools.om.IFlagContainer#getFlag(java.lang.String)
277
*/
278
public Flag getLocalFlag(String flagId) {
279
return flags.get(flagId);
280
}
281
282
/**
283
* @see com.ibm.j9tools.om.IFlagContainer#getFlags()
284
*/
285
public Map<String, Flag> getFlags() {
286
return getLocalFlags();
287
}
288
289
/**
290
* @see com.ibm.j9tools.om.IFlagContainer#getFlags(java.lang.String)
291
*/
292
public Map<String, Flag> getFlags(String category) {
293
Map<String, Flag> allFlags = new TreeMap<String, Flag>();
294
295
for (Flag flag : getFlags().values()) {
296
if (flag.getCategory().equals(category)) {
297
allFlags.put(flag.getId(), flag);
298
}
299
}
300
301
return Collections.unmodifiableMap(allFlags);
302
}
303
304
/**
305
* @see com.ibm.j9tools.om.IFlagContainer#getFlags()
306
*/
307
public Map<String, Flag> getLocalFlags() {
308
return Collections.unmodifiableMap(flags);
309
}
310
311
/**
312
* @see com.ibm.j9tools.om.IFlagContainer#getLocalFlags(java.lang.String)
313
*/
314
public Map<String, Flag> getLocalFlags(String category) {
315
Map<String, Flag> allFlags = new TreeMap<String, Flag>();
316
317
for (Flag flag : flags.values()) {
318
if (flag.getCategory().equals(category)) {
319
allFlags.put(flag.getId(), flag);
320
}
321
}
322
323
return Collections.unmodifiableMap(allFlags);
324
}
325
326
/**
327
* Adds the given source to this feature.
328
*
329
* @param source the source to be added
330
*/
331
public void addSource(Source source) {
332
sources.put(source.getId(), source);
333
}
334
335
/**
336
* Adds the given sources to this {@link FeatureDefinition}.
337
*
338
* @param sourceNames new source names
339
*/
340
public void addAllSources(Set<String> sourceNames) {
341
for (String name : sourceNames) {
342
sources.put(name, new Source(name));
343
}
344
}
345
346
/**
347
* Removes the given source from this feature.
348
*
349
* @param source the source to be removed
350
*/
351
public void removeSource(Source source) {
352
sources.remove(source.getId());
353
}
354
355
/**
356
* Removes the source with the given ID from this feature.
357
*
358
* @param sourceId the source ID
359
*/
360
public void removeSource(String sourceId) {
361
sources.remove(sourceId);
362
}
363
364
/**
365
* Determines if the given source is defined in this feature.
366
*
367
* @param sourceId the source ID
368
* @return <code>true</code> if this feature contains the source, <code>false</code> otherwise
369
*/
370
public boolean hasSource(String sourceId) {
371
return hasLocalSource(sourceId);
372
}
373
374
/**
375
* Checks for existence of a local source.
376
*
377
* @param sourceId ID of the source to check for
378
* @return <code>true</code> if the source is included for this container, <code>false</code> otherwise
379
*/
380
public boolean hasLocalSource(String sourceId) {
381
return sources.containsKey(sourceId);
382
}
383
384
/**
385
* @see com.ibm.j9tools.om.ISourceContainer#getSource(java.lang.String)
386
*/
387
public Source getSource(String sourceId) {
388
return getLocalSource(sourceId);
389
}
390
391
/**
392
* Retrieves the sources with the given ID.
393
*
394
* @param sourceId the source ID
395
* @return the source with the given ID or null if the source in not part of this feature
396
*/
397
public Source getLocalSource(String sourceId) {
398
return sources.get(sourceId);
399
}
400
401
/**
402
* Retrieves all the sources for this feature.
403
*
404
* @return the {@link Map} of sources
405
*/
406
public Map<String, Source> getSources() {
407
return getLocalSources();
408
}
409
410
/**
411
* @see com.ibm.j9tools.om.ISourceContainer#getLocalSources()
412
*/
413
public Map<String, Source> getLocalSources() {
414
return Collections.unmodifiableMap(sources);
415
}
416
417
/**
418
* Determines if this {@link FeatureDefinition} is fully defined.
419
*
420
* @return returns true if all elements are defined
421
*/
422
public boolean isComplete() {
423
return complete;
424
}
425
426
/**
427
* Verifies the validity of this feature by checking its sources and flags. Sources are verified by a
428
* {@link SourceVerifier} and flags by a {@link FlagVerifier}.
429
*
430
* @throws InvalidFeatureDefinitionException thrown when source of flag errors are found
431
*/
432
public void verify(FlagDefinitions flagDefinitions, Set<String> sources) throws InvalidFeatureDefinitionException {
433
Collection<Throwable> errors = new HashSet<Throwable>();
434
435
SourceVerifier sourceVerifier = new SourceVerifier(this, sources);
436
errors.addAll(sourceVerifier.verify());
437
438
FlagVerifier flagVerifier = new FlagVerifier(this, flagDefinitions);
439
errors.addAll(flagVerifier.verify());
440
441
if (errors.size() > 0) {
442
throw new InvalidFeatureDefinitionException(errors, this);
443
}
444
}
445
446
/**
447
* Debug helper used to dump this feature's member variables and lists
448
*
449
* @param out output stream to print to
450
* @param prefix prefix to prepend to each line
451
* @param indentLevel number of spaces to append to the prefix
452
*/
453
public void dump(PrintStream out, String prefix, int indentLevel) {
454
StringBuffer indent = new StringBuffer(prefix);
455
for (int i = 0; i < indentLevel; i++) {
456
indent.append(' ');
457
}
458
459
out.println(indent + "Feature "); //$NON-NLS-1$
460
out.println(indent + " +--- id: " + this.getId()); //$NON-NLS-1$
461
out.println(indent + " |--- name: " + this.getName()); //$NON-NLS-1$
462
out.println(indent + " |--- description: " + this.getDescription()); //$NON-NLS-1$
463
464
out.println(indent + " |"); //$NON-NLS-1$
465
out.println(indent + " |--- Flags "); //$NON-NLS-1$
466
for (String flagId : flags.keySet()) {
467
Flag f = flags.get(flagId);
468
out.println(indent + " | |-- Flag"); //$NON-NLS-1$
469
out.println(indent + " | | +-- id: " + f.getId()); //$NON-NLS-1$
470
out.println(indent + " | | +-- state: " + f.getState()); //$NON-NLS-1$
471
}
472
473
out.println(indent + " |"); //$NON-NLS-1$
474
out.println(indent + " |--- Sources "); //$NON-NLS-1$
475
for (String sourceId : sources.keySet()) {
476
Source s = sources.get(sourceId);
477
out.println(indent + " | |-- Source"); //$NON-NLS-1$
478
out.println(indent + " | | +-- id: " + s.getId()); //$NON-NLS-1$
479
}
480
}
481
482
}
483
484