1 package org.apache.lucene.facet.util;
3 import java.io.IOException;
4 import java.util.ArrayList;
7 import org.apache.lucene.facet.search.CategoryListIterator;
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
17 * http://www.apache.org/licenses/LICENSE-2.0
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.
27 * Iterates over multiple {@link CategoryListIterator}s, consuming the provided
30 * @lucene.experimental
32 public class MultiCategoryListIterator implements CategoryListIterator {
34 private final CategoryListIterator[] iterators;
35 private final List<CategoryListIterator> validIterators;
36 private final List<CategoryListIterator> perDocValidIterators;
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>();
45 /** Fails if all given iterators fail to init */
46 public boolean init() throws IOException {
47 for (CategoryListIterator cli : iterators) {
49 validIterators.add(cli);
52 return !validIterators.isEmpty();
56 * Return a value larger than {@link Integer#MAX_VALUE} only if all
57 * iterators are exhausted
59 public long nextCategory() throws IOException {
60 while (!perDocValidIterators.isEmpty()) {
61 long value = perDocValidIterators.get(0).nextCategory();
62 if (value <= Integer.MAX_VALUE) {
65 perDocValidIterators.remove(0);
71 * Fails only if skipTo on all the provided iterators returned {@code false}
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);
80 return !perDocValidIterators.isEmpty();