1 7 package org.jboss.cache; 8 9 10 import org.apache.commons.logging.Log; 11 import org.apache.commons.logging.LogFactory; 12 13 import java.io.Externalizable ; 14 import java.io.IOException ; 15 import java.io.ObjectInput ; 16 import java.io.ObjectOutput ; 17 import java.security.AccessController ; 18 import java.security.PrivilegedAction ; 19 import java.util.ArrayList ; 20 import java.util.Arrays ; 21 import java.util.Collections ; 22 import java.util.Iterator ; 23 import java.util.List ; 24 import java.util.StringTokenizer ; 25 26 46 public class Fqn implements Cloneable , Externalizable , Comparable <Fqn> 47 { 48 49 52 public static final String SEPARATOR = "/"; 53 54 private List elements; 55 private transient int hash_code = 0; 56 57 61 static boolean REL_123_COMPATIBLE = false; 62 63 static 64 { 65 try 73 { 74 AccessController.doPrivileged( 75 new PrivilegedAction () 76 { 77 public Object run() 78 { 79 String compatible = System.getProperty("jboss.cache.fqn.123compatible"); 80 REL_123_COMPATIBLE = Boolean.valueOf(compatible); 81 return null; 82 } 83 }); 84 } 85 catch (SecurityException ignored) 86 { 87 } 89 catch (Throwable t) 90 { 91 LogFactory.getLog(Fqn.class).error("Caught throwable reading system property " + 92 "jboss.cache.fqn.123compatible", t); 93 } 94 } 95 96 private static final long serialVersionUID = -5351930616956603651L; 97 98 101 public static final Fqn ROOT = new Fqn(); 102 private static Log log = LogFactory.getLog(Fqn.class); 103 104 107 public Fqn() 108 { 109 elements = Collections.EMPTY_LIST; 110 } 111 112 118 public Fqn(Object name) 119 { 120 elements = Collections.singletonList(name); 121 } 122 123 126 public Fqn(List names) 127 { 128 if (names != null) 129 elements = Arrays.asList(names.toArray()); 130 else 131 elements = Collections.EMPTY_LIST; 132 } 133 134 137 public Fqn(Object [] names) 138 { 139 if (names == null) 140 elements = Collections.EMPTY_LIST; 141 else 142 { 143 elements = Arrays.asList(names); 144 } 145 } 146 147 150 public Fqn(Fqn base, Object relative_name) 151 { 152 elements = new ArrayList (base.elements.size() + 1); 153 elements.addAll(base.elements); 154 elements.add(relative_name); 155 } 156 157 160 public Fqn(Fqn base, Fqn relative) 161 { 162 this(base, relative.elements); 163 } 164 165 168 public Fqn(Fqn base, List relative) 169 { 170 elements = new ArrayList (base.elements.size() + relative.size()); 171 elements.addAll(base.elements); 172 elements.addAll(relative); 173 } 174 175 178 public Fqn(Fqn base, Object relative_name1, Object relative_name2) 179 { 180 elements = new ArrayList (base.elements.size() + 2); 181 elements.addAll(base.elements); 182 elements.add(relative_name1); 183 elements.add(relative_name2); 184 } 185 186 189 public Fqn(Fqn base, Object relative_name1, Object relative_name2, Object relative_name3) 190 { 191 elements = new ArrayList (base.elements.size() + 3); 192 elements.addAll(base.elements); 193 elements.add(relative_name1); 194 elements.add(relative_name2); 195 elements.add(relative_name3); 196 } 197 198 202 private static Fqn createFqn(List list) 203 { 204 Fqn fqn = new Fqn(); 205 fqn.elements = list; 206 return fqn; 207 } 208 209 228 public static Fqn fromString(String fqn) 229 { 230 if (fqn == null) 231 return ROOT; 232 return createFqn(parse(fqn)); 233 } 234 235 private static List parse(String fqn) 236 { 237 List list = new ArrayList (); 238 StringTokenizer tok = new StringTokenizer (fqn, SEPARATOR); 239 while (tok.hasMoreTokens()) 240 { 241 list.add(tok.nextToken()); 242 } 243 return list; 244 } 245 246 261 public Fqn getFqnChild(int index) 262 { 263 return getFqnChild(0, index); 264 } 265 266 269 public Fqn getFqnChild(int startIndex, int endIndex) 270 { 271 return createFqn(elements.subList(startIndex, endIndex)); 272 } 273 274 278 public int size() 279 { 280 return elements.size(); 281 } 282 283 286 public Object get(int index) 287 { 288 return elements.get(index); 289 } 290 291 296 public Object getLastElement() 297 { 298 if (isRoot()) return SEPARATOR; 299 return elements.get(elements.size() - 1); 300 } 301 302 305 public boolean hasElement(Object o) 306 { 307 return elements.lastIndexOf(o) != -1; 308 } 309 310 313 public Fqn clone() 314 { 315 try 316 { 317 return (Fqn) super.clone(); 318 } 319 catch (CloneNotSupportedException e) 320 { 321 log.error("Unable to clone Fqn " + this, e); 322 return null; 323 } 324 } 325 326 329 public boolean equals(Object obj) 330 { 331 if (this == obj) 332 return true; 333 if (!(obj instanceof Fqn)) 334 return false; 335 Fqn other = (Fqn) obj; 336 return elements.equals(other.elements); 337 } 338 339 342 public int hashCode() 343 { 344 if (hash_code == 0) 345 { 346 hash_code = _hashCode(); 347 } 348 return hash_code; 349 } 350 351 361 public String toString() 362 { 363 if (isRoot()) return SEPARATOR; 364 StringBuffer sb = new StringBuffer (); 365 for (Iterator it = elements.iterator(); it.hasNext();) 366 { 367 sb.append(SEPARATOR).append(it.next()); 368 } 369 return sb.toString(); 370 } 371 372 public void writeExternal(ObjectOutput out) throws IOException 373 { 374 375 if (REL_123_COMPATIBLE) 376 { 377 out.writeObject(elements); 378 } 379 else 380 { 381 out.writeShort(elements.size()); 382 Object element; 383 for (Iterator it = elements.iterator(); it.hasNext();) 384 { 385 element = it.next(); 386 out.writeObject(element); 387 } 388 } 389 } 390 391 public void readExternal(ObjectInput in) throws IOException , ClassNotFoundException 392 { 393 394 if (REL_123_COMPATIBLE) 395 { 396 elements = (List ) in.readObject(); 397 } 398 else 399 { 400 short length = in.readShort(); 401 this.elements = new ArrayList (length); 402 for (int i = 0; i < length; i++) 403 { 404 elements.add(in.readObject()); 405 } 406 } 407 } 408 409 410 416 public boolean isChildOf(Fqn parentFqn) 417 { 418 if (parentFqn.elements.size() == elements.size()) 419 return false; 420 return isChildOrEquals(parentFqn); 421 } 422 423 426 public boolean isChildOrEquals(Fqn parentFqn) 427 { 428 List parentList = parentFqn.elements; 429 if (parentList.size() > elements.size()) 430 return false; 431 for (int i = parentList.size() - 1; i >= 0; i--) 432 { 433 if (!parentList.get(i).equals(elements.get(i))) 434 return false; 435 } 436 return true; 437 } 438 439 442 private int _hashCode() 443 { 444 int hashCode = 0; 445 int count = 1; 446 Object o; 447 for (Iterator it = elements.iterator(); it.hasNext();) 448 { 449 o = it.next(); 450 hashCode += (o == null) ? 0 : o.hashCode() * count++; 451 } 452 if (hashCode == 0) hashCode = 0xFEED; 454 return hashCode; 455 } 456 457 460 public Fqn getParent() 461 { 462 switch (elements.size()) 463 { 464 case 0: 465 case 1: 466 return ROOT; 467 default: 468 return createFqn(elements.subList(0, elements.size() - 1)); 469 } 470 } 471 472 475 public boolean isRoot() 476 { 477 return elements.isEmpty(); 478 } 479 480 484 public String getLastElementAsString() 485 { 486 if (isRoot()) return SEPARATOR; 487 else return String.valueOf(getLastElement()); 488 } 489 490 496 public List peekElements() 497 { 498 return Collections.unmodifiableList(elements); 499 } 500 501 504 public int compareTo(Fqn fqn) 505 { 506 return FqnComparator.INSTANCE.compare(this, fqn); 507 } 508 } | Popular Tags |