1 16 17 package com.google.gwt.user.client.ui; 18 19 import com.google.gwt.core.client.JavaScriptObject; 20 21 import java.util.AbstractCollection ; 22 import java.util.ArrayList ; 23 import java.util.Collection ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 import java.util.NoSuchElementException ; 27 28 32 class PrefixTree extends AbstractCollection { 33 34 42 private static class PrefixTreeIterator implements Iterator { 43 44 private JavaScriptObject stack; 45 46 51 public PrefixTreeIterator(PrefixTree tree) { 52 init(); 53 addTree(tree, ""); 54 } 55 56 public boolean hasNext() { 57 return nextImpl(true) != null; 59 } 60 61 64 public Object next() { 65 final Object toReturn = nextImpl(false); 66 67 if (toReturn == null) { 69 if (!hasNext()) { 71 throw new NoSuchElementException ("No more elements in the iterator"); 72 } else { 73 throw new RuntimeException ( 74 "nextImpl() returned null, but hasNext says otherwise"); 75 } 76 } 77 78 return toReturn; 79 } 80 81 public void remove() { 82 throw new UnsupportedOperationException ("PrefixTree does not support " 83 + "removal. Use clear()"); 84 } 85 86 100 private native void addTree(PrefixTree tree, String prefix) ; 116 117 120 private native void init() ; 123 124 131 private native Object nextImpl(boolean peek) ; 178 } 179 180 187 protected static PrefixTree createPrefixTree(int prefixLength) { 188 return new PrefixTree(prefixLength); 189 } 190 191 198 private static String safe(String s) { 199 return ':' + s; 200 } 201 202 208 private static String unsafe(String s) { 209 return s.substring(1); 210 } 211 212 215 protected final int prefixLength; 216 217 220 protected JavaScriptObject suffixes; 221 222 225 protected JavaScriptObject subtrees; 226 227 231 protected int size = 0; 232 233 236 public PrefixTree() { 237 this(2, null); 238 } 239 240 245 public PrefixTree(Collection source) { 246 this(2, source); 247 } 248 249 255 public PrefixTree(final int prefixLength) { 256 this(prefixLength, null); 257 } 258 259 266 public PrefixTree(final int prefixLength, final Collection source) { 267 this.prefixLength = prefixLength; 268 clear(); 269 270 if (source != null) { 271 addAll(source); 272 } 273 } 274 275 283 public boolean add(Object o) throws UnsupportedOperationException { 284 if (o instanceof String ) { 285 return add((String ) o); 286 } else { 287 throw new UnsupportedOperationException ( 288 "Cannot add non-Strings to PrefixTree"); 289 } 290 } 291 292 299 public native boolean add(String s) ; 348 349 352 public native void clear() ; 357 358 public boolean contains(Object o) { 359 if (o instanceof String ) { 360 return contains((String ) o); 361 } else { 362 return false; 363 } 364 } 365 366 public boolean contains(String s) { 367 return (getSuggestions(s, 1)).contains(s); 368 } 369 370 382 public List getSuggestions(String search, int limit) { 383 final List toReturn = new ArrayList (); 384 if ((search != null) && (limit > 0)) { 385 suggestImpl(search, "", toReturn, limit); 386 } 387 return toReturn; 388 } 389 390 public Iterator iterator() { 391 return new PrefixTreeIterator(this); 392 } 393 394 399 public int size() { 400 return size; 401 } 402 403 protected native void suggestImpl(String search, String prefix, 404 Collection output, int limit) ; 469 470 476 private void dump(Collection output, String prefix) { 477 for (final Iterator i = iterator(); i.hasNext();) { 478 output.add(prefix + (String ) i.next()); 479 } 480 } 481 } 482 | Popular Tags |