File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\model\labels\LabelExpression.java.html

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2010, InfraDNA, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 package hudson.model.labels;
25
26 import hudson.model.Label;
27 import hudson.util.VariableResolver;
28
29 /**
30 * Boolean expression of labels.
31 *
32 * @author Kohsuke Kawaguchi
33 * @since 1.372
34 */
35 public abstract class LabelExpression extends Label {
36 protected LabelExpression(String name) {
37 super(name);
38 }
39
40 @Override
41 public String getExpression() {
42 return getDisplayName();
43 }
44
45 public static class Not extends LabelExpression {
46 public final Label base;
47
48 public Not(Label base) {
49 super('!'+paren(LabelOperatorPrecedence.NOT,base));
50 this.base = base;
51 }
52
53 @Override
54 public boolean matches(VariableResolver<Boolean> resolver) {
55 return !base.matches(resolver);
56 }
57
58 @Override
59 public <V, P> V accept(LabelVisitor<V, P> visitor, P param) {
60 return visitor.onNot(this, param);
61 }
62
63 @Override
64 public LabelOperatorPrecedence precedence() {
65 return LabelOperatorPrecedence.NOT;
66 }
67 }
68
69 /**
70 * No-op but useful for preserving the parenthesis in the user input.
71 */
72 public static class Paren extends LabelExpression {
73 public final Label base;
74
75 public Paren(Label base) {
76 super('('+base.getExpression()+')');
77 this.base = base;
78 }
79
80 @Override
81 public boolean matches(VariableResolver<Boolean> resolver) {
82 return base.matches(resolver);
83 }
84
85 @Override
86 public <V, P> V accept(LabelVisitor<V, P> visitor, P param) {
87 return visitor.onParen(this, param);
88 }
89
90 @Override
91 public LabelOperatorPrecedence precedence() {
92 return LabelOperatorPrecedence.ATOM;
93 }
94 }
95
96 /**
97 * Puts the label name into a parenthesis if the given operator will have a higher precedence.
98 */
99 static String paren(LabelOperatorPrecedence op, Label l) {
100 if (op.compareTo(l.precedence())<0)
101 return '('+l.getExpression()+')';
102 return l.getExpression();
103 }
104
105 public static abstract class Binary extends LabelExpression {
106 public final Label lhs,rhs;
107
108 public Binary(Label lhs, Label rhs, LabelOperatorPrecedence op) {
109 super(combine(lhs, rhs, op));
110 this.lhs = lhs;
111 this.rhs = rhs;
112 }
113
114 private static String combine(Label lhs, Label rhs, LabelOperatorPrecedence op) {
115 return paren(op,lhs)+op.str+paren(op,rhs);
116 }
117
118 /**
119 * Note that we evaluate both branches of the expression all the time.
120 * That is, it behaves like "a|b" not "a||b"
121 */
122 @Override
123 public boolean matches(VariableResolver<Boolean> resolver) {
124 return op(lhs.matches(resolver),rhs.matches(resolver));
125 }
126
127 protected abstract boolean op(boolean a, boolean b);
128 }
129
130 public static final class And extends Binary {
131 public And(Label lhs, Label rhs) {
132 super(lhs, rhs, LabelOperatorPrecedence.AND);
133 }
134
135 @Override
136 protected boolean op(boolean a, boolean b) {
137 return a && b;
138 }
139
140 @Override
141 public <V, P> V accept(LabelVisitor<V, P> visitor, P param) {
142 return visitor.onAnd(this, param);
143 }
144
145 @Override
146 public LabelOperatorPrecedence precedence() {
147 return LabelOperatorPrecedence.AND;
148 }
149 }
150
151 public static final class Or extends Binary {
152 public Or(Label lhs, Label rhs) {
153 super(lhs, rhs, LabelOperatorPrecedence.OR);
154 }
155
156 @Override
157 protected boolean op(boolean a, boolean b) {
158 return a || b;
159 }
160
161 @Override
162 public <V, P> V accept(LabelVisitor<V, P> visitor, P param) {
163 return visitor.onOr(this, param);
164 }
165
166 @Override
167 public LabelOperatorPrecedence precedence() {
168 return LabelOperatorPrecedence.OR;
169 }
170 }
171
172 public static final class Iff extends Binary {
173 public Iff(Label lhs, Label rhs) {
174 super(lhs, rhs, LabelOperatorPrecedence.IFF);
175 }
176
177 @Override
178 protected boolean op(boolean a, boolean b) {
179 return !(a ^ b);
180 }
181
182 @Override
183 public <V, P> V accept(LabelVisitor<V, P> visitor, P param) {
184 return visitor.onIff(this, param);
185 }
186
187 @Override
188 public LabelOperatorPrecedence precedence() {
189 return LabelOperatorPrecedence.IFF;
190 }
191 }
192
193 public static final class Implies extends Binary {
194 public Implies(Label lhs, Label rhs) {
195 super(lhs, rhs, LabelOperatorPrecedence.IMPLIES);
196 }
197
198 @Override
199 protected boolean op(boolean a, boolean b) {
200 return !a || b;
201 }
202
203 @Override
204 public <V, P> V accept(LabelVisitor<V, P> visitor, P param) {
205 return visitor.onImplies(this, param);
206 }
207
208 @Override
209 public LabelOperatorPrecedence precedence() {
210 return LabelOperatorPrecedence.IMPLIES;
211 }
212 }
213 }
214