1 19 20 package org.apache.cayenne.project; 21 22 import java.util.Arrays ; 23 24 37 public class ProjectPath { 38 public static final Object [] EMPTY_PATH = new Object [0]; 39 40 protected Object [] path; 41 42 public ProjectPath() { 43 path = EMPTY_PATH; 44 } 45 46 49 public ProjectPath(Object object) { 50 path = new Object [] { object }; 51 } 52 53 56 public ProjectPath(Object [] path) { 57 this.path = (path != null) ? path : EMPTY_PATH; 58 } 59 60 public Object [] getPath() { 61 return path; 62 } 63 64 public boolean isEmpty() { 65 return path == null || path.length == 0; 66 } 67 68 71 public Object firstInstanceOf(Class aClass) { 72 for (int i = 0; i < path.length; i++) { 73 if (path[i] != null && aClass.isAssignableFrom(path[i].getClass())) { 74 return path[i]; 75 } 76 } 77 78 return null; 79 } 80 81 85 public ProjectPath appendToPath(Object object) { 86 if (object != null) { 87 Object [] newPath = new Object [path.length + 1]; 88 89 if (path.length > 0) { 90 System.arraycopy(path, 0, newPath, 0, path.length); 91 } 92 newPath[path.length] = object; 93 return new ProjectPath(newPath); 94 } 95 else { 96 return this; 97 } 98 } 99 100 104 public ProjectPath subpathWithSize(int subpathSize) { 105 if (subpathSize <= 0) { 106 return new ProjectPath(); 107 } 108 else if(subpathSize == path.length) { 109 return this; 110 } 111 112 if (subpathSize > path.length) { 113 throw new ArrayIndexOutOfBoundsException ( 114 "Subpath is longer than this path " 115 + subpathSize 116 + " components. Path size: " 117 + path.length); 118 } 119 120 Object [] newPath = new Object [subpathSize]; 121 System.arraycopy(path, 0, newPath, 0, subpathSize); 122 return new ProjectPath(newPath); 123 } 124 125 130 public ProjectPath subpathOfObject(Object object) { 131 for (int i = 0; i < path.length; i++) { 132 if (path[i] == object) { 133 return subpathWithSize(i + 1); 135 } 136 } 137 138 return null; 139 } 140 141 144 public Object getRoot() { 145 if (path.length == 0) { 146 return null; 147 } 148 149 return path[0]; 150 } 151 152 155 public Object getObject() { 156 if (path.length == 0) { 157 return null; 158 } 159 160 return path[path.length - 1]; 162 } 163 164 169 public Object getObjectParent() { 170 if (path.length == 0) { 171 return null; 172 } 173 174 return (path.length > 1) ? path[path.length - 2] : null; 176 } 177 178 public String toString() { 179 StringBuffer buf = new StringBuffer (); 180 buf.append("[ProjectPath: "); 181 for (int i = 0; i < path.length; i++) { 182 if (i > 0) { 183 buf.append(", "); 184 } 185 186 String token = (path[i] != null) ? path[i].getClass().getName() : "<null>"; 187 buf.append(token); 188 } 189 buf.append("]"); 190 return buf.toString(); 191 } 192 193 public boolean equals(Object object) { 194 if (object == this) { 195 return true; 196 } 197 198 if (!(object instanceof ProjectPath)) { 199 return false; 200 } 201 202 ProjectPath otherPath = (ProjectPath) object; 203 return Arrays.equals(getPath(), otherPath.getPath()); 204 } 205 } 206 | Popular Tags |