forked from xjboss/KCauldronX
Use iterator for entities loop
This commit is contained in:
77
src/main/java/kcauldron/wrapper/CollectionWrapper.java
Normal file
77
src/main/java/kcauldron/wrapper/CollectionWrapper.java
Normal file
@ -0,0 +1,77 @@
|
||||
package kcauldron.wrapper;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
|
||||
public class CollectionWrapper<T, C extends Collection<T>> implements Collection<T> {
|
||||
private C mCollection;
|
||||
|
||||
public CollectionWrapper(C collection) {
|
||||
mCollection = collection;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean add(T e) {
|
||||
return mCollection.add(e);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean addAll(Collection<? extends T> c) {
|
||||
return mCollection.addAll(c);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
mCollection.clear();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean contains(Object o) {
|
||||
return mCollection.contains(o);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsAll(Collection<?> c) {
|
||||
return mCollection.containsAll(c);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return mCollection.isEmpty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<T> iterator() {
|
||||
return mCollection.iterator();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean remove(Object o) {
|
||||
return mCollection.remove(o);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean removeAll(Collection<?> c) {
|
||||
return mCollection.removeAll(c);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean retainAll(Collection<?> c) {
|
||||
return mCollection.retainAll(c);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return mCollection.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object[] toArray() {
|
||||
return mCollection.toArray();
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T[] toArray(T[] a) {
|
||||
return mCollection.toArray(a);
|
||||
}
|
||||
}
|
144
src/main/java/kcauldron/wrapper/LinkedHelper.java
Normal file
144
src/main/java/kcauldron/wrapper/LinkedHelper.java
Normal file
@ -0,0 +1,144 @@
|
||||
package kcauldron.wrapper;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.ListIterator;
|
||||
|
||||
public class LinkedHelper<T> {
|
||||
private Iterable<T> mIterable;
|
||||
private Iterator<T> mIterator;
|
||||
private int iteratorIndex;
|
||||
private T indexValue;
|
||||
|
||||
public LinkedHelper(Iterable<T> iterable) {
|
||||
mIterable = iterable;
|
||||
}
|
||||
|
||||
public T get(int index) {
|
||||
if (iteratorIndex > index || mIterator == null) {
|
||||
mIterator = mIterable.iterator();
|
||||
iteratorIndex = -1;
|
||||
}
|
||||
while (iteratorIndex < index) {
|
||||
if (mIterator.hasNext()) {
|
||||
indexValue = mIterator.next();
|
||||
iteratorIndex++;
|
||||
} else {
|
||||
throw new IndexOutOfBoundsException();
|
||||
}
|
||||
}
|
||||
return indexValue;
|
||||
}
|
||||
|
||||
public int indexOf(Object o) {
|
||||
int i = 0;
|
||||
for (T value : mIterable) {
|
||||
if (o == null ? value == null : o.equals(value))
|
||||
return i;
|
||||
i++;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
public int lastIndexOf(Object o) {
|
||||
if (!(mIterable instanceof List)) {
|
||||
throw new UnsupportedOperationException();
|
||||
|
||||
}
|
||||
ListIterator<T> iterator = ((List<T>) mIterable).listIterator();
|
||||
int i = 0;
|
||||
while (iterator.hasPrevious()) {
|
||||
T value = iterator.previous();
|
||||
if (o == null ? value == null : o.equals(value))
|
||||
return i;
|
||||
i++;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
public ListIterator<T> listIterator() {
|
||||
if (mIterable instanceof List) {
|
||||
return ((List<T>) mIterable).listIterator();
|
||||
}
|
||||
return new LinkedListIterator<T>(mIterable.iterator(), 0);
|
||||
}
|
||||
|
||||
public ListIterator<T> listIterator(int index) {
|
||||
if (mIterable instanceof List) {
|
||||
return ((List<T>) mIterable).listIterator(index);
|
||||
}
|
||||
Iterator<T> iterator = mIterable.iterator();
|
||||
for (int i = 0; i < index; i++)
|
||||
iterator.next();
|
||||
return new LinkedListIterator<T>(iterator, index);
|
||||
}
|
||||
|
||||
private static final class LinkedListIterator<T> implements ListIterator<T> {
|
||||
private final Iterator<T> mIterator;
|
||||
private final ListIterator<T> mListIterator;
|
||||
private int mNextIndex;
|
||||
|
||||
public LinkedListIterator(Iterator<T> iterator, int index) {
|
||||
mIterator = iterator;
|
||||
mListIterator = iterator instanceof ListIterator ? (ListIterator<T>) iterator : null;
|
||||
mNextIndex = index;
|
||||
}
|
||||
|
||||
private void ensureListIterator() {
|
||||
if (mListIterator == null)
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void add(T e) {
|
||||
ensureListIterator();
|
||||
mListIterator.add(e);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return mIterator.hasNext();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPrevious() {
|
||||
ensureListIterator();
|
||||
return mListIterator.hasPrevious();
|
||||
}
|
||||
|
||||
@Override
|
||||
public T next() {
|
||||
mNextIndex++;
|
||||
return mIterator.next();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int nextIndex() {
|
||||
return mNextIndex;
|
||||
}
|
||||
|
||||
@Override
|
||||
public T previous() {
|
||||
ensureListIterator();
|
||||
return mListIterator.previous();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int previousIndex() {
|
||||
ensureListIterator();
|
||||
return mListIterator.previousIndex();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove() {
|
||||
ensureListIterator();
|
||||
mListIterator.remove();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void set(T e) {
|
||||
ensureListIterator();
|
||||
mListIterator.set(e);
|
||||
}
|
||||
}
|
||||
}
|
68
src/main/java/kcauldron/wrapper/QueueToList.java
Normal file
68
src/main/java/kcauldron/wrapper/QueueToList.java
Normal file
@ -0,0 +1,68 @@
|
||||
package kcauldron.wrapper;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.ListIterator;
|
||||
import java.util.Queue;
|
||||
|
||||
public class QueueToList<T> extends CollectionWrapper<T, Queue<T>>implements List<T> {
|
||||
private final Queue<T> mQueue;
|
||||
private final LinkedHelper<T> mHelper;
|
||||
|
||||
public QueueToList(Queue<T> queue) {
|
||||
super(queue);
|
||||
mQueue = queue;
|
||||
mHelper = new LinkedHelper<T>(mQueue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void add(int index, T element) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean addAll(int index, Collection<? extends T> c) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public T get(int index) {
|
||||
return mHelper.get(index);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int indexOf(Object o) {
|
||||
return mHelper.indexOf(o);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int lastIndexOf(Object o) {
|
||||
return mHelper.lastIndexOf(o);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ListIterator<T> listIterator() {
|
||||
return mHelper.listIterator();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ListIterator<T> listIterator(int index) {
|
||||
return mHelper.listIterator(index);
|
||||
}
|
||||
|
||||
@Override
|
||||
public T remove(int index) {
|
||||
T object = mHelper.get(index);
|
||||
return mQueue.remove(object) ? object : null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public T set(int index, T element) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<T> subList(int fromIndex, int toIndex) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user