/
CellPath.java
254 lines (220 loc) · 6.55 KB
/
CellPath.java
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package dmg.cells.nucleus ;
import com.google.common.collect.Lists;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;
import static com.google.common.base.Preconditions.checkArgument;
import static java.util.stream.Collectors.toList;
/**
*
* The CellPath is an abstraction of the path a CellMessage is
* assumed to travel. The path consists of a defined sequence of
* cell hops and a current position. The last hop which might
* as well be the only one, is called the FinalDestination.
* At any point a new Cell Hop can be added in two ways :
* <ul>
* <li>At the end of the sequence. The added Cell becomes the
* new FinalDestination.
* <li>Insert the new Cell behind the current position. The new
* Hop becomes the next hop.
* </ul>
* The string representation of a cell path can have the format:
* <pre>
* path : <addr1>[:<<addr2>[...]]
* addr : <cellName> | <cellName@domainName> | <cellName@local>
* </pre>
*
* @author Patrick Fuhrmann
* @version 0.1, 15 Feb 1998
*/
public final class CellPath implements Cloneable , Serializable
{
private static final long serialVersionUID = -4922955783102747577L;
private final List<CellAddressCore> _list;
private int _position = -1;
private CellPath(int position, List<CellAddressCore> list)
{
_position = position;
_list = list;
}
protected CellPath()
{
this(-1, new ArrayList<>());
}
public CellPath(String path)
{
this(0, streamOfPath(path).collect(toList()));
}
public CellPath(CellAddressCore address)
{
this(0, Lists.newArrayList(address));
}
public CellPath(String cellName, String domainName)
{
this(new CellAddressCore(cellName, domainName));
}
public synchronized int hops()
{
return _list.size();
}
public synchronized void add(CellAddressCore core)
{
_list.add(core) ;
if (_position < 0) {
_position = 0;
}
}
public synchronized void add(CellPath addr)
{
_list.addAll(addr._list) ;
if (_position < 0) {
_position = 0;
}
}
/**
* Adds a cell path <path> to the end of the current path.
*
* @param path The added cell travel path.
*/
public synchronized void add(String path)
{
streamOfPath(path).forEachOrdered(this::add);
}
@Override
public synchronized Object clone()
{
return new CellPath(_position, Lists.newArrayList(_list));
}
/**
* Adds a cell path <path> after the current position.
*/
public synchronized void insert( CellPath path ){
_list.addAll(_position + 1, path._list);
}
/**
* Increment the current cell position by one.
*
* @return true if the cell hops could be shifted, false if
* current cell was the final destination.
*/
public synchronized boolean next(){
if( _position >= ( _list.size() - 1 ) ) {
return false;
}
_position++ ;
return true;
}
public synchronized CellPath revert(){
return new CellPath(_list.isEmpty() ? -1 : 0,
Lists.newArrayList(Lists.reverse(_list)));
}
public synchronized boolean isFinalDestination(){
return _position >= ( _list.size() - 1 ) ;
}
public synchronized boolean isFirstDestination(){
return _position == 0 ;
}
public synchronized CellAddressCore getCurrent(){
if( ( _list.size() == 0 ) ||
( _position < 0 ) ||
( _position >=_list.size() ) ) {
return null;
}
return _list.get( _position ) ;
}
public synchronized CellAddressCore getSourceAddress(){
return _list.get(0);
}
public synchronized CellAddressCore getDestinationAddress(){
return _list.get(_list.size()-1);
}
synchronized void replaceCurrent( CellAddressCore core ){
if( ( _list.size() == 0 ) ||
( _position < 0 ) ||
( _position >=_list.size() ) ) {
return;
}
_list.set(_position, core ) ;
}
public String getCellName(){
CellAddressCore core = getCurrent() ;
return core == null ? null : core.getCellName() ;
}
public String getCellDomainName(){
CellAddressCore core = getCurrent() ;
return core == null ? null : core.getCellDomainName() ;
}
public synchronized String toSmallString(){
int size = _list.size() ;
if( size == 0 ) {
return "[empty]";
}
if( ( _position >= size ) || ( _position < 0 ) ) {
return "[INVALID]";
}
CellAddressCore core = _list.get(_position) ;
if( size == 1 ){
return "["+core.toString()+"]" ;
}
if( _position == 0 ) {
return "[" + core.toString() + ":...(" + (size - 1) + ")...]";
}
if( _position == (size-1) ) {
return "[...(" + (size - 1) + ")...:" + core.toString() + "]";
}
return "[...("+_position+")...:"+
core.toString()+
"...("+(size-_position-1)+")...]" ;
}
@Override
public String toString(){ return toFullString() ; }
public synchronized String toFullString(){
int size = _list.size() ;
if( size == 0 ) {
return "[empty]";
}
if( ( _position >= size ) || ( _position < 0 ) ) {
return "[INVALID]";
}
StringBuilder sb = new StringBuilder() ;
sb.append("[") ;
for( int i = 0 ; i < _list.size() ; i ++ ){
if( i > 0 ) {
sb.append(":");
}
if( i == _position ) {
sb.append(">");
}
sb.append(_list.get(i).toString());
}
sb.append("]") ;
return sb.toString();
}
@Override
public boolean equals(Object obj)
{
if (obj == this) {
return true;
}
if (!(obj instanceof CellPath)) {
return false;
}
List<CellAddressCore> other;
synchronized (obj) {
other = new ArrayList<>(((CellPath) obj)._list);
}
synchronized (this) {
return _list.equals(other);
}
}
@Override
public synchronized int hashCode(){
return (_list.hashCode() * 17) ^ _position;
}
private static Stream<CellAddressCore> streamOfPath(String path)
{
checkArgument(!path.isEmpty());
return Stream.of(path.split(":")).map(CellAddressCore::new);
}
}