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 short field values from the
028: * {@link org.apache.lucene.search.FieldCache FieldCache}
029: * using <code>getShorts()</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: public class ShortFieldSource extends FieldCacheSource {
041: private FieldCache.ShortParser parser;
042:
043: /**
044: * Create a cached short field source with default string-to-short parser.
045: */
046: public ShortFieldSource(String field) {
047: this (field, null);
048: }
049:
050: /**
051: * Create a cached short field source with a specific string-to-short parser.
052: */
053: public ShortFieldSource(String field, FieldCache.ShortParser parser) {
054: super (field);
055: this .parser = parser;
056: }
057:
058: /*(non-Javadoc) @see org.apache.lucene.search.function.ValueSource#description() */
059: public String description() {
060: return "short(" + super .description() + ')';
061: }
062:
063: /*(non-Javadoc) @see org.apache.lucene.search.function.FieldCacheSource#getCachedValues(org.apache.lucene.search.FieldCache, java.lang.String, org.apache.lucene.index.IndexReader) */
064: public DocValues getCachedFieldValues(FieldCache cache,
065: String field, IndexReader reader) throws IOException {
066: final short[] arr = (parser == null) ? cache.getShorts(reader,
067: field) : cache.getShorts(reader, field, parser);
068: return new DocValues() {
069: /*(non-Javadoc) @see org.apache.lucene.search.function.DocValues#floatVal(int) */
070: public float floatVal(int doc) {
071: return (float) arr[doc];
072: }
073:
074: /*(non-Javadoc) @see org.apache.lucene.search.function.DocValues#intVal(int) */
075: public int intVal(int doc) {
076: return arr[doc];
077: }
078:
079: /*(non-Javadoc) @see org.apache.lucene.search.function.DocValues#toString(int) */
080: public String toString(int doc) {
081: return description() + '=' + intVal(doc);
082: }
083:
084: /*(non-Javadoc) @see org.apache.lucene.search.function.DocValues#getInnerArray() */
085: Object getInnerArray() {
086: return arr;
087: }
088: };
089: }
090:
091: /*(non-Javadoc) @see org.apache.lucene.search.function.FieldCacheSource#cachedFieldSourceEquals(org.apache.lucene.search.function.FieldCacheSource) */
092: public boolean cachedFieldSourceEquals(FieldCacheSource o) {
093: if (o.getClass() != ShortFieldSource.class) {
094: return false;
095: }
096: ShortFieldSource other = (ShortFieldSource) o;
097: return this .parser == null ? other.parser == null : this .parser
098: .getClass() == other.parser.getClass();
099: }
100:
101: /*(non-Javadoc) @see org.apache.lucene.search.function.FieldCacheSource#cachedFieldSourceHashCode() */
102: public int cachedFieldSourceHashCode() {
103: return parser == null ? Short.class.hashCode() : parser
104: .getClass().hashCode();
105: }
106:
107: }
|