01: /**
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */package org.apache.solr.search.function;
17:
18: import org.apache.lucene.index.IndexReader;
19:
20: import java.io.IOException;
21:
22: /**
23: * Returns the max of a ValueSource and a float
24: * (which is useful for "bottoming out" another function at 0.0,
25: * or some positive number).
26: * <br>
27: * Normally Used as an argument to a {@link FunctionQuery}
28: *
29: * @author hossman
30: * @version $Id: MaxFloatFunction.java 472574 2006-11-08 18:25:52Z yonik $
31: */
32: public class MaxFloatFunction extends ValueSource {
33: protected final ValueSource source;
34: protected final float fval;
35:
36: public MaxFloatFunction(ValueSource source, float fval) {
37: this .source = source;
38: this .fval = fval;
39: }
40:
41: public String description() {
42: return "max(" + source.description() + "," + fval + ")";
43: }
44:
45: public DocValues getValues(IndexReader reader) throws IOException {
46: final DocValues vals = source.getValues(reader);
47: return new DocValues() {
48: public float floatVal(int doc) {
49: float v = vals.floatVal(doc);
50: return v < fval ? fval : v;
51: }
52:
53: public int intVal(int doc) {
54: return (int) floatVal(doc);
55: }
56:
57: public long longVal(int doc) {
58: return (long) floatVal(doc);
59: }
60:
61: public double doubleVal(int doc) {
62: return (double) floatVal(doc);
63: }
64:
65: public String strVal(int doc) {
66: return Float.toString(floatVal(doc));
67: }
68:
69: public String toString(int doc) {
70: return "max(" + vals.toString(doc) + "," + fval + ")";
71: }
72: };
73: }
74:
75: public int hashCode() {
76: int h = Float.floatToIntBits(fval);
77: h = (h >>> 2) | (h << 30);
78: return h + source.hashCode();
79: }
80:
81: public boolean equals(Object o) {
82: if (MaxFloatFunction.class != o.getClass())
83: return false;
84: MaxFloatFunction other = (MaxFloatFunction) o;
85: return this.fval == other.fval
86: && this.source.equals(other.source);
87: }
88: }
|