diff --git a/src/main/java/org/redisson/RedissonList.java b/src/main/java/org/redisson/RedissonList.java index 00c873b76..f95a82b1a 100644 --- a/src/main/java/org/redisson/RedissonList.java +++ b/src/main/java/org/redisson/RedissonList.java @@ -223,6 +223,10 @@ public class RedissonList extends RedissonExpirable implements RList { @Override public V get(final int index) { checkIndex(index); + return getValue(index); + } + + private V getValue(final int index) { return connectionManager.read(new ResultOperation() { @Override protected Future execute(RedisAsyncConnection async) { @@ -374,50 +378,69 @@ public class RedissonList extends RedissonExpirable implements RList { public ListIterator listIterator(final int ind) { return new ListIterator() { + private V prevCurrentValue; + private V nextCurrentValue; + private V currentValueHasRead; private int currentIndex = ind - 1; private boolean removeExecuted; @Override public boolean hasNext() { - int size = size(); - return currentIndex+1 < size && size > 0; + V val = RedissonList.this.getValue(currentIndex+1); + if (val != null) { + nextCurrentValue = val; + } + return val != null; } @Override public V next() { - if (!hasNext()) { + if (nextCurrentValue == null && !hasNext()) { throw new NoSuchElementException("No such element at index " + currentIndex); } currentIndex++; + currentValueHasRead = nextCurrentValue; + nextCurrentValue = null; removeExecuted = false; - return RedissonList.this.get(currentIndex); + return currentValueHasRead; } @Override public void remove() { + if (currentValueHasRead == null) { + throw new IllegalStateException("Neither next nor previous have been called"); + } if (removeExecuted) { throw new IllegalStateException("Element been already deleted"); } - RedissonList.this.remove(currentIndex); + RedissonList.this.remove(currentValueHasRead); currentIndex--; removeExecuted = true; + currentValueHasRead = null; } @Override public boolean hasPrevious() { - int size = size(); - return currentIndex-1 < size && size > 0 && currentIndex >= 0; + if (currentIndex < 0) { + return false; + } + V val = RedissonList.this.getValue(currentIndex); + if (val != null) { + prevCurrentValue = val; + } + return val != null; } @Override public V previous() { - if (!hasPrevious()) { + if (prevCurrentValue == null && !hasPrevious()) { throw new NoSuchElementException("No such element at index " + currentIndex); } - removeExecuted = false; - V res = RedissonList.this.get(currentIndex); currentIndex--; - return res; + removeExecuted = false; + currentValueHasRead = prevCurrentValue; + prevCurrentValue = null; + return currentValueHasRead; } @Override