add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / src / java / org / apache / lucene / util / DocIdBitSet.java
1 package org.apache.lucene.util;
2
3 /**
4  * Licensed to the Apache Software Foundation (ASF) under one or more
5  * contributor license agreements.  See the NOTICE file distributed with
6  * this work for additional information regarding copyright ownership.
7  * The ASF licenses this file to You under the Apache License, Version 2.0
8  * (the "License"); you may not use this file except in compliance with
9  * the License.  You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 import java.util.BitSet;
21
22 import org.apache.lucene.search.DocIdSet;
23 import org.apache.lucene.search.DocIdSetIterator;
24
25
26 /** Simple DocIdSet and DocIdSetIterator backed by a BitSet */
27 public class DocIdBitSet extends DocIdSet {
28   private BitSet bitSet;
29     
30   public DocIdBitSet(BitSet bitSet) {
31     this.bitSet = bitSet;
32   }
33
34   @Override
35   public DocIdSetIterator iterator() {
36     return new DocIdBitSetIterator(bitSet);
37   }
38
39   /** This DocIdSet implementation is cacheable. */
40   @Override
41   public boolean isCacheable() {
42     return true;
43   }
44   
45   /**
46    * Returns the underlying BitSet. 
47    */
48   public BitSet getBitSet() {
49         return this.bitSet;
50   }
51   
52   private static class DocIdBitSetIterator extends DocIdSetIterator {
53     private int docId;
54     private BitSet bitSet;
55     
56     DocIdBitSetIterator(BitSet bitSet) {
57       this.bitSet = bitSet;
58       this.docId = -1;
59     }
60     
61     @Override
62     public int docID() {
63       return docId;
64     }
65     
66     @Override
67     public int nextDoc() {
68       // (docId + 1) on next line requires -1 initial value for docNr:
69       int d = bitSet.nextSetBit(docId + 1);
70       // -1 returned by BitSet.nextSetBit() when exhausted
71       docId = d == -1 ? NO_MORE_DOCS : d;
72       return docId;
73     }
74   
75     @Override
76     public int advance(int target) {
77       int d = bitSet.nextSetBit(target);
78       // -1 returned by BitSet.nextSetBit() when exhausted
79       docId = d == -1 ? NO_MORE_DOCS : d;
80       return docId;
81     }
82   }
83 }