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: * <code>LinearFloatFunction</code> implements a linear function over
24: * another {@link ValueSource}.
25: * <br>
26: * Normally Used as an argument to a {@link FunctionQuery}
27: *
28: * @author yonik
29: * @version $Id: LinearFloatFunction.java 472574 2006-11-08 18:25:52Z yonik $
30: */
31: public class LinearFloatFunction extends ValueSource {
32: protected final ValueSource source;
33: protected final float slope;
34: protected final float intercept;
35:
36: public LinearFloatFunction(ValueSource source, float slope,
37: float intercept) {
38: this .source = source;
39: this .slope = slope;
40: this .intercept = intercept;
41: }
42:
43: public String description() {
44: return slope + "*float(" + source.description() + ")+"
45: + intercept;
46: }
47:
48: public DocValues getValues(IndexReader reader) throws IOException {
49: final DocValues vals = source.getValues(reader);
50: return new DocValues() {
51: public float floatVal(int doc) {
52: return vals.floatVal(doc) * slope + intercept;
53: }
54:
55: public int intVal(int doc) {
56: return (int) floatVal(doc);
57: }
58:
59: public long longVal(int doc) {
60: return (long) floatVal(doc);
61: }
62:
63: public double doubleVal(int doc) {
64: return (double) floatVal(doc);
65: }
66:
67: public String strVal(int doc) {
68: return Float.toString(floatVal(doc));
69: }
70:
71: public String toString(int doc) {
72: return slope + "*float(" + vals.toString(doc) + ")+"
73: + intercept;
74: }
75: };
76: }
77:
78: public int hashCode() {
79: int h = Float.floatToIntBits(slope);
80: h = (h >>> 2) | (h << 30);
81: h += Float.floatToIntBits(intercept);
82: h ^= (h << 14) | (h >>> 19);
83: return h + source.hashCode();
84: }
85:
86: public boolean equals(Object o) {
87: if (LinearFloatFunction.class != o.getClass())
88: return false;
89: LinearFloatFunction other = (LinearFloatFunction) o;
90: return this.slope == other.slope
91: && this.intercept == other.intercept
92: && this.source.equals(other.source);
93: }
94: }
|