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

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi
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.util;
25
26 import java.util.Collection;
27 import java.util.Map;
28
29 /**
30 * Resolves variables to its value, while encapsulating
31 * how that resolution happens.
32 *
33 * @author Kohsuke Kawaguchi
34 */
35 public interface VariableResolver<V> {
36 /**
37 * Receives a variable name and obtains the value associated with the name.
38 *
39 * <p>
40 * This can be implemented simply on top of a {@link Map} (see {@link ByMap}), or
41 * this can be used like an expression evaluator.
42 *
43 * @param name
44 * Name of the variable to be resolved.
45 * Never null, never empty. The name shouldn't include the syntactic
46 * marker of an expression. IOW, it should be "foo" but not "${foo}".
47 * A part of the goal of this design is to abstract away the expression
48 * marker syntax.
49 * @return
50 * Object referenced by the name.
51 * Null if not found.
52 */
53 V resolve(String name);
54
55 /**
56 * Empty resolver that always returns null.
57 */
58 VariableResolver NONE = new VariableResolver() {
59 public Object resolve(String name) {
60 return null;
61 }
62 };
63
64 /**
65 * {@link VariableResolver} backed by a {@link Map}.
66 */
67 final class ByMap<V> implements VariableResolver<V> {
68 private final Map<String,V> data;
69
70 public ByMap(Map<String, V> data) {
71 this.data = data;
72 }
73
74 public V resolve(String name) {
75 return data.get(name);
76 }
77 }
78
79 /**
80 * Union of multiple {@link VariableResolver}.
81 */
82 final class Union<V> implements VariableResolver<V> {
83 private final VariableResolver<? extends V>[] resolvers;
84
85 public Union(VariableResolver<? extends V>... resolvers) {
86 this.resolvers = resolvers.clone();
87 }
88
89 public Union(Collection<? extends VariableResolver<? extends V>> resolvers) {
90 this.resolvers = resolvers.toArray(new VariableResolver[resolvers.size()]);
91 }
92
93 public V resolve(String name) {
94 for (VariableResolver<? extends V> r : resolvers) {
95 V v = r.resolve(name);
96 if(v!=null) return v;
97 }
98 return null;
99 }
100 }
101 }
102