KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hudson > util > DescribableList


1 package hudson.util;
2
3 import com.thoughtworks.xstream.converters.Converter;
4 import com.thoughtworks.xstream.converters.MarshallingContext;
5 import com.thoughtworks.xstream.converters.UnmarshallingContext;
6 import com.thoughtworks.xstream.converters.collections.AbstractCollectionConverter;
7 import com.thoughtworks.xstream.io.HierarchicalStreamReader;
8 import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
9 import com.thoughtworks.xstream.mapper.Mapper;
10 import hudson.model.Describable;
11 import hudson.model.Descriptor;
12 import hudson.model.Descriptor.FormException;
13 import hudson.maven.MavenReporterDescriptor;
14 import org.kohsuke.stapler.StaplerRequest;
15
16 import java.io.IOException JavaDoc;
17 import java.util.ArrayList JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.List JavaDoc;
20 import java.util.Map JavaDoc;
21 import java.util.Collection JavaDoc;
22
23 /**
24  * Persisted list of {@link Describable}s with some operations specific
25  * to {@link Descriptor}s.
26  *
27  * @author Kohsuke Kawaguchi
28  */

29 public class DescribableList<T extends Describable<T>, D extends Descriptor<T>> implements Iterable JavaDoc<T> {
30     private final CopyOnWriteList<T> data = new CopyOnWriteList<T>();
31     private Owner owner;
32
33     private DescribableList() {
34     }
35
36     public DescribableList(Owner owner) {
37         setOwner(owner);
38     }
39
40     public void setOwner(Owner owner) {
41         this.owner = owner;
42     }
43
44     public void add(T item) throws IOException JavaDoc {
45         data.add(item);
46         owner.save();
47     }
48
49     public T get(D descriptor) {
50         for (T t : data)
51             if(t.getDescriptor()==descriptor)
52                 return t;
53         return null;
54     }
55
56     public boolean contains(D d) {
57         return get(d)!=null;
58     }
59
60     public void remove(D descriptor) throws IOException JavaDoc {
61         for (T t : data) {
62             if(t.getDescriptor()==descriptor) {
63                 data.remove(t);
64                 owner.save();
65                 return;
66             }
67         }
68     }
69
70     public Iterator JavaDoc<T> iterator() {
71         return data.iterator();
72     }
73
74     @SuppressWarnings JavaDoc("unchecked")
75     public Map JavaDoc<D,T> toMap() {
76         return (Map JavaDoc)Descriptor.toMap(data);
77     }
78
79     /**
80      * Gets all the {@link Describable}s in an array.
81      */

82     public T[] toArray(T[] array) {
83         return data.toArray(array);
84     }
85
86     public void addAllTo(Collection JavaDoc<? super T> dst) {
87         data.addAllTo(dst);
88     }
89
90     /**
91      * Rebuilds the list by creating a fresh instances from the submitted form.
92      *
93      * <p>
94      * This method is almost always used by the owner.
95      * This method does not invoke the save method.
96      */

97     public void rebuild(StaplerRequest req, List JavaDoc<? extends Descriptor<T>> descriptors, String JavaDoc prefix) throws FormException {
98         List JavaDoc<T> newList = new ArrayList JavaDoc<T>();
99
100         for( int i=0; i< descriptors.size(); i++ ) {
101             if(req.getParameter(prefix +i)!=null) {
102                 T instance = descriptors.get(i).newInstance(req);
103                 newList.add(instance);
104             }
105         }
106
107         data.replaceBy(newList);
108     }
109
110     public interface Owner {
111         /**
112          * Called whenever the list is changed, so that it can be saved.
113          */

114         void save() throws IOException JavaDoc;
115     }
116
117     /**
118      * {@link Converter} implementation for XStream.
119      */

120     public static final class ConverterImpl extends AbstractCollectionConverter {
121         CopyOnWriteList.ConverterImpl copyOnWriteListConverter;
122
123         public ConverterImpl(Mapper mapper) {
124             super(mapper);
125             copyOnWriteListConverter = new CopyOnWriteList.ConverterImpl(mapper());
126         }
127
128         public boolean canConvert(Class JavaDoc type) {
129             return type==DescribableList.class;
130         }
131
132         public void marshal(Object JavaDoc source, HierarchicalStreamWriter writer, MarshallingContext context) {
133             for (Object JavaDoc o : (DescribableList) source)
134                 writeItem(o, context, writer);
135         }
136
137         public Object JavaDoc unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
138             CopyOnWriteList core = copyOnWriteListConverter.unmarshal(reader, context);
139
140             DescribableList r = new DescribableList();
141             r.data.replaceBy(core);
142             return r;
143         }
144     }
145 }
146
Popular Tags