1 57 package org.objectstyle.cayenne.project; 58 59 import java.util.Arrays ; 60 61 74 public class ProjectPath { 75 public static final Object [] EMPTY_PATH = new Object [0]; 76 77 protected Object [] path; 78 79 public ProjectPath() { 80 path = EMPTY_PATH; 81 } 82 83 86 public ProjectPath(Object object) { 87 path = new Object [] { object }; 88 } 89 90 93 public ProjectPath(Object [] path) { 94 this.path = (path != null) ? path : EMPTY_PATH; 95 } 96 97 public Object [] getPath() { 98 return path; 99 } 100 101 public boolean isEmpty() { 102 return path == null || path.length == 0; 103 } 104 105 108 public Object firstInstanceOf(Class aClass) { 109 for (int i = 0; i < path.length; i++) { 110 if (path[i] != null && aClass.isAssignableFrom(path[i].getClass())) { 111 return path[i]; 112 } 113 } 114 115 return null; 116 } 117 118 122 public ProjectPath appendToPath(Object object) { 123 if (object != null) { 124 Object [] newPath = new Object [path.length + 1]; 125 126 if (path.length > 0) { 127 System.arraycopy(path, 0, newPath, 0, path.length); 128 } 129 newPath[path.length] = object; 130 return new ProjectPath(newPath); 131 } 132 else { 133 return this; 134 } 135 } 136 137 141 public ProjectPath subpathWithSize(int subpathSize) { 142 if (subpathSize <= 0) { 143 return new ProjectPath(); 144 } 145 else if(subpathSize == path.length) { 146 return this; 147 } 148 149 if (subpathSize > path.length) { 150 throw new ArrayIndexOutOfBoundsException ( 151 "Subpath is longer than this path " 152 + subpathSize 153 + " components. Path size: " 154 + path.length); 155 } 156 157 Object [] newPath = new Object [subpathSize]; 158 System.arraycopy(path, 0, newPath, 0, subpathSize); 159 return new ProjectPath(newPath); 160 } 161 162 167 public ProjectPath subpathOfObject(Object object) { 168 for (int i = 0; i < path.length; i++) { 169 if (path[i] == object) { 170 return subpathWithSize(i + 1); 172 } 173 } 174 175 return null; 176 } 177 178 181 public Object getRoot() { 182 if (path.length == 0) { 183 return null; 184 } 185 186 return path[0]; 187 } 188 189 192 public Object getObject() { 193 if (path.length == 0) { 194 return null; 195 } 196 197 return path[path.length - 1]; 199 } 200 201 206 public Object getObjectParent() { 207 if (path.length == 0) { 208 return null; 209 } 210 211 return (path.length > 1) ? path[path.length - 2] : null; 213 } 214 215 public String toString() { 216 StringBuffer buf = new StringBuffer (); 217 buf.append("[ProjectPath: "); 218 for (int i = 0; i < path.length; i++) { 219 if (i > 0) { 220 buf.append(", "); 221 } 222 223 String token = (path[i] != null) ? path[i].getClass().getName() : "<null>"; 224 buf.append(token); 225 } 226 buf.append("]"); 227 return buf.toString(); 228 } 229 230 public boolean equals(Object object) { 231 if (object == this) { 232 return true; 233 } 234 235 if (!(object instanceof ProjectPath)) { 236 return false; 237 } 238 239 ProjectPath otherPath = (ProjectPath) object; 240 return Arrays.equals(getPath(), otherPath.getPath()); 241 } 242 } 243 | Popular Tags |