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

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2011, CloudBees, 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 jenkins;
25
26 import com.google.common.collect.Lists;
27 import hudson.ExtensionComponent;
28 import hudson.ExtensionFinder;
29 import hudson.ExtensionPoint;
30 import hudson.model.Descriptor;
31 import hudson.model.Hudson;
32 import jenkins.model.Jenkins;
33
34 import java.util.Arrays;
35 import java.util.Collection;
36 import java.util.Collections;
37 import java.util.List;
38
39 /**
40 * Represents the components that's newly discovered during {@link ExtensionFinder#refresh()}.
41 *
42 * @author Kohsuke Kawaguchi
43 * @since 1.442
44 */
45 public abstract class ExtensionComponentSet {
46 /**
47 * Discover extensions of the given type.
48 *
49 * <p>
50 * This method is called only once per the given type after all the plugins are loaded,
51 * so implementations need not worry about caching.
52 *
53 * @param <T>
54 * The type of the extension points. This is not bound to {@link ExtensionPoint} because
55 * of {@link Descriptor}, which by itself doesn't implement {@link ExtensionPoint} for
56 * a historical reason.
57 * @return
58 * Can be empty but never null.
59 */
60 public abstract <T> Collection<ExtensionComponent<T>> find(Class<T> type);
61
62 /**
63 * Apply {@link ExtensionFilter}s and returns a filtered set.
64 */
65 public final ExtensionComponentSet filtered() {
66 final ExtensionComponentSet base = this;
67 return new ExtensionComponentSet() {
68 @Override
69 public <T> Collection<ExtensionComponent<T>> find(Class<T> type) {
70 List<ExtensionComponent<T>> a = Lists.newArrayList();
71 for (ExtensionComponent<T> c : base.find(type)) {
72 if (ExtensionFilter.isAllowed(type,c))
73 a.add(c);
74 }
75 return a;
76 }
77 };
78 }
79
80 /**
81 * Constant that has zero component in it.
82 */
83 public static final ExtensionComponentSet EMPTY = new ExtensionComponentSet() {
84 @Override
85 public <T> Collection<ExtensionComponent<T>> find(Class<T> type) {
86 return Collections.emptyList();
87 }
88 };
89
90 /**
91 * Computes the union of all the given delta.
92 */
93 public static ExtensionComponentSet union(final Collection<? extends ExtensionComponentSet> base) {
94 return new ExtensionComponentSet() {
95 @Override
96 public <T> Collection<ExtensionComponent<T>> find(Class<T> type) {
97 List<ExtensionComponent<T>> r = Lists.newArrayList();
98 for (ExtensionComponentSet d : base)
99 r.addAll(d.find(type));
100 return r;
101 }
102 };
103 }
104
105 public static ExtensionComponentSet union(ExtensionComponentSet... members) {
106 return union(Arrays.asList(members));
107 }
108
109 /**
110 * Wraps {@link ExtensionFinder} into {@link ExtensionComponentSet}.
111 */
112 public static ExtensionComponentSet allOf(final ExtensionFinder f) {
113 return new ExtensionComponentSet() {
114 @Override
115 public <T> Collection<ExtensionComponent<T>> find(Class<T> type) {
116 return f.find(type,Hudson.getInstance());
117 }
118 };
119 }
120 }
121