add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / facet / src / java / org / apache / lucene / facet / util / MultiCategoryListIterator.java
1 package org.apache.lucene.facet.util;
2
3 import java.io.IOException;
4 import java.util.ArrayList;
5 import java.util.List;
6
7 import org.apache.lucene.facet.search.CategoryListIterator;
8
9 /**
10  * Licensed to the Apache Software Foundation (ASF) under one or more
11  * contributor license agreements.  See the NOTICE file distributed with
12  * this work for additional information regarding copyright ownership.
13  * The ASF licenses this file to You under the Apache License, Version 2.0
14  * (the "License"); you may not use this file except in compliance with
15  * the License.  You may obtain a copy of the License at
16  *
17  *     http://www.apache.org/licenses/LICENSE-2.0
18  *
19  * Unless required by applicable law or agreed to in writing, software
20  * distributed under the License is distributed on an "AS IS" BASIS,
21  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22  * See the License for the specific language governing permissions and
23  * limitations under the License.
24  */
25
26 /**
27  * Iterates over multiple {@link CategoryListIterator}s, consuming the provided
28  * iterators in order.
29  * 
30  * @lucene.experimental
31  */
32 public class MultiCategoryListIterator implements CategoryListIterator {
33
34   private final CategoryListIterator[] iterators;
35   private final List<CategoryListIterator> validIterators;
36   private final List<CategoryListIterator> perDocValidIterators;
37
38   /** Receives the iterators to iterate on */
39   public MultiCategoryListIterator(CategoryListIterator... iterators) {
40     this.iterators = iterators;
41     this.validIterators = new ArrayList<CategoryListIterator>();
42     this.perDocValidIterators = new ArrayList<CategoryListIterator>();
43   }
44
45   /** Fails if all given iterators fail to init */
46   public boolean init() throws IOException {
47     for (CategoryListIterator cli : iterators) {
48       if (cli.init()) {
49         validIterators.add(cli);
50       }
51     }
52     return !validIterators.isEmpty();
53   }
54
55   /**
56    * Return a value larger than {@link Integer#MAX_VALUE} only if all
57    * iterators are exhausted
58    */
59   public long nextCategory() throws IOException {
60     while (!perDocValidIterators.isEmpty()) {
61       long value = perDocValidIterators.get(0).nextCategory();
62       if (value <= Integer.MAX_VALUE) {
63         return value;
64       }
65       perDocValidIterators.remove(0);
66     }
67     return 0x100000000L;
68   }
69
70   /**
71    * Fails only if skipTo on all the provided iterators returned {@code false}
72    */
73   public boolean skipTo(int docId) throws IOException {
74     perDocValidIterators.clear();
75     for (CategoryListIterator cli : validIterators) {
76       if (cli.skipTo(docId)) {
77         perDocValidIterators.add(cli);
78       }
79     }
80     return !perDocValidIterators.isEmpty();
81   }
82
83 }