1 23 24 package org.objectweb.fractal.julia.loader; 25 26 29 30 public class Tree { 31 32 35 36 private final String leaf; 37 38 41 42 private final Tree[] subTrees; 43 44 48 53 54 public Tree (final String leaf) { 55 this.leaf = leaf; 56 this.subTrees = null; 57 } 58 64 65 public Tree (final Tree[] subTrees) { 66 this.leaf = null; 67 this.subTrees = subTrees; 68 } 69 70 74 79 80 public int getSize () { 81 return (subTrees == null ? 0 : subTrees.length); 82 } 83 84 92 93 public Tree getSubTree (final int index) { 94 if (subTrees == null) { 95 throw new ArrayIndexOutOfBoundsException (index); 96 } else { 97 return subTrees[index]; 98 } 99 } 100 101 106 107 public Tree[] getSubTrees () { 108 if (subTrees == null) { 109 return new Tree[0]; 110 } else { 111 return subTrees; 112 } 113 } 114 115 122 123 public Tree getValue (final String key) { 124 Tree[] t = getSubTrees(); 125 for (int i = 0; i < t.length; ++i) { 126 if (t[i].getSize() == 2) { 127 if (t[i].getSubTree(0).equals(key)) { 128 return t[i].getSubTree(1); 129 } 130 } 131 } 132 return null; 133 } 134 135 139 147 148 public boolean equals (final Object o) { 149 if (o instanceof String ) { 150 String s = (String )o; 151 return equals(s, 0, s.length()) == 0; 155 } else { 156 throw new RuntimeException ("Not yet implemented"); 157 } 158 } 159 160 165 166 public int hashCode () { 167 return hashCode(0); 171 } 172 173 179 180 public String toString () { 181 if (leaf != null) { 182 return leaf; 183 } 184 if (subTrees.length == 0) { 185 return "()"; 186 } 187 StringBuffer buf = new StringBuffer (); 188 toString(buf); 189 return buf.toString(); 190 } 191 192 196 202 203 private int hashCode (int hc) { 204 if (leaf != null) { 205 hc = 17 * (hc + leaf.hashCode()); 206 } else { 207 int l = subTrees.length; 208 hc = 17 * (hc + '('); 209 for (int i = 0; i < l; ++i) { 210 if (i > 0) { 211 hc = 17 * (hc + ' '); 212 } 213 hc = subTrees[i].hashCode(hc); 214 } 215 hc = 17 * (hc + ')'); 216 } 217 return hc; 218 } 219 220 233 234 public int equals (final String str, int off, int len) { 235 if (leaf != null) { 236 int l = leaf.length(); 237 if (len < l || !str.regionMatches(false, off, leaf, 0, l)) { 238 return -1; 239 } 240 return len - l; 241 } else { 242 int l = subTrees.length; 243 if (len == 0 || str.charAt(off) != '(') { 244 return -1; 245 } 246 ++off; 247 --len; 248 for (int i = 0; i < l; ++i) { 249 if (i > 0) { 250 if (len == 0 || str.charAt(off) != ' ') { 251 return -1; 252 } 253 ++off; 254 --len; 255 } 256 int newLen = subTrees[i].equals(str, off, len); 257 if (newLen == -1) { 258 return -1; 259 } 260 off += len - newLen; 261 len = newLen; 262 } 263 if (len == 0 || str.charAt(off) != ')') { 264 return -1; 265 } 266 return len - 1; 267 } 268 } 269 270 276 277 private void toString (final StringBuffer buf) { 278 if (subTrees == null) { 279 buf.append(leaf); 280 } else { 281 int l = subTrees.length; 282 buf.append('('); 283 for (int i = 0; i < l; ++i) { 284 if (i > 0) { 285 buf.append(' '); 286 } 287 subTrees[i].toString(buf); 288 } 289 buf.append(')'); 290 } 291 } 292 } 293 | Popular Tags |