Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openj9
Path: blob/master/sourcetools/objectmodel/com/ibm/j9tools/om/FlagDefinitions.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.text.MessageFormat;
25
import java.util.Collection;
26
import java.util.Collections;
27
import java.util.HashMap;
28
import java.util.HashSet;
29
import java.util.Map;
30
import java.util.Set;
31
import java.util.TreeMap;
32
import java.util.TreeSet;
33
34
/**
35
*
36
* @author Maciek Klimkowski
37
* @author Gabriel Castro
38
*/
39
public class FlagDefinitions extends OMObject {
40
private String runtime;
41
private final Map<String, FlagDefinition> _flagDefinitions = new TreeMap<String, FlagDefinition>();
42
private final Map<String, Set<FlagDefinition>> _flagCategories = new HashMap<String, Set<FlagDefinition>>();
43
44
/**
45
* Defaults FlagDefinitions constructor
46
*/
47
public FlagDefinitions() {
48
}
49
50
/**
51
* Sets the runtime to which this flag definitions list belongs.
52
*
53
* @param runtime the parent runtime
54
*/
55
public void setRuntime(String runtime) {
56
this.runtime = runtime;
57
}
58
59
/**
60
* Returns the runtime to which this flag definition belongs to, or <code>null</code> if none.
61
*
62
* @return the runtime
63
*/
64
public String getRuntime() {
65
return runtime;
66
}
67
68
public boolean contains(String flagDefinitionId) {
69
return _flagDefinitions.containsKey(flagDefinitionId);
70
}
71
72
public boolean contains(FlagDefinition flagDefinition) {
73
return _flagDefinitions.containsValue(flagDefinition);
74
}
75
76
/**
77
* @param flagDefinition Flag Definition to be added to this flag definition list
78
*/
79
public void addFlagDefinition(FlagDefinition flagDefinition) {
80
_flagDefinitions.put(flagDefinition.getId(), flagDefinition);
81
82
Set<FlagDefinition> definitions = _flagCategories.get(flagDefinition.getCategory());
83
84
if (definitions == null) {
85
definitions = new HashSet<FlagDefinition>();
86
_flagCategories.put(flagDefinition.getCategory(), definitions);
87
}
88
89
definitions.add(flagDefinition);
90
}
91
92
/**
93
* Remove a flag definition
94
*
95
* @param flagDefinitionId ID of the flag definition
96
*/
97
public FlagDefinition removeFlagDefinition(String flagDefinitionId) {
98
FlagDefinition flagDefinition = _flagDefinitions.get(flagDefinitionId);
99
100
return (flagDefinition != null) ? removeFlagDefinition(flagDefinition) : null;
101
}
102
103
/**
104
* Remove a flag definition
105
*
106
* @param flagDefinition The FlagDefinition to remove (based on its ID)
107
*/
108
public FlagDefinition removeFlagDefinition(FlagDefinition flagDefinition) {
109
if (_flagDefinitions.containsValue(flagDefinition)) {
110
// Remove from category list
111
Set<FlagDefinition> categoryDefinitions = _flagCategories.get(flagDefinition.getCategory());
112
categoryDefinitions.remove(flagDefinition);
113
114
// Remove category if this was the last flag definition
115
if (categoryDefinitions.size() == 0) {
116
_flagCategories.remove(flagDefinition.getCategory());
117
}
118
119
// Remove any require/preclude reference to this category
120
for (FlagDefinition def : _flagDefinitions.values()) {
121
if (def.getRequires().containsKey(flagDefinition.getId())) {
122
def.removeRequires(flagDefinition.getId());
123
}
124
125
if (def.getPrecludes().containsKey(flagDefinition.getId())) {
126
def.removePrecludes(flagDefinition.getId());
127
}
128
}
129
130
return _flagDefinitions.remove(flagDefinition.getId());
131
}
132
133
return null;
134
}
135
136
/**
137
* Retrieve a FlagDefinition.
138
*
139
* @param flagDefinitionId The ID of FlagDefinition to be retrieved
140
* @return A FlagDefinition identified by flagDefinitionId argument
141
*/
142
public FlagDefinition getFlagDefinition(String flagDefinitionId) {
143
return _flagDefinitions.get(flagDefinitionId);
144
}
145
146
/**
147
* Retrieve the HashMap containing all flag definitions contained by this FlagDefinitions object.
148
*
149
* @return A HashMap with all the flag definitions that this FlagDefinitions object holds.
150
*/
151
public Map<String, FlagDefinition> getFlagDefinitions() {
152
return Collections.unmodifiableMap(_flagDefinitions);
153
}
154
155
/**
156
* Retrieves the list of {@link FlagDefinition} for the given category.
157
*
158
* @param category the category for which to retrieve flag definitions
159
* @return the list of {@link FlagDefinition}
160
*/
161
public Set<FlagDefinition> getFlagDefinitions(String category) {
162
return _flagCategories.containsKey(category) ? _flagCategories.get(category) : new HashSet<FlagDefinition>();
163
}
164
165
/**
166
* Retrieves the list of flag definitions categories.
167
*
168
* @return the list of categories
169
*/
170
public Set<String> getCategories() {
171
return _flagCategories.keySet();
172
}
173
174
/**
175
* Retrieves the list of {@link FlagDefinition} IDs that preclude the given flag ID.
176
*
177
* @param flagId the ID of the flag definition for which preclusions should be computed
178
* @return the list of precluded by flag definitions IDs
179
*/
180
public Set<String> getPrecludedBy(String flagId) {
181
Set<String> precludedBys = new TreeSet<String>();
182
183
for (FlagDefinition flagDefinition : _flagDefinitions.values()) {
184
for (FlagDefinition preclude : flagDefinition.getPrecludes().values()) {
185
if (flagId.equals(preclude.getId())) {
186
precludedBys.add(flagDefinition.getId());
187
}
188
}
189
}
190
191
return precludedBys;
192
}
193
194
/**
195
* Retrieves the list of {@link FlagDefinition} IDs that preclude the given flag.
196
*
197
* @param flag the flag definition for which preclusions should be computed
198
* @return the list of flag definitions IDs
199
*/
200
public Set<String> getPrecludedBy(FlagDefinition flag) {
201
return getPrecludedBy(flag.getId());
202
}
203
204
/**
205
* Retrieves the list of {@link FlagDefinition} IDs that require the given flag ID.
206
*
207
* @param flagId the ID of the flag definition for which dependencies should be computed
208
* @return the list of dependent flag definitions IDs
209
*/
210
public Set<String> getDependents(String flagId) {
211
Set<String> requiredBys = new TreeSet<String>();
212
213
for (FlagDefinition flagDefinition : _flagDefinitions.values()) {
214
for (FlagDefinition require : flagDefinition.getRequires().values()) {
215
if (flagId.equals(require.getId())) {
216
requiredBys.add(flagDefinition.getId());
217
}
218
}
219
}
220
221
return requiredBys;
222
}
223
224
/**
225
* Retrieves the list of {@link FlagDefinition} IDs that required the given flag.
226
*
227
* @param flag the flag definition for which dependencies should be computed
228
* @return the list of dependent flag definitions IDs
229
*/
230
public Set<String> getDependents(FlagDefinition flag) {
231
return getDependents(flag.getId());
232
}
233
234
/**
235
* Verify that the requires and precludes flag lists of each FlagDefinition refer to flag
236
* definitions that exist in _flagDefinitions.
237
*/
238
public void verify() throws InvalidFlagDefinitionsException {
239
Collection<Throwable> errors = new HashSet<Throwable>();
240
241
for (FlagDefinition flagDef : _flagDefinitions.values()) {
242
errors.addAll(verifyDefinition(flagDef));
243
}
244
245
if (errors.size() > 0) {
246
throw new InvalidFlagDefinitionsException(errors, this);
247
}
248
}
249
250
public Collection<OMException> verifyDefinition(FlagDefinition flagDef) {
251
Collection<OMException> errors = new HashSet<OMException>();
252
253
for (FlagDefinition req : flagDef.getLocalRequires().values()) {
254
if (!_flagDefinitions.containsKey(req.getId())) {
255
errors.add(new InvalidFlagException(MessageFormat.format(Messages.getString("FlagDefinitions.invalidRequires"), new Object[] { req.getId() }), flagDef)); //$NON-NLS-1$
256
}
257
}
258
259
for (FlagDefinition prec : flagDef.getLocalPrecludes().values()) {
260
if (!_flagDefinitions.containsKey(prec.getId())) {
261
errors.add(new InvalidFlagException(MessageFormat.format(Messages.getString("FlagDefinitions.invalidPrecludes"), new Object[] { prec.getId() }), flagDef)); //$NON-NLS-1$
262
}
263
}
264
265
if (flagDef.getLocalRequires().containsKey(flagDef.getId())) {
266
errors.add(new InvalidFlagException(MessageFormat.format(Messages.getString("FlagDefinitions.requireSelf"), new Object[] { flagDef.getId() }), flagDef)); //$NON-NLS-1$
267
}
268
269
if (flagDef.getLocalPrecludes().containsKey(flagDef.getId())) {
270
errors.add(new InvalidFlagException(MessageFormat.format(Messages.getString("FlagDefinitions.precludeSelf"), new Object[] { flagDef.getId() }), flagDef)); //$NON-NLS-1$
271
}
272
273
Map<String, FlagDefinition> allRequires = flagDef.getRequires();
274
Map<String, FlagDefinition> allPrecludes = flagDef.getPrecludes();
275
276
if (allPrecludes.containsKey(flagDef.getId())) {
277
errors.add(new InvalidFlagException(MessageFormat.format(Messages.getString("FlagDefinitions.precludeCycle"), new Object[] { flagDef.getId() }), flagDef)); //$NON-NLS-1$
278
}
279
280
for (FlagDefinition req : allRequires.values()) {
281
if (allPrecludes.containsKey(req.getId())) {
282
errors.add(new InvalidFlagException(MessageFormat.format(Messages.getString("FlagDefinitions.requireAndPreclude"), new Object[] { flagDef.getId(), req.getId() }), flagDef)); //$NON-NLS-1$
283
}
284
}
285
286
return errors;
287
}
288
}
289
290