1 | package dmg.cells.nucleus ; |
2 | import java.util.* ; |
3 | import java.io.* ; |
4 | /** |
5 | * |
6 | * The CellPath is an abstraction of the path a CellMessage is |
7 | * assumed to travel. The path consists of a defined sequence of |
8 | * cell hops and a current position. The last hop which might |
9 | * as well be the only one, is called the FinalDestination. |
10 | * At any point a new Cell Hop can be added in two ways : |
11 | * <ul> |
12 | * <li>At the end of the sequence. The added Cell becomes the |
13 | * new FinalDestination. |
14 | * <li>Insert the new Cell behind the current position. The new |
15 | * Hop becomes the next hop. |
16 | * </ul> |
17 | * The string representation of a cell path can have the format: |
18 | * <pre> |
19 | * path : <addr1>[:<<addr2>[...]] |
20 | * addr : <cellName> | <cellName@domainName> | <cellName@local> |
21 | * </pre> |
22 | * |
23 | * @author Patrick Fuhrmann |
24 | * @version 0.1, 15 Feb 1998 |
25 | */ |
26 | /* |
27 | */ |
28 | public class CellPath implements Cloneable , Serializable { |
29 | private static final long serialVersionUID = -4922955783102747577L; |
30 | |
31 | private List<CellAddressCore> _list = new ArrayList<CellAddressCore>() ; |
32 | private List<CellAddressCore> _mark = null ; |
33 | private int _position = -1 ; |
34 | private int _storage = -1 ; |
35 | |
36 | protected CellPath(){ /* only subclasses allowed to created 'empty' paths*/} |
37 | protected CellPath( CellPath addr ){ |
38 | // |
39 | // this only works because _list contains |
40 | // immutable objects |
41 | // |
42 | _list.addAll(addr._list); |
43 | _position = addr._position ; |
44 | _storage = addr._storage ; |
45 | } |
46 | /** |
47 | * Creates a CellAddress with an initial path of <path> |
48 | * |
49 | * @param path The initial cell travel path. |
50 | */ |
51 | public CellPath( String path ){ |
52 | add( path ) ; |
53 | } |
54 | public CellPath( String cellName , String domainName ){ |
55 | add( new CellAddressCore( cellName , domainName ) ) ; ; |
56 | } |
57 | public int hops(){ return _list.size() ; } |
58 | synchronized void add( CellAddressCore core ){ |
59 | _list.add( core ) ; |
60 | if( _position < 0 )_position = 0 ; |
61 | } |
62 | public synchronized void add( CellPath addr ){ |
63 | _list.addAll( addr._list ) ; |
64 | } |
65 | public void add( String cell , String domain ){ |
66 | add( new CellAddressCore( cell , domain ) ) ; |
67 | } |
68 | /** |
69 | * Adds a cell path <path> to the end of the current path. |
70 | * |
71 | * @param path The added cell travel path. |
72 | */ |
73 | public synchronized void add( String path ){ |
74 | StringTokenizer st = new StringTokenizer( path ,":" ) ; |
75 | for( ; st.hasMoreTokens() ; ){ |
76 | add( new CellAddressCore( st.nextToken() ) ) ; |
77 | } |
78 | } |
79 | /** |
80 | * Creates a CellAddress with a single cell as initial destination. |
81 | * The cell is represented by its name and the name of its domain. |
82 | * |
83 | * @param cellName The name of the initial destination cell. |
84 | * @param domainName The name of the initial destination cells domain. |
85 | */ |
86 | public Object clone(){ |
87 | CellPath addr = new CellPath() ; |
88 | addr._list.addAll(_list); |
89 | addr._position = _position ; |
90 | addr._storage = _storage ; |
91 | return addr ; |
92 | } |
93 | /** |
94 | * Adds a cell path <path> to the end of the current path. |
95 | * |
96 | * @param path The added cell travel path. |
97 | */ |
98 | synchronized void insert( CellAddressCore core ){ |
99 | _list.add(_position + 1, core) ; |
100 | if( _position < 0 )_position = 0 ; |
101 | } |
102 | public synchronized void insert( String path ){ |
103 | StringTokenizer st = new StringTokenizer( path ,":" ) ; |
104 | for( ; st.hasMoreTokens() ; ){ |
105 | insert( new CellAddressCore( st.nextToken() ) ) ; |
106 | } |
107 | } |
108 | public void mark(){ |
109 | _mark = new ArrayList<CellAddressCore>(_list) ; |
110 | _storage = _position ; |
111 | } |
112 | public void reset(){ |
113 | if( _mark == null )return ; |
114 | _list = _mark ; |
115 | _position = _storage ; |
116 | |
117 | _storage = -1; |
118 | _mark = null; |
119 | } |
120 | public void insert( String cell , String domain ){ |
121 | add( new CellAddressCore( cell , domain ) ) ; |
122 | } |
123 | /** |
124 | * Increment the current cell position by one. |
125 | * |
126 | * @return true if the cell hops could be shifted, false if |
127 | * current cell was the final destination. |
128 | */ |
129 | public synchronized boolean next(){ |
130 | if( _position >= ( _list.size() - 1 ) )return false ; |
131 | _position++ ; |
132 | return true; |
133 | } |
134 | public synchronized void toFirstDestination(){ |
135 | _position = _list.size() == 0 ? -1 : 0 ; |
136 | } |
137 | public synchronized void revert(){ |
138 | |
139 | Collections.reverse(_list); |
140 | toFirstDestination() ; |
141 | } |
142 | public synchronized boolean isFinalDestination(){ |
143 | return _position >= ( _list.size() - 1 ) ; |
144 | } |
145 | public synchronized boolean isFirstDestination(){ |
146 | return _position == 0 ; |
147 | } |
148 | CellAddressCore getCurrent(){ |
149 | if( ( _list.size() == 0 ) || |
150 | ( _position < 0 ) || |
151 | ( _position >=_list.size() ) )return null ; |
152 | return _list.get( _position ) ; |
153 | } |
154 | public CellAddressCore getDestinationAddress(){ |
155 | return _list.get(_list.size()-1); |
156 | } |
157 | void replaceCurrent( CellAddressCore core ){ |
158 | if( ( _list.size() == 0 ) || |
159 | ( _position < 0 ) || |
160 | ( _position >=_list.size() ) )return ; |
161 | _list.set(_position, core ) ; |
162 | } |
163 | public String getCellName(){ |
164 | CellAddressCore core = getCurrent() ; |
165 | return core == null ? null : core.getCellName() ; |
166 | } |
167 | public String getCellDomainName(){ |
168 | CellAddressCore core = getCurrent() ; |
169 | return core == null ? null : core.getCellDomainName() ; |
170 | } |
171 | /* |
172 | public void wasStored(){ |
173 | _storage = _list.size()-1 ; |
174 | } |
175 | public synchronized CellAddress getPreviousStorageAddress(){ |
176 | if( _storage < 0 )return null ; |
177 | CellAddress addr = new CellAddress(); |
178 | for( int i = _list.size() -1 ; i >= _storage ; i -- ) |
179 | addr.add( (CellAddressCore)_list.elementAt(i) ) ; |
180 | return addr ; |
181 | } |
182 | */ |
183 | public String toSmallString(){ |
184 | int size = _list.size() ; |
185 | if( size == 0 )return "[empty]" ; |
186 | if( ( _position >= size ) || ( _position < 0 ) )return "[INVALID]" ; |
187 | |
188 | CellAddressCore core = _list.get(_position) ; |
189 | |
190 | if( size == 1 ){ |
191 | return "["+core.toString()+"]" ; |
192 | } |
193 | |
194 | if( _position == 0 ) |
195 | return "["+core.toString()+":...("+(size-1)+")...]" ; |
196 | if( _position == (size-1) ) |
197 | return "[...("+(size-1)+")...:"+core.toString()+"]" ; |
198 | |
199 | return "[...("+_position+")...:"+ |
200 | core.toString()+ |
201 | "...("+(size-_position-1)+")...]" ; |
202 | |
203 | |
204 | } |
205 | public String toString(){ return toFullString() ; } |
206 | public String toFullString(){ |
207 | int size = _list.size() ; |
208 | if( size == 0 )return "[empty]" ; |
209 | if( ( _position >= size ) || ( _position < 0 ) )return "[INVALID]" ; |
210 | |
211 | StringBuilder sb = new StringBuilder() ; |
212 | |
213 | sb.append("[") ; |
214 | for( int i = 0 ; i < _list.size() ; i ++ ){ |
215 | if( i > 0 )sb.append(":") ; |
216 | if( i == _position )sb.append(">") ; |
217 | if( ( _storage > -1 ) && ( i == _storage ) )sb.append("*") ; |
218 | sb.append(_list.get(i).toString()); |
219 | } |
220 | sb.append("]") ; |
221 | return sb.toString(); |
222 | } |
223 | public synchronized boolean equals( Object obj ){ |
224 | if( ! ( obj instanceof CellPath ) )return false ; |
225 | |
226 | CellPath other = (CellPath)obj ; |
227 | synchronized( other ){ // not deadlock free |
228 | int s = 0 ; |
229 | if( ( s = _list.size() ) != other._list.size() )return false ; |
230 | |
231 | for( int i = 0 ; i < s ; i++ ) |
232 | if( |
233 | ! _list.get(i).equals(other._list.get(i)) |
234 | )return false; |
235 | |
236 | return true ; |
237 | } |
238 | } |
239 | public synchronized int hashCode(){ |
240 | int sum = 0 ; |
241 | for( CellAddressCore addr: _list ) { |
242 | sum += addr.hashCode() ; |
243 | } |
244 | return sum ; |
245 | } |
246 | |
247 | public static void main( String [] args ){ |
248 | CellPath addr = new CellPath() ; |
249 | for( int i = 0 ; i < args.length ; i ++ ){ |
250 | addr.add( args[i] ) ; |
251 | |
252 | } |
253 | System.out.println( addr.toFullString() ) ; |
254 | System.out.println( addr.toString() ) ; |
255 | while( addr.next() )System.out.println( addr.toString() ) ; |
256 | } |
257 | |
258 | |
259 | } |