710 |
|
throw new NoSuchElementException(); |
711 |
|
final Object[] es = elements; |
712 |
|
E e = nonNullElementAt(es, cursor); |
713 |
< |
lastRet = cursor; |
714 |
< |
cursor = inc(cursor, es.length); |
713 |
> |
cursor = inc(lastRet = cursor, es.length); |
714 |
|
remaining--; |
715 |
|
return e; |
716 |
|
} |
758 |
|
throw new NoSuchElementException(); |
759 |
|
final Object[] es = elements; |
760 |
|
E e = nonNullElementAt(es, cursor); |
761 |
< |
lastRet = cursor; |
763 |
< |
cursor = dec(cursor, es.length); |
761 |
> |
cursor = dec(lastRet = cursor, es.length); |
762 |
|
remaining--; |
763 |
|
return e; |
764 |
|
} |
862 |
|
} |
863 |
|
|
864 |
|
public boolean tryAdvance(Consumer<? super E> action) { |
865 |
< |
if (action == null) |
868 |
< |
throw new NullPointerException(); |
869 |
< |
final int t, i; |
870 |
< |
if ((t = getFence()) == (i = cursor)) |
871 |
< |
return false; |
865 |
> |
Objects.requireNonNull(action); |
866 |
|
final Object[] es = elements; |
867 |
+ |
if (fence < 0) { fence = tail; cursor = head; } // late-binding |
868 |
+ |
final int i; |
869 |
+ |
if ((i = cursor) == fence) |
870 |
+ |
return false; |
871 |
+ |
E e = nonNullElementAt(es, i); |
872 |
|
cursor = inc(i, es.length); |
873 |
< |
action.accept(nonNullElementAt(es, i)); |
873 |
> |
action.accept(e); |
874 |
|
return true; |
875 |
|
} |
876 |
|
|
1103 |
|
private <T> T[] toArray(Class<T[]> klazz) { |
1104 |
|
final Object[] es = elements; |
1105 |
|
final T[] a; |
1106 |
< |
final int size = size(), head = this.head, end; |
1107 |
< |
final int len = Math.min(size, es.length - head); |
1108 |
< |
if ((end = head + size) >= 0) { |
1106 |
> |
final int head = this.head, tail = this.tail, end; |
1107 |
> |
if ((end = tail + ((head <= tail) ? 0 : es.length)) >= 0) { |
1108 |
> |
// Uses null extension feature of copyOfRange |
1109 |
|
a = Arrays.copyOfRange(es, head, end, klazz); |
1110 |
|
} else { |
1111 |
|
// integer overflow! |
1112 |
< |
a = Arrays.copyOfRange(es, 0, size, klazz); |
1113 |
< |
System.arraycopy(es, head, a, 0, len); |
1112 |
> |
a = Arrays.copyOfRange(es, 0, end - head, klazz); |
1113 |
> |
System.arraycopy(es, head, a, 0, es.length - head); |
1114 |
|
} |
1115 |
< |
if (tail < head) |
1116 |
< |
System.arraycopy(es, 0, a, len, tail); |
1115 |
> |
if (end != tail) |
1116 |
> |
System.arraycopy(es, 0, a, es.length - head, tail); |
1117 |
|
return a; |
1118 |
|
} |
1119 |
|
|