forked from xjboss/KCauldronX
Fix #214
This commit is contained in:
parent
ad909f2788
commit
4cfa07d415
@ -5,29 +5,27 @@ import java.util.List;
|
|||||||
import java.util.ListIterator;
|
import java.util.ListIterator;
|
||||||
|
|
||||||
public class LinkedHelper<T> {
|
public class LinkedHelper<T> {
|
||||||
private Iterable<T> mIterable;
|
private final Iterable<T> mIterable;
|
||||||
private Iterator<T> mIterator;
|
private volatile Iterator<T> mIndexIterator;
|
||||||
private int iteratorIndex;
|
private volatile int mIndex;
|
||||||
private T indexValue;
|
private volatile T mIndexValue;
|
||||||
|
|
||||||
public LinkedHelper(Iterable<T> iterable) {
|
public LinkedHelper(Iterable<T> iterable) {
|
||||||
mIterable = iterable;
|
mIterable = iterable;
|
||||||
}
|
}
|
||||||
|
|
||||||
public T get(int index) {
|
public synchronized T get(int index) {
|
||||||
if (iteratorIndex > index || mIterator == null) {
|
if (mIndexIterator == null || mIndex > index) {
|
||||||
mIterator = mIterable.iterator();
|
mIndexIterator = mIterable.iterator();
|
||||||
iteratorIndex = -1;
|
mIndex = -1;
|
||||||
}
|
}
|
||||||
while (iteratorIndex < index) {
|
if (mIndex == index) return mIndexValue;
|
||||||
if (mIterator.hasNext()) {
|
T value = null;
|
||||||
indexValue = mIterator.next();
|
while (mIndex < index) {
|
||||||
iteratorIndex++;
|
value = mIndexIterator.next();
|
||||||
} else {
|
mIndex++;
|
||||||
throw new IndexOutOfBoundsException();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return indexValue;
|
return mIndexValue = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int indexOf(Object o) {
|
public int indexOf(Object o) {
|
||||||
@ -43,7 +41,6 @@ public class LinkedHelper<T> {
|
|||||||
public int lastIndexOf(Object o) {
|
public int lastIndexOf(Object o) {
|
||||||
if (!(mIterable instanceof List)) {
|
if (!(mIterable instanceof List)) {
|
||||||
throw new UnsupportedOperationException();
|
throw new UnsupportedOperationException();
|
||||||
|
|
||||||
}
|
}
|
||||||
ListIterator<T> iterator = ((List<T>) mIterable).listIterator();
|
ListIterator<T> iterator = ((List<T>) mIterable).listIterator();
|
||||||
int i = 0;
|
int i = 0;
|
||||||
@ -114,7 +111,7 @@ public class LinkedHelper<T> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int nextIndex() {
|
public int nextIndex() {
|
||||||
return mNextIndex;
|
return mListIterator != null ? mListIterator.nextIndex() : mNextIndex;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -131,8 +128,7 @@ public class LinkedHelper<T> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void remove() {
|
public void remove() {
|
||||||
ensureListIterator();
|
mIterator.remove();
|
||||||
mListIterator.remove();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
Loading…
Reference in New Issue
Block a user