jcp.ch01.basic.CachedFactorizer

 1 package book.jcp.ch01.basic;
 2 
 3 import java.math.BigInteger;
 4 
 5 public class CachedFactorizer {
 6 
 7     private BigInteger lastNumberBigInteger;
 8     private BigInteger[] lastFactorsBigIntegers;
 9     private long hits;
10     private long cacheHits;
11 
12     public synchronized long getHits() {
13         return hits;
14     }
15 
16     public synchronized double getHitRatio() {
17         return (double) cacheHits / (double) hits;
18     }
19 
20     public void service(int req, int resp) {
21         BigInteger i = extractFromRequest(req);
22         BigInteger[] newFactors = null;
23         synchronized (this) {
24             ++hits;
25             if (i.equals(lastNumberBigInteger)) {
26                 ++cacheHits;
27                 newFactors = lastFactorsBigIntegers.clone();
28             }
29         }
30         if (newFactors == null) {
31             newFactors = factor(i);
32             synchronized (this) {
33                 lastNumberBigInteger = i;
34                 lastFactorsBigIntegers = newFactors.clone();
35             }
36         }
37         encodeIntoResponse(resp, newFactors);
38     }
39 
40     private BigInteger[] factor(BigInteger i) {
41         // TODO Auto-generated method stub
42         return null;
43     }
44 
45     private void encodeIntoResponse(int resp, BigInteger[] factors) {
46         // TODO Auto-generated method stub
47 
48     }
49 
50     private BigInteger extractFromRequest(int req) {
51         // TODO Auto-generated method stub
52         return null;
53     }
54 }

 

posted @ 2014-08-27 11:10  达度  阅读(386)  评论(0编辑  收藏  举报