pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / facet / src / java / org / apache / lucene / facet / enhancements / EnhancementsPayloadIterator.java
1 package org.apache.lucene.facet.enhancements;
2
3 import java.io.IOException;
4 import java.util.List;
5
6 import org.apache.lucene.index.IndexReader;
7 import org.apache.lucene.index.Term;
8
9 import org.apache.lucene.facet.search.PayloadIterator;
10 import org.apache.lucene.util.Vint8;
11 import org.apache.lucene.util.Vint8.Position;
12
13 /**
14  * Licensed to the Apache Software Foundation (ASF) under one or more
15  * contributor license agreements.  See the NOTICE file distributed with
16  * this work for additional information regarding copyright ownership.
17  * The ASF licenses this file to You under the Apache License, Version 2.0
18  * (the "License"); you may not use this file except in compliance with
19  * the License.  You may obtain a copy of the License at
20  *
21  *     http://www.apache.org/licenses/LICENSE-2.0
22  *
23  * Unless required by applicable law or agreed to in writing, software
24  * distributed under the License is distributed on an "AS IS" BASIS,
25  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
26  * See the License for the specific language governing permissions and
27  * limitations under the License.
28  */
29
30 /**
31  * A {@link PayloadIterator} for iterating over category posting lists generated
32  * using {@link EnhancementsCategoryTokenizer}.
33  * 
34  * @lucene.experimental
35  */
36 public class EnhancementsPayloadIterator extends PayloadIterator {
37
38   private CategoryEnhancement[] EnhancedCategories;
39   int nEnhancements;
40   private int[] enhancementLength;
41   private int[] enhancementStart;
42
43   /**
44    * Constructor.
45    * 
46    * @param enhancementsList
47    *            A list of the {@link CategoryEnhancement}s from the indexing
48    *            params.
49    * @param indexReader
50    *            A reader of the index.
51    * @param term
52    *            The category term to iterate.
53    * @throws IOException
54    */
55   public EnhancementsPayloadIterator(
56       List<CategoryEnhancement> enhancementsList,
57       IndexReader indexReader, Term term) throws IOException {
58     super(indexReader, term);
59     EnhancedCategories = enhancementsList
60         .toArray(new CategoryEnhancement[enhancementsList.size()]);
61     enhancementLength = new int[EnhancedCategories.length];
62     enhancementStart = new int[EnhancedCategories.length];
63   }
64
65   @Override
66   public boolean setdoc(int docId) throws IOException {
67     if (!super.setdoc(docId)) {
68       return false;
69     }
70
71     // read header - number of enhancements and their lengths
72     Position position = new Position();
73     nEnhancements = Vint8.decode(buffer, position);
74     for (int i = 0; i < nEnhancements; i++) {
75       enhancementLength[i] = Vint8.decode(buffer, position);
76     }
77
78     // set enhancements start points
79     enhancementStart[0] = position.pos;
80     for (int i = 1; i < nEnhancements; i++) {
81       enhancementStart[i] = enhancementStart[i - 1] + enhancementLength[i - 1];
82     }
83
84     return true;
85   }
86
87   /**
88    * Get the data of the current category and document for a certain
89    * enhancement, or {@code null} if no such enhancement exists.
90    * 
91    * @param enhancedCategory
92    *            The category enhancement to apply.
93    * @return the data of the current category and document for a certain
94    *         enhancement, or {@code null} if no such enhancement exists.
95    */
96   public Object getCategoryData(CategoryEnhancement enhancedCategory) {
97     for (int i = 0; i < nEnhancements; i++) {
98       if (enhancedCategory.equals(EnhancedCategories[i])) {
99         return enhancedCategory.extractCategoryTokenData(buffer,
100             enhancementStart[i], enhancementLength[i]);
101       }
102     }
103     return null;
104   }
105 }