001: package org.apache.lucene.search.function;
002:
003: /**
004: * Licensed to the Apache Software Foundation (ASF) under one or more
005: * contributor license agreements. See the NOTICE file distributed with
006: * this work for additional information regarding copyright ownership.
007: * The ASF licenses this file to You under the Apache License, Version 2.0
008: * (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: */
019:
020: import org.apache.lucene.index.IndexReader;
021: import org.apache.lucene.search.FieldCache;
022: import org.apache.lucene.search.function.DocValues;
023:
024: import java.io.IOException;
025:
026: /**
027: * Expert: obtains float field values from the
028: * {@link org.apache.lucene.search.FieldCache FieldCache}
029: * using <code>getFloats()</code> and makes those values
030: * available as other numeric types, casting as needed.
031: *
032: * <p><font color="#FF0000">
033: * WARNING: The status of the <b>search.function</b> package is experimental.
034: * The APIs introduced here might change in the future and will not be
035: * supported anymore in such a case.</font>
036: *
037: * @see org.apache.lucene.search.function.FieldCacheSource for requirements
038: * on the field.
039: *
040: * @author yonik
041: */
042: public class FloatFieldSource extends FieldCacheSource {
043: private FieldCache.FloatParser parser;
044:
045: /**
046: * Create a cached float field source with default string-to-float parser.
047: */
048: public FloatFieldSource(String field) {
049: this (field, null);
050: }
051:
052: /**
053: * Create a cached float field source with a specific string-to-float parser.
054: */
055: public FloatFieldSource(String field, FieldCache.FloatParser parser) {
056: super (field);
057: this .parser = parser;
058: }
059:
060: /*(non-Javadoc) @see org.apache.lucene.search.function.ValueSource#description() */
061: public String description() {
062: return "float(" + super .description() + ')';
063: }
064:
065: /*(non-Javadoc) @see org.apache.lucene.search.function.FieldCacheSource#getCachedValues(org.apache.lucene.search.FieldCache, java.lang.String, org.apache.lucene.index.IndexReader) */
066: public DocValues getCachedFieldValues(FieldCache cache,
067: String field, IndexReader reader) throws IOException {
068: final float[] arr = (parser == null) ? cache.getFloats(reader,
069: field) : cache.getFloats(reader, field, parser);
070: return new DocValues() {
071: /*(non-Javadoc) @see org.apache.lucene.search.function.DocValues#floatVal(int) */
072: public float floatVal(int doc) {
073: return arr[doc];
074: }
075:
076: /*(non-Javadoc) @see org.apache.lucene.search.function.DocValues#toString(int) */
077: public String toString(int doc) {
078: return description() + '=' + arr[doc];
079: }
080:
081: /*(non-Javadoc) @see org.apache.lucene.search.function.DocValues#getInnerArray() */
082: Object getInnerArray() {
083: return arr;
084: }
085: };
086: }
087:
088: /*(non-Javadoc) @see org.apache.lucene.search.function.FieldCacheSource#cachedFieldSourceEquals(org.apache.lucene.search.function.FieldCacheSource) */
089: public boolean cachedFieldSourceEquals(FieldCacheSource o) {
090: if (o.getClass() != FloatFieldSource.class) {
091: return false;
092: }
093: FloatFieldSource other = (FloatFieldSource) o;
094: return this .parser == null ? other.parser == null : this .parser
095: .getClass() == other.parser.getClass();
096: }
097:
098: /*(non-Javadoc) @see org.apache.lucene.search.function.FieldCacheSource#cachedFieldSourceHashCode() */
099: public int cachedFieldSourceHashCode() {
100: return parser == null ? Float.class.hashCode() : parser
101: .getClass().hashCode();
102: }
103: }
|