Clover Coverage Report
Coverage timestamp: Fri May 9 2008 10:54:27 EST
../../../../img/srcFileCovDistChart7.png 63% of files have more coverage
16   80   8   3.2
6   39   0.5   5
5     1.6  
1    
 
  CachingWrapperFilter       Line # 31 16 8 63% 0.6296296
 
  (1)
 
1    package org.apache.lucene.search;
2   
3    /**
4    * Copyright 2004 The Apache Software Foundation
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10    * http://www.apache.org/licenses/LICENSE-2.0
11    *
12    * Unless required by applicable law or agreed to in writing, software
13    * distributed under the License is distributed on an "AS IS" BASIS,
14    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15    * See the License for the specific language governing permissions and
16    * limitations under the License.
17    */
18   
19    import org.apache.lucene.index.IndexReader;
20    import java.util.BitSet;
21    import java.util.WeakHashMap;
22    import java.util.Map;
23    import java.io.IOException;
24   
25    /**
26    * Wraps another filter's result and caches it. The caching
27    * behavior is like {@link QueryFilter}. The purpose is to allow
28    * filters to simply filter, and then wrap with this class to add
29    * caching, keeping the two concerns decoupled yet composable.
30    */
 
31    public class CachingWrapperFilter extends Filter {
32    private Filter filter;
33   
34    /**
35    * @todo What about serialization in RemoteSearchable? Caching won't work.
36    * Should transient be removed?
37    */
38    private transient Map cache;
39   
40    /**
41    * @param filter Filter to cache results of
42    */
 
43  1 toggle public CachingWrapperFilter(Filter filter) {
44  1 this.filter = filter;
45    }
46   
 
47  2 toggle public BitSet bits(IndexReader reader) throws IOException {
48  2 if (cache == null) {
49  1 cache = new WeakHashMap();
50    }
51   
52  2 synchronized (cache) { // check cache
53  2 BitSet cached = (BitSet) cache.get(reader);
54  2 if (cached != null) {
55  1 return cached;
56    }
57    }
58   
59  1 final BitSet bits = filter.bits(reader);
60   
61  1 synchronized (cache) { // update cache
62  1 cache.put(reader, bits);
63    }
64   
65  1 return bits;
66    }
67   
 
68  0 toggle public String toString() {
69  0 return "CachingWrapperFilter("+filter+")";
70    }
71   
 
72  0 toggle public boolean equals(Object o) {
73  0 if (!(o instanceof CachingWrapperFilter)) return false;
74  0 return this.filter.equals(((CachingWrapperFilter)o).filter);
75    }
76   
 
77  0 toggle public int hashCode() {
78  0 return filter.hashCode() ^ 0x1117BF25;
79    }
80    }