683 |
|
/* -------------- Traversal methods -------------- */ |
684 |
|
|
685 |
|
/** |
686 |
+ |
* Returns the successor of p, or the head node if p.next has been |
687 |
+ |
* linked to self, which will only be true if traversing with a |
688 |
+ |
* stale pointer that is now off the list. |
689 |
+ |
*/ |
690 |
+ |
final Node succ(Node p) { |
691 |
+ |
Node next = p.next; |
692 |
+ |
return (p == next) ? head : next; |
693 |
+ |
} |
694 |
+ |
|
695 |
+ |
/** |
696 |
|
* Returns the first unmatched node of the given mode, or null if |
697 |
|
* none. Used by methods isEmpty, hasWaitingConsumer. |
698 |
|
*/ |
699 |
< |
private Node firstOfMode(boolean data) { |
700 |
< |
for (Node p = head; p != null; ) { |
699 |
> |
private Node firstOfMode(boolean isData) { |
700 |
> |
for (Node p = head; p != null; p = succ(p)) { |
701 |
|
if (!p.isMatched()) |
702 |
< |
return (p.isData == data) ? p : null; |
693 |
< |
Node n = p.next; |
694 |
< |
p = (n != p) ? n : head; |
702 |
> |
return (p.isData == isData) ? p : null; |
703 |
|
} |
704 |
|
return null; |
705 |
|
} |
709 |
|
* null if none. Used by peek. |
710 |
|
*/ |
711 |
|
private E firstDataItem() { |
712 |
< |
for (Node p = head; p != null; ) { |
705 |
< |
boolean isData = p.isData; |
712 |
> |
for (Node p = head; p != null; p = succ(p)) { |
713 |
|
Object item = p.item; |
714 |
< |
if (item != p && (item != null) == isData) |
715 |
< |
return isData ? this.<E>cast(item) : null; |
716 |
< |
Node n = p.next; |
717 |
< |
p = (n != p) ? n : head; |
714 |
> |
if (p.isData) { |
715 |
> |
if (item != null && item != p) |
716 |
> |
return this.<E>cast(item); |
717 |
> |
} |
718 |
> |
else if (item == null) |
719 |
> |
return null; |
720 |
|
} |
721 |
|
return null; |
722 |
|
} |
757 |
|
private void advance(Node prev) { |
758 |
|
lastPred = lastRet; |
759 |
|
lastRet = prev; |
760 |
< |
Node p; |
761 |
< |
if (prev == null || (p = prev.next) == prev) |
753 |
< |
p = head; |
754 |
< |
while (p != null) { |
760 |
> |
for (Node p = (prev == null) ? head : succ(prev); |
761 |
> |
p != null; p = succ(p)) { |
762 |
|
Object item = p.item; |
763 |
|
if (p.isData) { |
764 |
|
if (item != null && item != p) { |
769 |
|
} |
770 |
|
else if (item == null) |
771 |
|
break; |
765 |
– |
Node n = p.next; |
766 |
– |
p = (n != p) ? n : head; |
772 |
|
} |
773 |
|
nextNode = null; |
774 |
|
} |