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