1
0
forked from GitHub/gf-core

GWT: somewhat nicer iterable() hack.

This commit is contained in:
bjorn
2008-09-22 10:11:15 +00:00
parent 0031276944
commit 4862018f14
2 changed files with 27 additions and 24 deletions

View File

@@ -28,27 +28,30 @@ public class GF {
public void onError (Throwable e) ;
}
public static <T extends JavaScriptObject> Iterable<T> iterable(final JsArray<T> array) {
return new Iterable<T>() {
public Iterator<T> iterator() {
return new Iterator<T>() {
int i = 0;
public static class IterableJsArray<T extends JavaScriptObject> extends JsArray<T> {
protected IterableJsArray() {}
public boolean hasNext() {
return i < array.length();
}
public T next() {
if (!hasNext()) {
throw new NoSuchElementException();
public final Iterable<T> iterable() {
return new Iterable<T>() {
public Iterator<T> iterator() {
return new Iterator<T>() {
private int i = 0;
public boolean hasNext() {
return i < length();
}
return array.get(i++);
}
public void remove() {
throw new UnsupportedOperationException();
}
};
}
};
public T next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
return get(i++);
}
public void remove() {
throw new UnsupportedOperationException();
}
};
}
};
}
}
/* Grammar */
@@ -66,7 +69,7 @@ public class GF {
public final native String getUserLanguage() /*-{ return this.userLanguage; }-*/;
public final native JsArray<Language> getLanguages() /*-{ return this.languages; }-*/;
public final native IterableJsArray<Language> getLanguages() /*-{ return this.languages; }-*/;
public final Language getLanguage(String name) {
int c = getLanguages().length();
@@ -109,7 +112,7 @@ public class GF {
public interface TranslateCallback extends GFCallback<Translations> { }
public static class Translations extends JsArray<Translation> {
public static class Translations extends IterableJsArray<Translation> {
protected Translations() { }
}
@@ -138,7 +141,7 @@ public class GF {
public interface CompleteCallback extends GFCallback<Completions> { }
public static class Completions extends JsArray<Translation> {
public static class Completions extends IterableJsArray<Translation> {
protected Completions() { }
}

View File

@@ -60,7 +60,7 @@ public class Translate implements EntryPoint {
gf.translate(suggest.getText(), listBoxSelection(fromLangBox), null,
listBoxSelection(toLangBox), new GF.TranslateCallback() {
public void onResult (GF.Translations translations) {
for (GF.Translation t : GF.iterable(translations)) {
for (GF.Translation t : translations.iterable()) {
addTranslation(t.getText(), t.getTo());
}
clearStatus();
@@ -103,7 +103,7 @@ public class Translate implements EntryPoint {
private void setGrammar(GF.Grammar grammar) {
this.grammar = grammar;
for (GF.Language l :GF.iterable(grammar.getLanguages())) {
for (GF.Language l : grammar.getLanguages().iterable()) {
String name = l.getName();
if (l.canParse()) {
fromLangBox.addItem(name);