pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / java / org / apache / lucene / search / payloads / MaxPayloadFunction.java
1 package org.apache.lucene.search.payloads;
2
3 import org.apache.lucene.search.Explanation;
4 /**
5  * Licensed to the Apache Software Foundation (ASF) under one or more
6  * contributor license agreements.  See the NOTICE file distributed with
7  * this work for additional information regarding copyright ownership.
8  * The ASF licenses this file to You under the Apache License, Version 2.0
9  * (the "License"); you may not use this file except in compliance with
10  * the License.  You may obtain a copy of the License at
11  *
12  *     http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20
21
22 /**
23  * Returns the maximum payload score seen, else 1 if there are no payloads on the doc.
24  * <p/>
25  * Is thread safe and completely reusable.
26  *
27  **/
28 public class MaxPayloadFunction extends PayloadFunction {
29   @Override
30   public float currentScore(int docId, String field, int start, int end, int numPayloadsSeen, float currentScore, float currentPayloadScore) {
31     if (numPayloadsSeen == 0) {
32       return currentPayloadScore;
33     } else {
34       return Math.max(currentPayloadScore, currentScore);
35     }
36   }
37
38   @Override
39   public float docScore(int docId, String field, int numPayloadsSeen, float payloadScore) {
40     return numPayloadsSeen > 0 ? payloadScore : 1;
41   }
42   
43   @Override
44   public Explanation explain(int doc, int numPayloadsSeen, float payloadScore) {
45             Explanation expl = new Explanation();
46             float maxPayloadScore = (numPayloadsSeen > 0 ? payloadScore : 1);
47             expl.setValue(maxPayloadScore);
48             expl.setDescription("MaxPayloadFunction(...)");
49             return expl;
50           } 
51   @Override
52   public int hashCode() {
53     final int prime = 31;
54     int result = 1;
55     result = prime * result + this.getClass().hashCode();
56     return result;
57   }
58
59   @Override
60   public boolean equals(Object obj) {
61     if (this == obj)
62       return true;
63     if (obj == null)
64       return false;
65     if (getClass() != obj.getClass())
66       return false;
67     return true;
68   }
69 }