提交 1a96978a 编写于 作者: K kohsuke

added a pseudo list implementation as this is often useful.

Using java.util.concurrent.CopyOnWriteList would be problematic because of retrotranslator.


git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@1638 71c3de6d-444a-0410-be80-ed276b4c234a
上级 b13f61f2
package hudson.util;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
/**
* {@link List}-like implementation that has copy-on-write semantics.
*
* <p>
* This class is suitable where the write operation is relatively uncommon.
*
* @author Kohsuke Kawaguchi
*/
public class CopyOnWriteList<E> implements Iterable<E> {
private volatile List<E> core;
public CopyOnWriteList(List<E> core) {
this.core = new ArrayList<E>(core);
}
public CopyOnWriteList() {
this.core = Collections.emptyList();
}
public synchronized void add(E e) {
List<E> n = new ArrayList<E>(core);
n.add(e);
core = n;
}
/**
* Removes an item from the list.
*
* @return
* true if the list contained the item. False if it didn't,
* in which case there's no change.
*/
public synchronized boolean remove(E e) {
List<E> n = new ArrayList<E>(core);
boolean r = n.remove(e);
core = n;
return r;
}
public Iterator<E> iterator() {
return core.iterator();
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册