1 package org.sapia.archie; 2 3 import java.io.Serializable ; 4 import java.util.ArrayList ; 5 import java.util.List ; 6 7 8 25 public class Name implements Serializable { 26 private List _parts = new ArrayList (); 27 private int _currentIndex; 28 29 protected Name(List parts) { 30 _parts.addAll(parts); 31 } 32 33 public Name() { 34 } 35 36 41 public int count() { 42 return _parts.size(); 43 } 44 45 50 public int getCurrentIndex() { 51 return _currentIndex; 52 } 53 54 59 public void setCurrentIndex(int i) { 60 if ((i < 0) || (i > (_parts.size() - 1))) { 61 throw new IllegalArgumentException ("Specified index exceeds bounds"); 62 } 63 64 _currentIndex = i; 65 } 66 67 70 public void reset() { 71 _currentIndex = 0; 72 } 73 74 82 public boolean hasNextPart() { 83 boolean hasNext = _currentIndex < _parts.size(); 84 85 if (!hasNext) { 86 reset(); 87 } 88 89 return hasNext; 90 } 91 92 97 public NamePart nextPart() { 98 return (NamePart) _parts.get(_currentIndex++); 99 } 100 101 106 public NamePart get(int i) { 107 return (NamePart) _parts.get(i); 108 } 109 110 116 public Name add(NamePart part) { 117 _parts.add(part); 118 119 return this; 120 } 121 122 public Name addAt(int pos, NamePart part){ 123 assertPos(pos); 124 _parts.add(pos, part); 125 return this; 126 } 127 128 public NamePart removeAt(int pos){ 129 return (NamePart)_parts.remove(pos); 130 } 131 132 public boolean endsWith(Name other){ 133 if(count() < other.count()) return false; 134 for(int i = other.count() - 1; i > 0; i--){ 135 if(!((NamePart)other.get(i)).asString().equals(get(i).asString())){ 136 return false; 137 } 138 } 139 return true; 140 } 141 142 public boolean startsWith(Name other){ 143 if(count() < other.count()) return false; 144 for(int i = 0; i < other.count(); i++){ 145 if(!((NamePart)other.get(i)).asString().equals(get(i).asString())){ 146 return false; 147 } 148 } 149 return true; 150 } 151 152 158 public Name add(Name n) { 159 _parts.addAll(n._parts); 160 161 return this; 162 } 163 164 172 public Name getTo(int to) { 173 Name n = new Name(); 174 NamePart current; 175 176 for (int i = 0; i < to; i++) { 177 current = (NamePart) _parts.get(i); 178 n._parts.add(current); 179 } 180 181 return n; 182 } 183 184 192 193 public Name getFrom(int from) { 194 Name n = new Name(); 195 NamePart current; 196 197 for (int i = from; i < _parts.size(); i++) { 198 current = (NamePart) _parts.get(i); 199 n._parts.add(current); 200 } 201 202 return n; 203 } 204 205 210 public NamePart first() { 211 return (NamePart) _parts.get(0); 212 } 213 214 219 public NamePart last() { 220 return (NamePart) _parts.get(_parts.size() - 1); 221 } 222 223 228 public NamePart chopLast() { 229 return (NamePart) _parts.remove(_parts.size() - 1); 230 } 231 232 237 public NamePart chopFirst() { 238 return (NamePart) _parts.remove(0); 239 } 240 241 public Object clone() { 242 return new Name(_parts); 243 } 244 245 248 public String toString() { 249 return _parts.toString(); 250 } 251 252 public boolean equals(Object o){ 253 try{ 254 Name other = (Name)o; 255 if(other.count() != count()){ 256 return false; 257 } 258 for(int i = 0; i < other.count(); i++){ 259 if(!other.get(i).asString().equals(get(i).asString())){ 260 return false; 261 } 262 } 263 return true; 264 }catch(ClassCastException e){ 265 return false; 266 } 267 } 268 269 270 271 private void assertPos(int pos){ 272 if(pos < 0){ 273 throw new ArrayIndexOutOfBoundsException ("Index too small: " + pos); 274 } 275 if(pos > _parts.size()){ 276 throw new ArrayIndexOutOfBoundsException ("Index too large: " + pos); 277 } 278 } 279 } 280 | Popular Tags |