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 int field values from the
028: * {@link org.apache.lucene.search.FieldCache FieldCache}
029: * using <code>getInts()</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: *
041: */
042: public class IntFieldSource extends FieldCacheSource {
043: private FieldCache.IntParser parser;
044:
045: /**
046: * Create a cached int field source with default string-to-int parser.
047: */
048: public IntFieldSource(String field) {
049: this (field, null);
050: }
051:
052: /**
053: * Create a cached int field source with a specific string-to-int parser.
054: */
055: public IntFieldSource(String field, FieldCache.IntParser 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 "int(" + 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 int[] arr = (parser == null) ? cache.getInts(reader,
069: field) : cache.getInts(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 (float) arr[doc];
074: }
075:
076: /*(non-Javadoc) @see org.apache.lucene.search.function.DocValues#intVal(int) */
077: public int intVal(int doc) {
078: return arr[doc];
079: }
080:
081: /*(non-Javadoc) @see org.apache.lucene.search.function.DocValues#toString(int) */
082: public String toString(int doc) {
083: return description() + '=' + intVal(doc);
084: }
085:
086: /*(non-Javadoc) @see org.apache.lucene.search.function.DocValues#getInnerArray() */
087: Object getInnerArray() {
088: return arr;
089: }
090: };
091: }
092:
093: /*(non-Javadoc) @see org.apache.lucene.search.function.FieldCacheSource#cachedFieldSourceEquals(org.apache.lucene.search.function.FieldCacheSource) */
094: public boolean cachedFieldSourceEquals(FieldCacheSource o) {
095: if (o.getClass() != IntFieldSource.class) {
096: return false;
097: }
098: IntFieldSource other = (IntFieldSource) o;
099: return this .parser == null ? other.parser == null : this .parser
100: .getClass() == other.parser.getClass();
101: }
102:
103: /*(non-Javadoc) @see org.apache.lucene.search.function.FieldCacheSource#cachedFieldSourceHashCode() */
104: public int cachedFieldSourceHashCode() {
105: return parser == null ? Integer.class.hashCode() : parser
106: .getClass().hashCode();
107: }
108:
109: }
|