blob: ea104fe6bb9a0fd1c17c45c3f526c7053d2ed9ed (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.messagebus.routing;
import com.yahoo.messagebus.Reply;
import java.util.Iterator;
import java.util.List;
/**
* Implements an iterator for routing nodes. Use {@link RoutingContext#getChildIterator()} to retrieve an instance of
* this.
*
* @author Simon Thoresen Hult
*/
public class RoutingNodeIterator {
// The underlying iterator.
private Iterator<RoutingNode> it;
// The current child entry.
private RoutingNode entry;
/**
* Constructs a new iterator based on a given list.
*
* @param children The list to iterate through.
*/
public RoutingNodeIterator(List<RoutingNode> children) {
it = children.iterator();
next();
}
/**
* Steps to the next child in the map.
*
* @return This, to allow chaining.
*/
public RoutingNodeIterator next() {
entry = it.hasNext() ? it.next() : null;
return this;
}
/**
* Skips the given number of children.
*
* @param num The number of children to skip.
* @return This, to allow chaining.
*/
public RoutingNodeIterator skip(int num) {
for (int i = 0; i < num && isValid(); ++i) {
next();
}
return this;
}
/**
* Returns whether or not this iterator is valid.
*
* @return True if we are still pointing to a valid entry.
*/
public boolean isValid() {
return entry != null;
}
/**
* Returns the route of the current child.
*
* @return The route.
*/
public Route getRoute() {
return entry.getRoute();
}
/**
* Returns whether or not a reply is set in the current child.
*
* @return True if a reply is available.
*/
public boolean hasReply() {
return entry.hasReply();
}
/**
* Removes and returns the reply of the current child. This is the correct way of reusing a reply of a child node,
* the {@link #getReplyRef()} should be used when just inspecting a child reply.
*
* @return The reply.
*/
public Reply removeReply() {
Reply ret = entry.getReply();
ret.getTrace().setLevel(entry.getTrace().getLevel());
ret.getTrace().swap(entry.getTrace());
entry.setReply(null);
return ret;
}
/**
* Returns the reply of the current child. It is VERY important that the reply returned by this function is not
* reused anywhere. This is a reference to another node's reply, do NOT use it for anything but inspection. If you
* want to retrieve and reuse it, call {@link #removeReply()} instead.
*
* @return The reply.
*/
public Reply getReplyRef() {
return entry.getReply();
}
}
|