File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\slaves\NodeList.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.slaves;
25
26 import com.thoughtworks.xstream.XStream;
27 import com.thoughtworks.xstream.converters.Converter;
28 import com.thoughtworks.xstream.converters.MarshallingContext;
29 import com.thoughtworks.xstream.converters.UnmarshallingContext;
30 import com.thoughtworks.xstream.io.HierarchicalStreamReader;
31 import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
32 import hudson.model.Node;
33 import hudson.util.RobustCollectionConverter;
34
35 import java.util.ArrayList;
36 import java.util.Arrays;
37 import java.util.Collection;
38 import java.util.HashMap;
39 import java.util.List;
40 import java.util.Map;
41 import java.util.concurrent.CopyOnWriteArrayList;
42 import javax.annotation.CheckForNull;
43
44 /**
45 * {@link CopyOnWriteArrayList} for {@link Node} that has special serialization semantics
46 * of not serializing {@link EphemeralNode}s.
47 *
48 * @author Kohsuke Kawaguchi
49 */
50 public final class NodeList extends ArrayList<Node> {
51
52 private Map<String,Node> map = new HashMap<String, Node>();
53
54 public NodeList() {
55 }
56
57 public NodeList(Collection<? extends Node> c) {
58 super(c);
59 for (Node node: c) {
60 if (map.put(node.getNodeName(), node) != null) {
61 // make sure that all names are unique
62 throw new IllegalArgumentException(node.getNodeName()+" is defined more than once");
63 }
64 }
65 }
66
67 public NodeList(Node... toCopyIn) {
68 this(Arrays.asList(toCopyIn));
69 }
70
71 public @CheckForNull Node getNode(String nodeName) {
72 return map.get(nodeName);
73 }
74
75
76 @Override
77 public void add(int index, Node element) {
78 throw new UnsupportedOperationException("unmodifiable list");
79 }
80
81 @Override
82 public Node remove(int index) {
83 throw new UnsupportedOperationException("unmodifiable list");
84 }
85
86 @Override
87 public boolean remove(Object o) {
88 throw new UnsupportedOperationException("unmodifiable list");
89 }
90
91 @Override
92 public void clear() {
93 throw new UnsupportedOperationException("unmodifiable list");
94 }
95
96 @Override
97 public boolean addAll(Collection<? extends Node> c) {
98 throw new UnsupportedOperationException("unmodifiable list");
99 }
100
101 @Override
102 public boolean addAll(int index, Collection<? extends Node> c) {
103 throw new UnsupportedOperationException("unmodifiable list");
104 }
105
106 @Override
107 protected void removeRange(int fromIndex, int toIndex) {
108 throw new UnsupportedOperationException("unmodifiable list");
109 }
110
111 @Override
112 public boolean removeAll(Collection<?> c) {
113 throw new UnsupportedOperationException("unmodifiable list");
114 }
115
116 @Override
117 public boolean retainAll(Collection<?> c) {
118 throw new UnsupportedOperationException("unmodifiable list");
119 }
120
121 @Override
122 public boolean add(Node node) {
123 throw new UnsupportedOperationException("unmodifiable list");
124 }
125
126 @Override
127 public Node set(int index, Node element) {
128 throw new UnsupportedOperationException("unmodifiable list");
129 }
130
131 /**
132 * {@link Converter} implementation for XStream.
133 *
134 * Serialization form is compatible with plain {@link List}.
135 */
136 public static final class ConverterImpl extends RobustCollectionConverter {
137 public ConverterImpl(XStream xstream) {
138 super(xstream);
139 }
140
141 @Override
142 public boolean canConvert(Class type) {
143 return type==NodeList.class;
144 }
145
146 @Override
147 public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) {
148 for (Node o : (NodeList) source) {
149 if(o instanceof EphemeralNode)
150 continue; // skip
151 writeItem(o, context, writer);
152 }
153 }
154
155 @Override
156 protected Object createCollection(Class type) {
157 return new ArrayList();
158 }
159
160 @Override
161 public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
162 return new NodeList((List<Node>)super.unmarshal(reader, context));
163 }
164 }
165 }
166