Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openj9
Path: blob/master/sourcetools/com.ibm.uma/com/ibm/uma/om/PredicateList.java
6004 views
1
/*******************************************************************************
2
* Copyright (c) 2001, 2017 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.uma.om;
23
24
import java.util.Vector;
25
26
import com.ibm.uma.UMA;
27
import com.ibm.uma.UMAException;
28
29
30
public class PredicateList {
31
Vector<Predicate> predicates = new Vector<Predicate>();
32
String containingFile;
33
boolean cachedResult;
34
boolean resultHasBeenCached = false;
35
36
public PredicateList(String containingFile) {
37
this.containingFile = containingFile;
38
}
39
40
public String getContainingFile() {
41
return containingFile;
42
}
43
44
public boolean evaluate() throws UMAException {
45
if ( resultHasBeenCached ) {
46
return cachedResult;
47
}
48
// if the there are no predicates return true;
49
if ( predicates.size() < 1 ) {
50
cachedResult = true;
51
resultHasBeenCached = true;
52
return cachedResult;
53
}
54
55
// the last true result in the list wins.
56
// if the last true result is for an exclude-if then return false
57
// if the last true result is for an include-if then return true
58
// if no predicates evaluated to true then
59
// - return false if all predicates were include-if
60
// - return true if all predicates were exclude-if
61
// - return false otherwise
62
boolean includeIfPresent = false;
63
for ( int i=predicates.size()-1; i>=0; i-- ) {
64
Predicate predicate = predicates.elementAt(i);
65
if ( evalutePredicate( predicate.getPredicate() ) ) {
66
switch ( predicate.getType() ) {
67
case Predicate.EXCLUDE_IF: {
68
cachedResult = false;
69
resultHasBeenCached = true;
70
return cachedResult;
71
}
72
case Predicate.INCLUDE_IF: {
73
cachedResult = true;
74
resultHasBeenCached = true;
75
return cachedResult;
76
}
77
}
78
} else if ( predicate.getType() == Predicate.INCLUDE_IF ) {
79
includeIfPresent = true;
80
}
81
}
82
83
// an include-if was present, but no condition was satisfied
84
if ( includeIfPresent ) {
85
cachedResult = false;
86
resultHasBeenCached = true;
87
return cachedResult;
88
}
89
90
// no include-if was present
91
cachedResult = true;
92
resultHasBeenCached = true;
93
return cachedResult;
94
}
95
96
boolean evaluateSinglePredicate(String predicate) throws UMAException {
97
return UMA.getUma().getSinglePredicateEvaluator().evaluateSinglePredicate(predicate);
98
}
99
100
boolean evalutePredicate(String predicate) throws UMAException {
101
String[] preds = predicate.split("\\s");
102
boolean result = true;
103
boolean predicateFound = false;
104
boolean andOp = false;
105
boolean orOp = false;
106
boolean xorOp = false;
107
boolean notOp = false;
108
for ( String pred : preds ) {
109
if ( pred.equalsIgnoreCase("and") ) {
110
if ( orOp || xorOp || notOp || !predicateFound ) {
111
throw new UMAException("Error: badly formed predicate [" + predicate + "] in " + containingFile );
112
}
113
andOp = true;
114
continue;
115
} else if ( pred.equalsIgnoreCase("or") ) {
116
if ( andOp || xorOp || notOp || !predicateFound ) {
117
throw new UMAException("Error: badly formed predicate [" + predicate + "] in " + containingFile );
118
}
119
orOp = true;
120
continue;
121
} else if ( pred.equalsIgnoreCase("xor") ) {
122
if ( andOp || orOp || notOp || !predicateFound ) {
123
throw new UMAException("Error: badly formed predicate [" + predicate + "] in " + containingFile );
124
}
125
xorOp = true;
126
continue;
127
}else if ( pred.equalsIgnoreCase("not") ) {
128
notOp = true;
129
continue;
130
} else {
131
predicateFound = true;
132
}
133
if ( andOp ) {
134
result = result &&
135
( notOp ? !evaluateSinglePredicate(pred) :
136
evaluateSinglePredicate(pred));
137
} else if ( orOp ) {
138
result = result ||
139
( notOp ? !evaluateSinglePredicate(pred) :
140
evaluateSinglePredicate(pred) );
141
} else if ( xorOp ) {
142
result = result ^
143
( notOp ? !evaluateSinglePredicate(pred) :
144
evaluateSinglePredicate(pred) );
145
} else {
146
result = notOp ? !evaluateSinglePredicate(pred) :
147
evaluateSinglePredicate(pred);
148
}
149
notOp = false;
150
orOp = false;
151
andOp = false;
152
xorOp = false;
153
}
154
return result;
155
}
156
157
public void add(Predicate predicate) {
158
predicates.add(predicate);
159
}
160
}
161
162