Last active
May 7, 2020 06:39
-
-
Save xhanin/0be9c58dc37fe624a8d50001a83c13c7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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