import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.RandomAccess;
import java.util.Spliterator;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.function.UnaryOperator;
import java.util.stream.Stream;
import com.xkind.collections.util.ReadWriteLockExtend;
public class ThreadSafeList<E> extends AbstractList<E>
implements RandomAccess {
private final List<E> m_list;
private static final ReadWriteLock m_signal = new ReentrantReadWriteLock();
public ThreadSafeList() {
m_list = new ArrayList<E>();
}
public ThreadSafeList(int capacity) {
m_list = new ArrayList<E>(capacity);
}
public ThreadSafeList(Collection<E> c) {
m_list = new ArrayList<E>(c);
}
@Override
public int size() {
return ReadWriteLockExtend.performReadLock(m_signal, () -> m_list.size());
}
@Override
public boolean isEmpty() {
return ReadWriteLockExtend.performReadLock(m_signal, () -> m_list.isEmpty());
}
@Override
public boolean contains(Object o) {
return ReadWriteLockExtend.performReadLock(m_signal, () -> m_list.contains(o));
}
@Override
public Iterator<E> iterator() {
return ReadWriteLockExtend.performReadLock(m_signal, () -> revert().iterator());
}
@Override
public Object[] toArray() {
return ReadWriteLockExtend.performReadLock(m_signal, () -> m_list.toArray());
}
@Override
public <T> T[] toArray(T[] a) {
return ReadWriteLockExtend.performReadLock(m_signal, () -> m_list.toArray(a));
}
@Override
public boolean add(E e) {
return ReadWriteLockExtend.performWriteLock(m_signal, () -> m_list.add(e));
}
@Override
public boolean remove(Object o) {
return ReadWriteLockExtend.performWriteLock(m_signal, () -> m_list.remove(o));
}
@Override
public void removeRange(int fromIndex, int toIndex) {
ReadWriteLockExtend.performWriteLock(m_signal, () -> m_list.subList(fromIndex, toIndex).clear());
}
@Override
public boolean containsAll(Collection<?> c) {
return ReadWriteLockExtend.performReadLock(m_signal, () -> m_list.containsAll(c));
}
@Override
public boolean addAll(Collection<? extends E> c) {
return ReadWriteLockExtend.performWriteLock(m_signal, () -> m_list.addAll(c));
}
@Override
public boolean addAll(int index, Collection<? extends E> c) {
return ReadWriteLockExtend.performWriteLock(m_signal, () -> m_list.addAll(index, c));
}
@Override
public boolean removeAll(Collection<?> c) {
return ReadWriteLockExtend.performWriteLock(m_signal, () -> m_list.removeAll(c));
}
@Override
public boolean retainAll(Collection<?> c) {
return ReadWriteLockExtend.performReadLock(m_signal, () -> m_list.retainAll(c));
}
@Override
public void clear() {
ReadWriteLockExtend.performWriteLock(m_signal, () -> m_list.clear());
}
@Override
public E get(int index) {
return ReadWriteLockExtend.performReadLock(m_signal, () -> m_list.get(index));
}
@Override
public E set(int index, E element) {
return ReadWriteLockExtend.performWriteLock(m_signal, () -> m_list.set(index, element));
}
@Override
public void add(int index, E element) {
ReadWriteLockExtend.performWriteLock(m_signal, () -> m_list.add(index, element));
}
@Override
public E remove(int index) {
return ReadWriteLockExtend.performWriteLock(m_signal, () -> m_list.remove(index));
}
@Override
public int indexOf(Object o) {
return ReadWriteLockExtend.performReadLock(m_signal, () -> m_list.indexOf(o));
}
@Override
public int lastIndexOf(Object o) {
return ReadWriteLockExtend.performReadLock(m_signal, () -> m_list.lastIndexOf(o));
}
@Override
public ListIterator<E> listIterator() {
return ReadWriteLockExtend.performReadLock(m_signal, () -> revert().listIterator());
}
@Override
public ListIterator<E> listIterator(int index) {
return ReadWriteLockExtend.performReadLock(m_signal, () -> revert().listIterator(index));
}
@Override
public List<E> subList(int fromIndex, int toIndex) {
return ReadWriteLockExtend.performReadLock(m_signal, () -> m_list.subList(fromIndex, toIndex));
}
@Override
public void replaceAll(UnaryOperator<E> operator) {
ReadWriteLockExtend.performWriteLock(m_signal, () -> m_list.replaceAll(operator));
}
@Override
public void sort(Comparator<? super E> c) {
ReadWriteLockExtend.performWriteLock(m_signal, () -> m_list.sort(c));
}
@Override
public Spliterator<E> spliterator() {
return ReadWriteLockExtend.performReadLock(m_signal, () -> m_list.spliterator());
}
@Override
public void forEach(Consumer<? super E> action) {
ReadWriteLockExtend.performWriteLock(m_signal, () -> m_list.forEach(action));
}
@Override
public boolean removeIf(Predicate<? super E> filter) {
return ReadWriteLockExtend.performWriteLock(m_signal, () -> m_list.removeIf(filter));
}
@Override
public Stream<E> stream() {
return ReadWriteLockExtend.performReadLock(m_signal, () -> m_list.stream());
}
@Override
public Stream<E> parallelStream() {
return ReadWriteLockExtend.performReadLock(m_signal, () -> m_list.parallelStream());
}
@Override
public String toString() {
return ReadWriteLockExtend.performReadLock(m_signal, () -> revert().toString());
}
private ArrayList<E> revert() {
if (m_list instanceof ArrayList) {
return (ArrayList<E>) m_list;
} else {
return new ArrayList<E>(m_list);
}
}
}
ReadWriteLockExtend
package com.xkind.collections.util;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.function.Supplier;
public class ReadWriteLockExtend {
public static void performReadLock(final ReadWriteLock signal, final Runnable runnable) {
signal.readLock().lock();
try {
runnable.run();
} finally {
signal.readLock().unlock();
}
}
public static <T> T performReadLock(final ReadWriteLock signal, final Supplier<T> supplier) {
signal.readLock().lock();
try {
return supplier.get();
} finally {
signal.readLock().unlock();
}
}
public static void performWriteLock(final ReadWriteLock signal, final Runnable runnable) {
signal.writeLock().lock();
try {
runnable.run();
} finally {
signal.writeLock().unlock();
}
}
public static <T> T performWriteLock(final ReadWriteLock signal, final Supplier<T> supplier) {
signal.writeLock().lock();
try {
return supplier.get();
} finally {
signal.writeLock().unlock();
}
}
}