~slub.team/goobi-indexserver/3.x

« back to all changes in this revision

Viewing changes to solr/core/src/java/org/apache/solr/search/function/RangeMapFloatFunction.java

  • Committer: Sebastian Meyer
  • Date: 2012-08-03 09:12:40 UTC
  • Revision ID: sebastian.meyer@slub-dresden.de-20120803091240-x6861b0vabq1xror
Remove Lucene and Solr source code and add patches instead
Fix Bug #985487: Auto-suggestion for the search interface

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/**
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
8
 
 *
9
 
 *     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
 
 */
17
 
 
18
 
package org.apache.solr.search.function;
19
 
 
20
 
import org.apache.lucene.index.IndexReader;
21
 
import org.apache.lucene.search.Searcher;
22
 
 
23
 
import java.io.IOException;
24
 
import java.util.Map;
25
 
 
26
 
/**
27
 
 * <code>LinearFloatFunction</code> implements a linear function over
28
 
 * another {@link org.apache.solr.search.function.ValueSource}.
29
 
 * <br>
30
 
 * Normally Used as an argument to a {@link org.apache.solr.search.function.FunctionQuery}
31
 
 *
32
 
 * @version $Id: RangeMapFloatFunction.java 1065312 2011-01-30 16:08:25Z rmuir $
33
 
 */
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;
40
 
 
41
 
  public RangeMapFloatFunction(ValueSource source, float min, float max, float target, Float def) {
42
 
    this.source = source;
43
 
    this.min = min;
44
 
    this.max = max;
45
 
    this.target = target;
46
 
    this.defaultVal = def;
47
 
  }
48
 
 
49
 
  @Override
50
 
  public String description() {
51
 
    return "map(" + source.description() + "," + min + "," + max + "," + target + ")";
52
 
  }
53
 
 
54
 
  @Override
55
 
  public DocValues getValues(Map context, IndexReader reader) throws IOException {
56
 
    final DocValues vals =  source.getValues(context, reader);
57
 
    return new DocValues() {
58
 
      @Override
59
 
      public float floatVal(int doc) {
60
 
        float val = vals.floatVal(doc);
61
 
        return (val>=min && val<=max) ? target : (defaultVal == null ? val : defaultVal);
62
 
      }
63
 
      @Override
64
 
      public int intVal(int doc) {
65
 
        return (int)floatVal(doc);
66
 
      }
67
 
      @Override
68
 
      public long longVal(int doc) {
69
 
        return (long)floatVal(doc);
70
 
      }
71
 
      @Override
72
 
      public double doubleVal(int doc) {
73
 
        return (double)floatVal(doc);
74
 
      }
75
 
      @Override
76
 
      public String strVal(int doc) {
77
 
        return Float.toString(floatVal(doc));
78
 
      }
79
 
      @Override
80
 
      public String toString(int doc) {
81
 
        return "map(" + vals.toString(doc) + ",min=" + min + ",max=" + max + ",target=" + target + ")";
82
 
      }
83
 
    };
84
 
  }
85
 
 
86
 
  @Override
87
 
  public void createWeight(Map context, Searcher searcher) throws IOException {
88
 
    source.createWeight(context, searcher);
89
 
  }
90
 
 
91
 
  @Override
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();
102
 
    return h;
103
 
  }
104
 
 
105
 
  @Override
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)));
114
 
  }
115
 
}