1 4 package org.oddjob.arooa.registry; 5 6 import java.io.Serializable ; 7 8 13 public class Path implements Serializable { 14 private static final long serialVersionUID = 20051117; 15 16 17 public static final char PATH_SEPARATOR = '/'; 18 19 20 private interface PathElement extends Serializable { 21 public String idOf(); 22 } 23 24 25 private class IdPathElement implements PathElement { 26 private static final long serialVersionUID = 20051122; 27 private final String id; 28 IdPathElement(String id) { 29 if (id == null) { 30 throw new NullPointerException ("Id must not be null!"); 31 } 32 this.id = id; 33 } 34 public String idOf() { 35 return id; 36 } 37 public boolean equals(Object o) { 38 if (!(o instanceof IdPathElement)) { 39 return false; 40 } 41 IdPathElement other = (IdPathElement) o; 42 return id.equals(other.id); 43 } 44 public int hashCode() { 45 return id.hashCode(); 46 } 47 } 48 49 50 private final PathElement[] pathElements; 51 52 53 private final static PathElement nullElement = new PathElement() { 54 private static final long serialVersionUID = 20051122; 55 public String idOf() { 56 return null; 57 } 58 }; 59 60 63 public Path() { 64 this.pathElements = new PathElement[0]; 65 } 66 67 72 public Path(String path) { 73 this.pathElements = parsePath(path); 74 } 75 76 81 private Path(PathElement[] pathElements) { 82 this.pathElements = pathElements; 83 } 84 85 91 private PathElement[] parsePath(String path) { 92 String [] ids = path.split(new String (new char[] {PATH_SEPARATOR})); 93 PathElement[] elements = new PathElement[ids.length]; 94 for (int i = 0; i < ids.length; ++i ) { 95 elements[i] = new IdPathElement(ids[i]); 96 } 97 return elements; 98 } 99 100 105 public int size() { 106 return pathElements.length; 107 } 108 109 116 public String getId() { 117 if (pathElements.length == 0) { 118 return null; 119 } 120 return pathElements[0].idOf(); 121 122 } 123 124 131 public Path getChildPath() { 132 if (pathElements.length == 0) { 133 return null; 134 } 135 if (pathElements[0] == nullElement) { 136 return null; 137 } 138 PathElement[] childElements = new PathElement[pathElements.length -1]; 139 System.arraycopy(pathElements, 1, childElements, 0, childElements.length); 140 return new Path(childElements); 141 } 142 143 150 public Path addId(String id) { 151 if (id == null) { 152 return addNull(); 153 } 154 PathElement[] newElements = new PathElement[pathElements.length + 1]; 155 System.arraycopy(pathElements, 0, newElements, 0, pathElements.length); 156 newElements[pathElements.length] = new IdPathElement(id); 157 return new Path(newElements); 158 } 159 160 165 public Path addNull() { 166 PathElement[] newElements = new PathElement[pathElements.length + 1]; 167 System.arraycopy(pathElements, 0, newElements, 0, pathElements.length); 168 newElements[pathElements.length] = nullElement; 169 return new Path(newElements); 170 } 171 172 177 public boolean containsNull() { 178 for (int i = 0; i < pathElements.length; ++i) { 179 if (pathElements[i] == nullElement) { 180 return true; 181 } 182 } 183 return false; 184 } 185 186 192 public Path addPath(Path extra) { 193 PathElement[] extraElements = extra.pathElements; 194 PathElement[] newElements 195 = new PathElement[pathElements.length + extraElements.length]; 196 System.arraycopy(pathElements, 0, newElements, 0, pathElements.length); 197 System.arraycopy(extraElements, 0, newElements, 198 pathElements.length, extraElements.length); 199 return new Path(newElements); 200 } 201 202 209 public Path relativeTo(Path other) { 210 PathElement[] otherElements = other.pathElements; 211 212 if (otherElements.length <= pathElements.length) { 214 return null; 215 } 216 217 for (int i = 0; i < pathElements.length; ++i) { 218 if (!otherElements[i].equals(pathElements[i])) { 219 return null; 220 } 221 } 222 223 PathElement[] result = new PathElement[otherElements.length - 224 pathElements.length]; 225 System.arraycopy(otherElements, pathElements.length, 226 result, 0, result.length); 227 228 return new Path(result); 229 230 } 231 232 236 public String toString() { 237 StringBuffer result = new StringBuffer (); 238 for (int i = 0; i < pathElements.length; ++i) { 239 if (i > 0) { 240 result.append(PATH_SEPARATOR); 241 } 242 result.append(pathElements[i].idOf()); 243 } 244 return result.toString(); 245 } 246 } 247 | Popular Tags |