mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-04-09 04:59:31 -06:00
Added iterable() method for nicer iteration through GF method results. (GWT)
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user