KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hudson > util > CopyOnWriteList


1 package hudson.util;
2
3 import com.thoughtworks.xstream.alias.CannotResolveClassException;
4 import com.thoughtworks.xstream.converters.Converter;
5 import com.thoughtworks.xstream.converters.MarshallingContext;
6 import com.thoughtworks.xstream.converters.UnmarshallingContext;
7 import com.thoughtworks.xstream.converters.collections.AbstractCollectionConverter;
8 import com.thoughtworks.xstream.io.HierarchicalStreamReader;
9 import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
10 import com.thoughtworks.xstream.mapper.Mapper;
11
12 import java.util.ArrayList JavaDoc;
13 import java.util.Collections JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16 import java.util.Collection JavaDoc;
17
18 /**
19  * {@link List}-like implementation that has copy-on-write semantics.
20  *
21  * <p>
22  * This class is suitable where highly concurrent access is needed, yet
23  * the write operation is relatively uncommon.
24  *
25  * @author Kohsuke Kawaguchi
26  */

27 public class CopyOnWriteList<E> implements Iterable JavaDoc<E> {
28     private volatile List JavaDoc<? extends E> core;
29
30     public CopyOnWriteList(List JavaDoc<E> core) {
31         this(core,false);
32     }
33
34     private CopyOnWriteList(List JavaDoc<E> core, boolean noCopy) {
35         this.core = noCopy ? core : new ArrayList JavaDoc<E>(core);
36     }
37
38     public CopyOnWriteList() {
39         this.core = Collections.emptyList();
40     }
41
42     public synchronized void add(E e) {
43         List JavaDoc<E> n = new ArrayList JavaDoc<E>(core);
44         n.add(e);
45         core = n;
46     }
47
48     /**
49      * Removes an item from the list.
50      *
51      * @return
52      * true if the list contained the item. False if it didn't,
53      * in which case there's no change.
54      */

55     public synchronized boolean remove(E e) {
56         List JavaDoc<E> n = new ArrayList JavaDoc<E>(core);
57         boolean r = n.remove(e);
58         core = n;
59         return r;
60     }
61
62     /**
63      * Returns an iterator.
64      *
65      * The returned iterator doesn't support the <tt>remove</tt> operation.
66      */

67     public Iterator JavaDoc<E> iterator() {
68         final Iterator JavaDoc<? extends E> itr = core.iterator();
69         return new Iterator JavaDoc<E>() {
70             public boolean hasNext() {
71                 return itr.hasNext();
72             }
73
74             public E next() {
75                 return itr.next();
76             }
77
78             public void remove() {
79                 throw new UnsupportedOperationException JavaDoc();
80             }
81         };
82     }
83
84     /**
85      * Completely replaces this list by the contents of the given list.
86      */

87     public void replaceBy(CopyOnWriteList<? extends E> that) {
88         this.core = that.core;
89     }
90
91     /**
92      * Completely replaces this list by the contents of the given list.
93      */

94     public void replaceBy(Collection JavaDoc<? extends E> that) {
95         this.core = new ArrayList JavaDoc<E>(that);
96     }
97
98     public E[] toArray(E[] array) {
99         return core.toArray(array);
100     }
101
102     public void addAllTo(Collection JavaDoc<? super E> dst) {
103         dst.addAll(core);
104     }
105
106     /**
107      * {@link Converter} implementation for XStream.
108      */

109     public static final class ConverterImpl extends AbstractCollectionConverter {
110         public ConverterImpl(Mapper mapper) {
111             super(mapper);
112         }
113
114         public boolean canConvert(Class JavaDoc type) {
115             return type==CopyOnWriteList.class;
116         }
117
118         public void marshal(Object JavaDoc source, HierarchicalStreamWriter writer, MarshallingContext context) {
119             for (Object JavaDoc o : (CopyOnWriteList) source)
120                 writeItem(o, context, writer);
121         }
122
123         @SuppressWarnings JavaDoc("unchecked")
124         public CopyOnWriteList unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
125             // read the items from xml into a list
126
List JavaDoc items = new ArrayList JavaDoc();
127             while (reader.hasMoreChildren()) {
128                 reader.moveDown();
129                 try {
130                     Object JavaDoc item = readItem(reader, context, items);
131                     items.add(item);
132                 } catch (CannotResolveClassException e) {
133                     System.err.println("failed to locate class: "+e);
134                 }
135                 reader.moveUp();
136             }
137
138             return new CopyOnWriteList(items,true);
139         }
140     }
141 }
142
Popular Tags