Skip to content

Instantly share code, notes, and snippets.

@xhanin
Last active May 7, 2020 06:39
Show Gist options
  • Save xhanin/0be9c58dc37fe624a8d50001a83c13c7 to your computer and use it in GitHub Desktop.
Save xhanin/0be9c58dc37fe624a8d50001a83c13c7 to your computer and use it in GitHub Desktop.
public class SafeCachingFactorizer implements Servlet {
private static class FactorResult { BigInteger number; BigInteger[] factors; }
private volatile FactorResult lastResult = null;
public void service(ServletRequest req, ServletResponse resp) {
BigInteger i = extractFromRequest(req);
FactorResult result = this.lastResult;
if (result == null || !i.equals(result.number)) {
result = new FactorResult() { { number = i; factors = factor(i); } };
this.lastResult = result;
}
encodeIntoResponse(resp, result.factors);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment