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: import org.apache.solr.search.function.DocValues;
20: import org.apache.lucene.search.FieldCache;
21:
22: import java.io.IOException;
23:
24: /**
25: * Obtains float field values from the {@link org.apache.lucene.search.FieldCache}
26: * using <code>getFloats()</code>
27: * and makes those values available as other numeric types, casting as needed.
28: *
29: * @author yonik
30: * @version $Id: FloatFieldSource.java 472574 2006-11-08 18:25:52Z yonik $
31: */
32:
33: public class FloatFieldSource extends FieldCacheSource {
34: protected FieldCache.FloatParser parser;
35:
36: public FloatFieldSource(String field) {
37: this (field, null);
38: }
39:
40: public FloatFieldSource(String field, FieldCache.FloatParser parser) {
41: super (field);
42: this .parser = parser;
43: }
44:
45: public String description() {
46: return "float(" + field + ')';
47: }
48:
49: public DocValues getValues(IndexReader reader) throws IOException {
50: final float[] arr = (parser == null) ? cache.getFloats(reader,
51: field) : cache.getFloats(reader, field, parser);
52: return new DocValues() {
53: public float floatVal(int doc) {
54: return arr[doc];
55: }
56:
57: public int intVal(int doc) {
58: return (int) arr[doc];
59: }
60:
61: public long longVal(int doc) {
62: return (long) arr[doc];
63: }
64:
65: public double doubleVal(int doc) {
66: return (double) arr[doc];
67: }
68:
69: public String strVal(int doc) {
70: return Float.toString(arr[doc]);
71: }
72:
73: public String toString(int doc) {
74: return description() + '=' + floatVal(doc);
75: }
76: };
77: }
78:
79: public boolean equals(Object o) {
80: if (o.getClass() != FloatFieldSource.class)
81: return false;
82: FloatFieldSource other = (FloatFieldSource) o;
83: return super .equals(other) && this .parser == null ? other.parser == null
84: : this .parser.getClass() == other.parser.getClass();
85: }
86:
87: public int hashCode() {
88: int h = parser == null ? Float.class.hashCode() : parser
89: .getClass().hashCode();
90: h += super.hashCode();
91: return h;
92: };
93:
94: }
|