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

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2009, Sun Microsystems, 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.util;
25
26 import com.thoughtworks.xstream.converters.Converter;
27 import com.thoughtworks.xstream.converters.MarshallingContext;
28 import com.thoughtworks.xstream.converters.UnmarshallingContext;
29 import com.thoughtworks.xstream.converters.collections.AbstractCollectionConverter;
30 import com.thoughtworks.xstream.io.HierarchicalStreamReader;
31 import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
32 import com.thoughtworks.xstream.mapper.Mapper;
33 import hudson.model.Describable;
34 import hudson.model.Saveable;
35
36 import java.io.IOException;
37 import java.util.ArrayList;
38 import java.util.Collection;
39 import java.util.Iterator;
40 import java.util.List;
41
42 /**
43 * Collection whose change is notified to the parent object for persistence.
44 *
45 * @author Kohsuke Kawaguchi
46 * @since 1.MULTISOURCE
47 */
48 public class PersistedList<T> implements Iterable<T> {
49 protected final CopyOnWriteList<T> data = new CopyOnWriteList<T>();
50 protected Saveable owner = Saveable.NOOP;
51
52 protected PersistedList() {
53 }
54
55 protected PersistedList(Collection<? extends T> initialList) {
56 data.replaceBy(initialList);
57 }
58
59 public PersistedList(Saveable owner) {
60 setOwner(owner);
61 }
62
63 public void setOwner(Saveable owner) {
64 this.owner = owner;
65 }
66
67 public void add(T item) throws IOException {
68 data.add(item);
69 onModified();
70 }
71
72 public void addAll(Collection<? extends T> items) throws IOException {
73 data.addAll(items);
74 onModified();
75 }
76
77 public void replaceBy(Collection<? extends T> col) throws IOException {
78 data.replaceBy(col);
79 onModified();
80 }
81
82 public T get(int index) {
83 return data.get(index);
84 }
85
86 public <U extends T> U get(Class<U> type) {
87 for (T t : data)
88 if(type.isInstance(t))
89 return type.cast(t);
90 return null;
91 }
92
93 /**
94 * Gets all instances that matches the given type.
95 */
96 public <U extends T> List<U> getAll(Class<U> type) {
97 List<U> r = new ArrayList<U>();
98 for (T t : data)
99 if(type.isInstance(t))
100 r.add(type.cast(t));
101 return r;
102 }
103
104 public int size() {
105 return data.size();
106 }
107
108 /**
109 * Removes an instance by its type.
110 */
111 public void remove(Class<? extends T> type) throws IOException {
112 for (T t : data) {
113 if(t.getClass()==type) {
114 data.remove(t);
115 onModified();
116 return;
117 }
118 }
119 }
120
121 /**
122 * A convenience method to replace a single item.
123 *
124 * This method shouldn't be used when you are replacing a lot of stuff
125 * as copy-on-write semantics make this rather slow.
126 */
127 public void replace(T from, T to) throws IOException {
128 List<T> copy = new ArrayList<T>(data.getView());
129 for (int i=0; i<copy.size(); i++) {
130 if (copy.get(i).equals(from))
131 copy.set(i,to);
132 }
133 data.replaceBy(copy);
134 }
135
136 public boolean remove(T o) throws IOException {
137 boolean b = data.remove(o);
138 if (b) onModified();
139 return b;
140 }
141
142 public void removeAll(Class<? extends T> type) throws IOException {
143 boolean modified=false;
144 for (T t : data) {
145 if(t.getClass()==type) {
146 data.remove(t);
147 modified=true;
148 }
149 }
150 if(modified)
151 onModified();
152 }
153
154
155 public void clear() {
156 data.clear();
157 }
158
159 public Iterator<T> iterator() {
160 return data.iterator();
161 }
162
163 /**
164 * Called when a list is mutated.
165 */
166 protected void onModified() throws IOException {
167 owner.save();
168 }
169
170 /**
171 * Returns the snapshot view of instances as list.
172 */
173 public List<T> toList() {
174 return data.getView();
175 }
176
177 /**
178 * Gets all the {@link Describable}s in an array.
179 */
180 public T[] toArray(T[] array) {
181 return data.toArray(array);
182 }
183
184 public void addAllTo(Collection<? super T> dst) {
185 data.addAllTo(dst);
186 }
187
188 public boolean isEmpty() {
189 return data.isEmpty();
190 }
191
192 public boolean contains(Object item) {
193 return data.contains(item);
194 }
195
196 /**
197 * {@link Converter} implementation for XStream.
198 *
199 * Serializaion form is compatible with plain {@link List}.
200 */
201 public static class ConverterImpl extends AbstractCollectionConverter {
202 CopyOnWriteList.ConverterImpl copyOnWriteListConverter;
203
204 public ConverterImpl(Mapper mapper) {
205 super(mapper);
206 copyOnWriteListConverter = new CopyOnWriteList.ConverterImpl(mapper());
207 }
208
209 public boolean canConvert(Class type) {
210 // handle subtypes in case the onModified method is overridden.
211 return PersistedList.class.isAssignableFrom(type);
212 }
213
214 public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) {
215 for (Object o : (PersistedList) source)
216 writeItem(o, context, writer);
217 }
218
219 public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
220 CopyOnWriteList core = copyOnWriteListConverter.unmarshal(reader, context);
221
222 try {
223 PersistedList r = (PersistedList)context.getRequiredType().newInstance();
224 r.data.replaceBy(core);
225 return r;
226 } catch (InstantiationException e) {
227 InstantiationError x = new InstantiationError();
228 x.initCause(e);
229 throw x;
230 } catch (IllegalAccessException e) {
231 IllegalAccessError x = new IllegalAccessError();
232 x.initCause(e);
233 throw x;
234 }
235 }
236 }
237 }
238
239