2
* Licensed to the Apache Software Foundation (ASF) under one or more
3
* contributor license agreements. See the NOTICE file distributed with
4
* this work for additional information regarding copyright ownership.
5
* The ASF licenses this file to You under the Apache License, Version 2.0
6
* (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
9
* http://www.apache.org/licenses/LICENSE-2.0
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.
18
package org.apache.solr.search.function;
20
import org.apache.lucene.index.IndexReader;
21
import org.apache.lucene.search.Searcher;
23
import java.io.IOException;
27
* <code>LinearFloatFunction</code> implements a linear function over
28
* another {@link org.apache.solr.search.function.ValueSource}.
30
* Normally Used as an argument to a {@link org.apache.solr.search.function.FunctionQuery}
32
* @version $Id: RangeMapFloatFunction.java 1065312 2011-01-30 16:08:25Z rmuir $
34
public class RangeMapFloatFunction extends ValueSource {
35
protected final ValueSource source;
36
protected final float min;
37
protected final float max;
38
protected final float target;
39
protected final Float defaultVal;
41
public RangeMapFloatFunction(ValueSource source, float min, float max, float target, Float def) {
46
this.defaultVal = def;
50
public String description() {
51
return "map(" + source.description() + "," + min + "," + max + "," + target + ")";
55
public DocValues getValues(Map context, IndexReader reader) throws IOException {
56
final DocValues vals = source.getValues(context, reader);
57
return new DocValues() {
59
public float floatVal(int doc) {
60
float val = vals.floatVal(doc);
61
return (val>=min && val<=max) ? target : (defaultVal == null ? val : defaultVal);
64
public int intVal(int doc) {
65
return (int)floatVal(doc);
68
public long longVal(int doc) {
69
return (long)floatVal(doc);
72
public double doubleVal(int doc) {
73
return (double)floatVal(doc);
76
public String strVal(int doc) {
77
return Float.toString(floatVal(doc));
80
public String toString(int doc) {
81
return "map(" + vals.toString(doc) + ",min=" + min + ",max=" + max + ",target=" + target + ")";
87
public void createWeight(Map context, Searcher searcher) throws IOException {
88
source.createWeight(context, searcher);
92
public int hashCode() {
93
int h = source.hashCode();
94
h ^= (h << 10) | (h >>> 23);
95
h += Float.floatToIntBits(min);
96
h ^= (h << 14) | (h >>> 19);
97
h += Float.floatToIntBits(max);
98
h ^= (h << 13) | (h >>> 20);
99
h += Float.floatToIntBits(target);
100
if (defaultVal != null)
101
h += defaultVal.hashCode();
106
public boolean equals(Object o) {
107
if (RangeMapFloatFunction.class != o.getClass()) return false;
108
RangeMapFloatFunction other = (RangeMapFloatFunction)o;
109
return this.min == other.min
110
&& this.max == other.max
111
&& this.target == other.target
112
&& this.source.equals(other.source)
113
&& (this.defaultVal == other.defaultVal || (this.defaultVal != null && this.defaultVal.equals(other.defaultVal)));