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 java.io.IOException;
021:
022: import org.apache.lucene.index.IndexReader;
023: import org.apache.lucene.search.FieldCache;
024:
025: /**
026: * Expert: A base class for ValueSource implementations that retrieve values for
027: * a single field from the {@link org.apache.lucene.search.FieldCache FieldCache}.
028: * <p>
029: * Fields used herein nust be indexed (doesn't matter if these fields are stored or not).
030: * <p>
031: * It is assumed that each such indexed field is untokenized, or at least has a single token in a document.
032: * For documents with multiple tokens of the same field, behavior is undefined (It is likely that current
033: * code would use the value of one of these tokens, but this is not guaranteed).
034: * <p>
035: * Document with no tokens in this field are assigned the <code>Zero</code> value.
036: *
037: * <p><font color="#FF0000">
038: * WARNING: The status of the <b>search.function</b> package is experimental.
039: * The APIs introduced here might change in the future and will not be
040: * supported anymore in such a case.</font>
041: *
042: * @author yonik
043: */
044: public abstract class FieldCacheSource extends ValueSource {
045: private String field;
046:
047: /**
048: * Create a cached field source for the input field.
049: */
050: public FieldCacheSource(String field) {
051: this .field = field;
052: }
053:
054: /* (non-Javadoc) @see org.apache.lucene.search.function.ValueSource#getValues(org.apache.lucene.index.IndexReader) */
055: public final DocValues getValues(IndexReader reader)
056: throws IOException {
057: return getCachedFieldValues(FieldCache.DEFAULT, field, reader);
058: }
059:
060: /* (non-Javadoc) @see org.apache.lucene.search.function.ValueSource#description() */
061: public String description() {
062: return field;
063: }
064:
065: /**
066: * Return cached DocValues for input field and reader.
067: * @param cache FieldCache so that values of a field are loaded once per reader (RAM allowing)
068: * @param field Field for which values are required.
069: * @see ValueSource
070: */
071: public abstract DocValues getCachedFieldValues(FieldCache cache,
072: String field, IndexReader reader) throws IOException;
073:
074: /*(non-Javadoc) @see java.lang.Object#equals(java.lang.Object) */
075: public final boolean equals(Object o) {
076: if (!(o instanceof FieldCacheSource)) {
077: return false;
078: }
079: FieldCacheSource other = (FieldCacheSource) o;
080: return this .field.equals(other.field)
081: && cachedFieldSourceEquals(other);
082: }
083:
084: /*(non-Javadoc) @see java.lang.Object#hashCode() */
085: public final int hashCode() {
086: return field.hashCode() + cachedFieldSourceHashCode();
087: }
088:
089: /**
090: * Check if equals to another {@link FieldCacheSource}, already knowing that cache and field are equal.
091: * @see Object#equals(java.lang.Object)
092: */
093: public abstract boolean cachedFieldSourceEquals(
094: FieldCacheSource other);
095:
096: /**
097: * Return a hash code of a {@link FieldCacheSource}, without the hash-codes of the field
098: * and the cache (those are taken care of elsewhere).
099: * @see Object#hashCode()
100: */
101: public abstract int cachedFieldSourceHashCode();
102: }
|