public void checkSpelling(
List<String> words,
final ServerRequestCallback<SpellCheckerResult> callback)
{
// results to return
final SpellCheckerResult spellCheckerResult = new SpellCheckerResult();
// only send words to the server that aren't in the cache
final ArrayList<String> wordsToCheck = new ArrayList<String>();
for (int i = 0; i<words.size(); i++)
{
String word = words.get(i);
Boolean isCorrect = previousResults_.get(word);
if (isCorrect != null)
{
if (isCorrect)
spellCheckerResult.getCorrect().add(word);
else
spellCheckerResult.getIncorrect().add(word);
}
else
{
wordsToCheck.add(word);
}
}
// if there are no words to check then return
if (wordsToCheck.size() == 0)
{
callback.onResponseReceived(spellCheckerResult);
return;
}
// hit the server
server_.checkSpelling(JsUtil.toJsArrayString(wordsToCheck),
new ServerRequestCallback<JsArrayInteger>() {
@Override
public void onResponseReceived(JsArrayInteger result)
{
// get misspelled indexes
ArrayList<Integer> misspelledIndexes = new ArrayList<Integer>();
for (int i=0; i<result.length(); i++)
misspelledIndexes.add(result.get(i));
// determine correct/incorrect status and populate result & cache
for (int i=0; i<wordsToCheck.size(); i++)
{
String word = wordsToCheck.get(i);
if (misspelledIndexes.contains(i))
{
spellCheckerResult.getIncorrect().add(word);
previousResults_.put(word, false);
}
else
{
spellCheckerResult.getCorrect().add(word);
previousResults_.put(word, true);
}
}
// return result