Added iterable() method for nicer iteration through GF method results. (GWT)

This commit is contained in:
bjorn
2008-09-21 20:37:27 +00:00
parent e6f5033bc5
commit 0031276944
2 changed files with 27 additions and 4 deletions

View File

@@ -10,7 +10,9 @@ import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONParser;
import java.util.Set;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.ArrayList;
public class GF {
@@ -26,6 +28,29 @@ 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 boolean hasNext() {
return i < array.length();
}
public T next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
return array.get(i++);
}
public void remove() {
throw new UnsupportedOperationException();
}
};
}
};
}
/* Grammar */
public GFRequest grammar (final GrammarCallback callback) {

View File

@@ -60,8 +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 (int i = 0; i < translations.length(); i++) {
GF.Translation t = translations.get(i);
for (GF.Translation t : GF.iterable(translations)) {
addTranslation(t.getText(), t.getTo());
}
clearStatus();
@@ -104,8 +103,7 @@ public class Translate implements EntryPoint {
private void setGrammar(GF.Grammar grammar) {
this.grammar = grammar;
for (int i = 0; i < grammar.getLanguages().length(); i++) {
GF.Language l = grammar.getLanguages().get(i);
for (GF.Language l :GF.iterable(grammar.getLanguages())) {
String name = l.getName();
if (l.canParse()) {
fromLangBox.addItem(name);