1 16 package org.apache.myfaces.custom.tree.model; 17 18 import java.io.Serializable ; 19 20 21 35 public final class TreePath 36 extends Object 37 implements Serializable 38 { 39 40 41 private Object [] elements; 42 43 44 49 public TreePath(Object [] pathElements) 50 { 51 if (pathElements == null || pathElements.length == 0) 52 { 53 throw new IllegalArgumentException ("pathElements must be non null and not empty"); 54 } 55 56 elements = pathElements; 57 } 58 59 60 64 protected TreePath(TreePath parent, Object lastElement) 65 { 66 elements = new Object [parent.elements.length + 1]; 67 68 System.arraycopy(parent.elements, 0, elements, 0, elements.length - 1); 69 elements[elements.length - 1] = lastElement; 70 } 71 72 73 79 protected TreePath(Object [] pathElements, int length) 80 { 81 Object [] elements = new Object [length]; 82 83 System.arraycopy(pathElements, 0, elements, 0, length); 84 } 85 86 87 93 public Object [] getPath() 94 { 95 Object [] answer = new Object [elements.length]; 96 System.arraycopy(elements, 0, answer, 0, elements.length); 97 return answer; 98 } 99 100 101 106 public Object getLastPathComponent() 107 { 108 return elements[elements.length - 1]; 109 } 110 111 112 117 public int getPathCount() 118 { 119 return elements.length; 120 } 121 122 123 131 public Object getPathComponent(int index) 132 { 133 if (index < 0 || index >= elements.length) 134 { 135 throw new IllegalArgumentException ("Index " + index + " is out of range"); 136 } 137 138 return elements[index]; 139 } 140 141 142 149 public boolean equals(Object o) 150 { 151 if (o == this) 152 { 153 return true; 154 } 155 156 if (!(o instanceof TreePath)) 157 { 158 return false; 159 } 160 TreePath other = (TreePath)o; 161 162 if (elements.length != other.elements.length) 163 { 164 return false; 165 } 166 167 for (int i = 0; i < elements.length; i++) 168 { 169 Object thisElement = elements[i]; 170 Object otherElement = other.elements[i]; 171 172 if (!thisElement.equals(otherElement)) 173 { 174 return false; 175 } 176 } 177 return true; 178 } 179 180 181 187 public int hashCode() 188 { 189 return elements[elements.length - 1].hashCode(); 190 } 191 192 193 203 public boolean isDescendant(TreePath path) 204 { 205 if (path == null) 206 { 207 return false; 208 } 209 210 if (elements.length < path.elements.length) 211 { 212 return false; 214 } 215 216 for (int i = 0; i < elements.length; i++) 217 { 218 Object thisElement = elements[i]; 219 Object otherElement = path.elements[i]; 220 221 if (!thisElement.equals(otherElement)) 222 { 223 return false; 224 } 225 } 226 return true; 227 } 228 229 230 237 public TreePath pathByAddingChild(Object child) 238 { 239 if (child == null) 240 { 241 throw new NullPointerException ("Null child not allowed"); 242 } 243 244 return new TreePath(this, child); 245 } 246 247 248 252 public TreePath getParentPath() 253 { 254 return new TreePath(elements, elements.length - 1); 255 } 256 257 258 264 public String toString() 265 { 266 StringBuffer buffer = new StringBuffer ("["); 267 268 for (int i = 0; i < elements.length; i++) 269 { 270 if (i > 0) 271 { 272 buffer.append(", "); 273 } 274 buffer.append(elements[i]); 275 276 } 277 278 buffer.append("]"); 279 return buffer.toString(); 280 } 281 } 282 283 | Popular Tags |