pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / java / org / apache / lucene / search / function / FloatFieldSource.java
1 package org.apache.lucene.search.function;
2
3 /**
4  * Licensed to the Apache Software Foundation (ASF) under one or more
5  * contributor license agreements.  See the NOTICE file distributed with
6  * this work for additional information regarding copyright ownership.
7  * The ASF licenses this file to You under the Apache License, Version 2.0
8  * (the "License"); you may not use this file except in compliance with
9  * the License.  You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 import org.apache.lucene.index.IndexReader;
21 import org.apache.lucene.search.FieldCache;
22 import org.apache.lucene.search.function.DocValues;
23
24 import java.io.IOException;
25
26 /**
27  * Expert: obtains float field values from the 
28  * {@link org.apache.lucene.search.FieldCache FieldCache}
29  * using <code>getFloats()</code> and makes those values 
30  * available as other numeric types, casting as needed.
31  * 
32  * @lucene.experimental
33  * 
34  * @see org.apache.lucene.search.function.FieldCacheSource for requirements 
35  * on the field.
36  *
37  * <p><b>NOTE</b>: with the switch in 2.9 to segment-based
38  * searching, if {@link #getValues} is invoked with a
39  * composite (multi-segment) reader, this can easily cause
40  * double RAM usage for the values in the FieldCache.  It's
41  * best to switch your application to pass only atomic
42  * (single segment) readers to this API.</p>
43  */
44 public class FloatFieldSource extends FieldCacheSource {
45   private FieldCache.FloatParser parser;
46
47   /**
48    * Create a cached float field source with default string-to-float parser. 
49    */
50   public FloatFieldSource(String field) {
51     this(field, null);
52   }
53
54   /**
55    * Create a cached float field source with a specific string-to-float parser. 
56    */
57   public FloatFieldSource(String field, FieldCache.FloatParser parser) {
58     super(field);
59     this.parser = parser;
60   }
61
62   /*(non-Javadoc) @see org.apache.lucene.search.function.ValueSource#description() */
63   @Override
64   public String description() {
65     return "float(" + super.description() + ')';
66   }
67
68   /*(non-Javadoc) @see org.apache.lucene.search.function.FieldCacheSource#getCachedValues(org.apache.lucene.search.FieldCache, java.lang.String, org.apache.lucene.index.IndexReader) */
69   @Override
70   public DocValues getCachedFieldValues (FieldCache cache, String field, IndexReader reader) throws IOException {
71     final float[] arr = cache.getFloats(reader, field, parser);
72     return new DocValues() {
73       /*(non-Javadoc) @see org.apache.lucene.search.function.DocValues#floatVal(int) */
74       @Override
75       public float floatVal(int doc) {
76         return arr[doc];      
77       }
78       /*(non-Javadoc) @see org.apache.lucene.search.function.DocValues#toString(int) */
79       @Override
80       public String toString(int doc) { 
81         return  description() + '=' + arr[doc];  
82       }
83       /*(non-Javadoc) @see org.apache.lucene.search.function.DocValues#getInnerArray() */
84       @Override
85       Object getInnerArray() {
86         return arr;
87       }
88     };
89   }
90
91   /*(non-Javadoc) @see org.apache.lucene.search.function.FieldCacheSource#cachedFieldSourceEquals(org.apache.lucene.search.function.FieldCacheSource) */
92   @Override
93   public boolean cachedFieldSourceEquals(FieldCacheSource o) {
94     if (o.getClass() !=  FloatFieldSource.class) {
95       return false;
96     }
97     FloatFieldSource other = (FloatFieldSource)o;
98     return this.parser==null ? 
99       other.parser==null :
100       this.parser.getClass() == other.parser.getClass();
101   }
102
103   /*(non-Javadoc) @see org.apache.lucene.search.function.FieldCacheSource#cachedFieldSourceHashCode() */
104   @Override
105   public int cachedFieldSourceHashCode() {
106     return parser==null ? 
107       Float.class.hashCode() : parser.getClass().hashCode();
108   }
109 }