3
0
This commit is contained in:
Prototik
2015-10-21 23:37:43 +07:00
parent 40ac2b7e23
commit 6458549af6
2 changed files with 114 additions and 108 deletions

View File

@ -6,127 +6,119 @@ import java.util.Queue;
import java.util.concurrent.atomic.AtomicInteger;
public class ProcessingQueue<T> extends QueueWrapper<T> {
public class ProcessingIterator implements Iterator<T> {
private Iterator<T> mRealIterator = mCollection.iterator();
private AtomicInteger mPosition = new AtomicInteger(0);
public class ProcessingIterator implements Iterator<T> {
private Iterator<T> mRealIterator = mCollection.iterator();
@Override
public boolean hasNext() {
final int size = mSize.get();
final int position = mPosition.get();
if (size == 0 || position >= size) {
mPosition.compareAndSet(position, 0);
mRealIterator = mCollection.iterator();
return false;
}
boolean value = mRealIterator.hasNext();
if (!value) {
mRealIterator = mCollection.iterator();
value = mRealIterator.hasNext();
}
return value;
}
@Override
public boolean hasNext() {
return mRealIterator.hasNext();
}
@Override
public T next() {
T value = mRealIterator.next();
mPosition.incrementAndGet();
return value;
}
@Override
public T next() {
return mRealIterator.next();
}
@Override
public void remove() {
mRealIterator.remove();
mSize.decrementAndGet();
mPosition.decrementAndGet();
}
@Override
public void remove() {
mRealIterator.remove();
mSize.decrementAndGet();
}
}
public void reset() {
mPosition.set(0);
}
}
private final AtomicInteger mSize;
private final AtomicInteger mSize;
private final ProcessingIterator mIterator = new ProcessingIterator();
@Override
public boolean add(T e) {
boolean result = super.add(e);
if (result) {
mSize.incrementAndGet();
}
return result;
};
@Override
public boolean add(T e) {
boolean result = super.add(e);
if (result) {
mSize.incrementAndGet();
}
return result;
};
@Override
public boolean offer(T e) {
boolean result = super.offer(e);
if (result) {
mSize.incrementAndGet();
}
return result;
}
@Override
public boolean offer(T e) {
boolean result = super.offer(e);
if (result) {
mSize.incrementAndGet();
}
return result;
}
@Override
public boolean addAll(Collection<? extends T> c) {
boolean result = false;
for (T t : c) {
result |= add(t);
}
return result;
}
@Override
public boolean addAll(Collection<? extends T> c) {
boolean result = false;
for (T t : c) {
result |= add(t);
}
return result;
}
@Override
public T remove() {
T result = super.remove();
mSize.decrementAndGet();
return result;
}
@Override
public T remove() {
T result = super.remove();
mSize.decrementAndGet();
return result;
}
@Override
public boolean remove(Object o) {
boolean result = super.remove(o);
if (result) {
mSize.decrementAndGet();
}
return result;
}
@Override
public boolean remove(Object o) {
boolean result = super.remove(o);
if (result) {
mSize.decrementAndGet();
}
return result;
}
@Override
public boolean removeAll(Collection<?> c) {
boolean result = false;
for (Object t : c) {
result |= remove(t);
}
return result;
}
@Override
public boolean removeAll(Collection<?> c) {
boolean result = false;
for (Object t : c) {
result |= remove(t);
}
return result;
}
@Override
public boolean retainAll(Collection<?> c) {
boolean result = super.retainAll(c);
if (result) {
mSize.set(mCollection.size());
}
return result;
}
@Override
public boolean retainAll(Collection<?> c) {
boolean result = super.retainAll(c);
if (result) {
mSize.set(mCollection.size());
}
return result;
}
@Override
public T poll() {
T result = super.remove();
if (result != null) {
mSize.decrementAndGet();
}
return result;
}
@Override
public T poll() {
T result = super.remove();
if (result != null) {
mSize.decrementAndGet();
}
return result;
}
public ProcessingQueue(Queue<T> collection) {
super(collection);
mSize = new AtomicInteger(collection.size());
}
public ProcessingQueue(Queue<T> collection) {
super(collection);
mSize = new AtomicInteger(collection.size());
}
@Override
public ProcessingIterator iterator() {
return new ProcessingIterator();
}
@Override
public ProcessingIterator iterator() {
mIterator.reset();
return mIterator;
}
@Override
public String toString() {
if (size() == 0)
return "[]";
StringBuilder builder = new StringBuilder();
for (T t : this) {
builder.append(',');
builder.append(t == null ? "null" : t.getClass().getSimpleName());
}
builder.setCharAt(0, '[');
builder.append(']');
return builder.toString();
}
}