add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / facet / src / java / org / apache / lucene / facet / index / categorypolicy / NonTopLevelOrdinalPolicy.java
1 package org.apache.lucene.facet.index.categorypolicy;
2
3 import org.apache.lucene.facet.taxonomy.TaxonomyReader;
4 import org.apache.lucene.facet.taxonomy.TaxonomyWriter;
5
6 /**
7  * Licensed to the Apache Software Foundation (ASF) under one or more
8  * contributor license agreements.  See the NOTICE file distributed with
9  * this work for additional information regarding copyright ownership.
10  * The ASF licenses this file to You under the Apache License, Version 2.0
11  * (the "License"); you may not use this file except in compliance with
12  * the License.  You may obtain a copy of the License at
13  *
14  *     http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  */
22
23 /**
24  * Filter out any "top level" category ordinals. <br> {@link #shouldAdd(int)}.
25  * 
26  * @lucene.experimental
27  */
28 public class NonTopLevelOrdinalPolicy implements OrdinalPolicy {
29
30   /**
31    * The taxonomyWriter with which the given ordinals' parent is determined.
32    */
33   private TaxonomyWriter taxonomyWriter;
34
35   /**
36    * Constructs a new non-top-level-ordinal-filter. With a given
37    * taxonomyWriter.
38    * 
39    */
40   public NonTopLevelOrdinalPolicy() {
41     this.taxonomyWriter = null;
42   }
43
44   /** 
45    * @param taxonomyWriter
46    *            A relevant taxonomyWriter object, with which ordinals sent to
47    *            {@link #shouldAdd(int)} are examined.
48    */
49   public void init(TaxonomyWriter taxonomyWriter) {
50     this.taxonomyWriter = taxonomyWriter;
51   }
52   
53   /**
54    * Filters out ordinal which are ROOT or who's parent is ROOT. In order to
55    * determine if a parent is root, there's a need for
56    * {@link TaxonomyWriter#getParent(int)}.
57    */
58   public boolean shouldAdd(int ordinal) {
59     if (ordinal > TaxonomyReader.ROOT_ORDINAL) {
60       try {
61         if (this.taxonomyWriter.getParent(ordinal) > TaxonomyReader.ROOT_ORDINAL) {
62           return true;
63         }
64       } catch (Exception e) {
65         return false;
66       }
67     }
68     return false;
69   }
70
71 }