add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / facet / src / java / org / apache / lucene / facet / search / aggregator / association / AssociationIntSumAggregator.java
1 package org.apache.lucene.facet.search.aggregator.association;
2
3 import java.io.IOException;
4
5 import org.apache.lucene.facet.enhancements.association.AssociationsPayloadIterator;
6 import org.apache.lucene.facet.index.params.CategoryListParams;
7 import org.apache.lucene.facet.search.aggregator.Aggregator;
8 import org.apache.lucene.index.IndexReader;
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 {@link Aggregator} which updates the weight of a category by summing the
29  * weights of the integer association it finds for every document.
30  * 
31  * @lucene.experimental
32  */
33 public class AssociationIntSumAggregator implements Aggregator {
34
35   protected final String field;
36   protected final int[] sumArray;
37   protected final AssociationsPayloadIterator associationsPayloadIterator;
38
39   public AssociationIntSumAggregator(IndexReader reader, int[] sumArray) throws IOException {
40     this(CategoryListParams.DEFAULT_TERM.field(), reader, sumArray);
41   }
42   
43   public AssociationIntSumAggregator(String field, IndexReader reader, int[] sumArray) throws IOException {
44     this.field = field;
45     associationsPayloadIterator = new AssociationsPayloadIterator(reader, field);
46     this.sumArray = sumArray;
47   }
48
49   public void aggregate(int ordinal) {
50     long association = associationsPayloadIterator.getAssociation(ordinal);
51     if (association != AssociationsPayloadIterator.NO_ASSOCIATION) {
52       sumArray[ordinal] += association;
53     }
54   }
55
56   @Override
57   public boolean equals(Object obj) {
58     if (obj == null || obj.getClass() != this.getClass()) {
59       return false;
60     }
61     AssociationIntSumAggregator that = (AssociationIntSumAggregator) obj;
62     return that.field.equals(field) && that.sumArray == sumArray;
63   }
64
65   @Override
66   public int hashCode() {
67     return field.hashCode();
68   }
69
70   public void setNextDoc(int docid, float score) throws IOException {
71     associationsPayloadIterator.setNextDoc(docid);
72   }
73   
74 }