pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / facet / src / java / org / apache / lucene / facet / index / streaming / CategoryAttributesStream.java
1 package org.apache.lucene.facet.index.streaming;
2
3 import java.io.IOException;
4 import java.util.Iterator;
5
6 import org.apache.lucene.analysis.TokenStream;
7
8 import org.apache.lucene.facet.index.attributes.CategoryAttribute;
9
10 /**
11  * Licensed to the Apache Software Foundation (ASF) under one or more
12  * contributor license agreements.  See the NOTICE file distributed with
13  * this work for additional information regarding copyright ownership.
14  * The ASF licenses this file to You under the Apache License, Version 2.0
15  * (the "License"); you may not use this file except in compliance with
16  * the License.  You may obtain a copy of the License at
17  *
18  *     http://www.apache.org/licenses/LICENSE-2.0
19  *
20  * Unless required by applicable law or agreed to in writing, software
21  * distributed under the License is distributed on an "AS IS" BASIS,
22  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23  * See the License for the specific language governing permissions and
24  * limitations under the License.
25  */
26
27 /**
28  * An attribute stream built from an {@link Iterable} of
29  * {@link CategoryAttribute}. This stream should then be passed through several
30  * filters (see {@link CategoryParentsStream}, {@link CategoryListTokenizer} and
31  * {@link CategoryTokenizer}) until a token stream is produced that can be
32  * indexed by Lucene.
33  * <P>
34  * A CategoryAttributesStream object can be reused for producing more than one
35  * stream. To do that, the user should cause the underlying
36  * Iterable<CategoryAttribute> object to return a new set of categories, and
37  * then call {@link #reset()} to allow this stream to be used again.
38  * 
39  * @lucene.experimental
40  */
41 public class CategoryAttributesStream extends TokenStream {
42
43   protected CategoryAttribute categoryAttribute;
44
45   private Iterable<CategoryAttribute> iterable;
46   private Iterator<CategoryAttribute> iterator;
47
48   /**
49    * Constructor
50    * 
51    * @param iterable
52    *            {@link Iterable} of {@link CategoryAttribute}, from which
53    *            categories are taken.
54    */
55   public CategoryAttributesStream(Iterable<CategoryAttribute> iterable) {
56     this.iterable = iterable;
57     this.iterator = null;
58     this.categoryAttribute = this.addAttribute(CategoryAttribute.class);
59   }
60
61   @Override
62   public final boolean incrementToken() throws IOException {
63     if (iterator == null) {
64       if (iterable == null) {
65         return false;
66       }
67       iterator = iterable.iterator();
68     }
69     if (iterator.hasNext()) {
70       categoryAttribute.set(iterator.next());
71       return true;
72     }
73     return false;
74   }
75
76   @Override
77   public void reset() {
78     this.iterator = null;
79   }
80
81 }