From page 439:
Note that we have to clone closestToLastWord when assigning to result since otherwise closestToLastWord might change before we encode it into the response.
and code:
public static void service(ServiceRequest req, ServiceResponse resp) {
String w = req.extractWordToCheckFromRequest();
String [] result = null;
synchronized (S2.class) {
if (w.equals(wLast)) {
result = Arrays.copyOf(closestToLastWord, closestToLastWord.length);
}
}
if (result == null) {
result = Spell.closestInDictionary(w);
synchronized (S2.class) {
wLast = w;
closestToLastWord = result;
}
}
resp.encodeIntoResponse(result);
}
It seems to me that it is not necessary to clone closestToLastWord
even when closestToLastWord
may be changed to point to another new array. The old closestToLastWord
(pointed by result
) should still be intact. Am I missing anything?