Apache Lucene open-source search software
LUCENE-1257:LUCENE-1257: More generified APIs and implementations.
Uwe Schindler0428efa34368
diff --git a/src/java/org/apache/lucene/search/FieldCacheImpl.java b/src/java/org/apache/lucene/search/FieldCacheImpl.javaindex affb7870446..c3205ee0db9 100644--- a/src/java/org/apache/lucene/search/FieldCacheImpl.java+++ b/src/java/org/apache/lucene/search/FieldCacheImpl.java@@ -21,13 +21,10 @@ import java.io.IOException;import java.io.PrintStream;import java.util.ArrayList;import java.util.HashMap;-import java.util.Iterator;import java.util.List;-import java.util.Locale;import java.util.Map;import java.util.WeakHashMap;-import org.apache.lucene.document.NumericField; // javadocimport org.apache.lucene.index.IndexReader;import org.apache.lucene.index.Term;import org.apache.lucene.index.TermDocs;@@ -45,12 +42,12 @@ import org.apache.lucene.util.FieldCacheSanityChecker;*/class FieldCacheImpl implements FieldCache {- private Map caches;+ private Map<Class<?>,Cache> caches;FieldCacheImpl() {init();}private synchronized void init() {- caches = new HashMap(7);+ caches = new HashMap<Class<?>,Cache>(7);caches.put(Byte.TYPE, new ByteCache(this));caches.put(Short.TYPE, new ShortCache(this));caches.put(Integer.TYPE, new IntCache(this));@@ -66,23 +63,18 @@ class FieldCacheImpl implements FieldCache {}public CacheEntry[] getCacheEntries() {- List result = new ArrayList(17);- Iterator outerKeys = caches.keySet().iterator();- while (outerKeys.hasNext()) {- Class cacheType = (Class)outerKeys.next();- Cache cache = (Cache)caches.get(cacheType);- Iterator innerKeys = cache.readerCache.keySet().iterator();- while (innerKeys.hasNext()) {+ List<CacheEntry> result = new ArrayList<CacheEntry>(17);+ for(final Class<?> cacheType: caches.keySet()) {+ Cache cache = caches.get(cacheType);+ for (final Object readerKey : cache.readerCache.keySet()) {// we've now materialized a hard ref- Object readerKey = innerKeys.next();+// innerKeys was backed by WeakHashMap, sanity check// that it wasn't GCed before we made hard refif (null != readerKey && cache.readerCache.containsKey(readerKey)) {- Map innerCache = ((Map)cache.readerCache.get(readerKey));- Iterator entrySetIterator = innerCache.entrySet().iterator();- while (entrySetIterator.hasNext()) {- Map.Entry mapEntry = (Map.Entry) entrySetIterator.next();- Entry entry = (Entry) mapEntry.getKey();+ Map<Entry, Object> innerCache = cache.readerCache.get(readerKey);+ for (final Map.Entry<Entry, Object> mapEntry : innerCache.entrySet()) {+ Entry entry = mapEntry.getKey();result.add(new CacheEntryImpl(readerKey, entry.field,cacheType, entry.custom,mapEntry.getValue()));@@ -90,17 +82,17 @@ class FieldCacheImpl implements FieldCache {}}}- return (CacheEntry[]) result.toArray(new CacheEntry[result.size()]);+ return result.toArray(new CacheEntry[result.size()]);}private static final class CacheEntryImpl extends CacheEntry {private final Object readerKey;private final String fieldName;- private final Class cacheType;+ private final Class<?> cacheType;private final Object custom;private final Object value;CacheEntryImpl(Object readerKey, String fieldName,- Class cacheType,+ Class<?> cacheType,Object custom,Object value) {this.readerKey = readerKey;@@ -117,7 +109,7 @@ class FieldCacheImpl implements FieldCache {}public Object getReaderKey() { return readerKey; }public String getFieldName() { return fieldName; }- public Class getCacheType() { return cacheType; }+ public Class<?> getCacheType() { return cacheType; }public Object getCustom() { return custom; }public Object getValue() { return value; }}@@ -142,19 +134,19 @@ class FieldCacheImpl implements FieldCache {final FieldCache wrapper;- final Map readerCache = new WeakHashMap();+ final Map<Object,Map<Entry,Object>> readerCache = new WeakHashMap<Object,Map<Entry,Object>>();protected abstract Object createValue(IndexReader reader, Entry key)throws IOException;public Object get(IndexReader reader, Entry key) throws IOException {- Map innerCache;+ Map<Entry,Object> innerCache;Object value;final Object readerKey = reader.getFieldCacheKey();synchronized (readerCache) {- innerCache = (Map) readerCache.get(readerKey);+ innerCache = readerCache.get(readerKey);if (innerCache == null) {- innerCache = new HashMap();+ innerCache = new HashMap<Entry,Object>();readerCache.put(readerKey, innerCache);value = null;} else {@@ -248,7 +240,7 @@ class FieldCacheImpl implements FieldCache {// inherit javadocspublic byte[] getBytes(IndexReader reader, String field, ByteParser parser)throws IOException {- return (byte[]) ((Cache)caches.get(Byte.TYPE)).get(reader, new Entry(field, parser));+ return (byte[]) caches.get(Byte.TYPE).get(reader, new Entry(field, parser));}static final class ByteCache extends Cache {@@ -257,7 +249,7 @@ class FieldCacheImpl implements FieldCache {}protected Object createValue(IndexReader reader, Entry entryKey)throws IOException {- Entry entry = (Entry) entryKey;+ Entry entry = entryKey;String field = entry.field;ByteParser parser = (ByteParser) entry.custom;if (parser == null) {@@ -293,7 +285,7 @@ class FieldCacheImpl implements FieldCache {// inherit javadocspublic short[] getShorts(IndexReader reader, String field, ShortParser parser)throws IOException {- return (short[]) ((Cache)caches.get(Short.TYPE)).get(reader, new Entry(field, parser));+ return (short[]) caches.get(Short.TYPE).get(reader, new Entry(field, parser));}static final class ShortCache extends Cache {@@ -303,7 +295,7 @@ class FieldCacheImpl implements FieldCache {protected Object createValue(IndexReader reader, Entry entryKey)throws IOException {- Entry entry = (Entry) entryKey;+ Entry entry = entryKey;String field = entry.field;ShortParser parser = (ShortParser) entry.custom;if (parser == null) {@@ -339,7 +331,7 @@ class FieldCacheImpl implements FieldCache {// inherit javadocspublic int[] getInts(IndexReader reader, String field, IntParser parser)throws IOException {- return (int[]) ((Cache)caches.get(Integer.TYPE)).get(reader, new Entry(field, parser));+ return (int[]) caches.get(Integer.TYPE).get(reader, new Entry(field, parser));}static final class IntCache extends Cache {@@ -349,7 +341,7 @@ class FieldCacheImpl implements FieldCache {protected Object createValue(IndexReader reader, Entry entryKey)throws IOException {- Entry entry = (Entry) entryKey;+ Entry entry = entryKey;String field = entry.field;IntParser parser = (IntParser) entry.custom;if (parser == null) {@@ -396,7 +388,7 @@ class FieldCacheImpl implements FieldCache {public float[] getFloats(IndexReader reader, String field, FloatParser parser)throws IOException {- return (float[]) ((Cache)caches.get(Float.TYPE)).get(reader, new Entry(field, parser));+ return (float[]) caches.get(Float.TYPE).get(reader, new Entry(field, parser));}static final class FloatCache extends Cache {@@ -406,7 +398,7 @@ class FieldCacheImpl implements FieldCache {protected Object createValue(IndexReader reader, Entry entryKey)throws IOException {- Entry entry = (Entry) entryKey;+ Entry entry = entryKey;String field = entry.field;FloatParser parser = (FloatParser) entry.custom;if (parser == null) {@@ -450,7 +442,7 @@ class FieldCacheImpl implements FieldCache {// inherit javadocspublic long[] getLongs(IndexReader reader, String field, FieldCache.LongParser parser)throws IOException {- return (long[]) ((Cache)caches.get(Long.TYPE)).get(reader, new Entry(field, parser));+ return (long[]) caches.get(Long.TYPE).get(reader, new Entry(field, parser));}static final class LongCache extends Cache {@@ -505,7 +497,7 @@ class FieldCacheImpl implements FieldCache {// inherit javadocspublic double[] getDoubles(IndexReader reader, String field, FieldCache.DoubleParser parser)throws IOException {- return (double[]) ((Cache)caches.get(Double.TYPE)).get(reader, new Entry(field, parser));+ return (double[]) caches.get(Double.TYPE).get(reader, new Entry(field, parser));}static final class DoubleCache extends Cache {@@ -515,7 +507,7 @@ class FieldCacheImpl implements FieldCache {protected Object createValue(IndexReader reader, Entry entryKey)throws IOException {- Entry entry = (Entry) entryKey;+ Entry entry = entryKey;String field = entry.field;FieldCache.DoubleParser parser = (FieldCache.DoubleParser) entry.custom;if (parser == null) {@@ -554,7 +546,7 @@ class FieldCacheImpl implements FieldCache {// inherit javadocspublic String[] getStrings(IndexReader reader, String field)throws IOException {- return (String[]) ((Cache)caches.get(String.class)).get(reader, new Entry(field, (Parser)null));+ return (String[]) caches.get(String.class).get(reader, new Entry(field, (Parser)null));}static final class StringCache extends Cache {@@ -589,7 +581,7 @@ class FieldCacheImpl implements FieldCache {// inherit javadocspublic StringIndex getStringIndex(IndexReader reader, String field)throws IOException {- return (StringIndex) ((Cache)caches.get(StringIndex.class)).get(reader, new Entry(field, (Parser)null));+ return (StringIndex) caches.get(StringIndex.class).get(reader, new Entry(field, (Parser)null));}static final class StringIndexCache extends Cache {diff --git a/src/java/org/apache/lucene/search/FieldValueHitQueue.java b/src/java/org/apache/lucene/search/FieldValueHitQueue.javaindex 446c5c34f5b..59465673b52 100644--- a/src/java/org/apache/lucene/search/FieldValueHitQueue.java+++ b/src/java/org/apache/lucene/search/FieldValueHitQueue.java@@ -34,7 +34,7 @@ import org.apache.lucene.util.PriorityQueue;* @see Searcher#search(Query,Filter,int,Sort)* @see FieldCache*/-public abstract class FieldValueHitQueue extends PriorityQueue {+public abstract class FieldValueHitQueue extends PriorityQueue<FieldValueHitQueue.Entry> {final static class Entry {int slot;@@ -84,9 +84,7 @@ public abstract class FieldValueHitQueue extends PriorityQueue {* @param b ScoreDoc* @return <code>true</code> if document <code>a</code> should be sorted after document <code>b</code>.*/- protected boolean lessThan(final Object a, final Object b) {- final Entry hitA = (Entry) a;- final Entry hitB = (Entry) b;+ protected boolean lessThan(final Entry hitA, final Entry hitB) {assert hitA != hitB;assert hitA.slot != hitB.slot;@@ -123,9 +121,7 @@ public abstract class FieldValueHitQueue extends PriorityQueue {initialize(size);}- protected boolean lessThan(final Object a, final Object b) {- final Entry hitA = (Entry) a;- final Entry hitB = (Entry) b;+ protected boolean lessThan(final Entry hitA, final Entry hitB) {assert hitA != hitB;assert hitA.slot != hitB.slot;@@ -194,7 +190,7 @@ public abstract class FieldValueHitQueue extends PriorityQueue {protected final FieldComparator[] comparators;protected final int[] reverseMul;- protected abstract boolean lessThan (final Object a, final Object b);+ protected abstract boolean lessThan (final Entry a, final Entry b);/*** Given a queue Entry, creates a corresponding FieldDocdiff --git a/src/java/org/apache/lucene/search/MultiTermQueryWrapperFilter.java b/src/java/org/apache/lucene/search/MultiTermQueryWrapperFilter.javaindex 5245b493445..6183c76b04f 100644--- a/src/java/org/apache/lucene/search/MultiTermQueryWrapperFilter.java+++ b/src/java/org/apache/lucene/search/MultiTermQueryWrapperFilter.java@@ -39,14 +39,14 @@ import org.apache.lucene.util.OpenBitSet;* {@link MultiTermQuery#CONSTANT_SCORE_FILTER_REWRITE};* this is why it is not abstract.*/-public class MultiTermQueryWrapperFilter extends Filter {+public class MultiTermQueryWrapperFilter<Q extends MultiTermQuery> extends Filter {- protected final MultiTermQuery query;+ protected final Q query;/*** Wrap a {@link MultiTermQuery} as a Filter.*/- protected MultiTermQueryWrapperFilter(MultiTermQuery query) {+ protected MultiTermQueryWrapperFilter(Q query) {this.query = query;}diff --git a/src/java/org/apache/lucene/search/NumericRangeFilter.java b/src/java/org/apache/lucene/search/NumericRangeFilter.javaindex 025c3418b32..da42bfc0aa3 100644--- a/src/java/org/apache/lucene/search/NumericRangeFilter.java+++ b/src/java/org/apache/lucene/search/NumericRangeFilter.java@@ -45,7 +45,7 @@ import org.apache.lucene.util.NumericUtils; // for javadocs** @since 2.9**/-public final class NumericRangeFilter<T extends Number> extends MultiTermQueryWrapperFilter {+public final class NumericRangeFilter<T extends Number> extends MultiTermQueryWrapperFilter<NumericRangeQuery<T>> {private NumericRangeFilter(final NumericRangeQuery<T> query) {super(query);@@ -172,23 +172,18 @@ public final class NumericRangeFilter<T extends Number> extends MultiTermQueryWr}/** Returns the field name for this filter */- @SuppressWarnings("unchecked")- public String getField() { return ((NumericRangeQuery<T>)query).getField(); }+ public String getField() { return query.getField(); }/** Returns <code>true</code> if the lower endpoint is inclusive */- @SuppressWarnings("unchecked")- public boolean includesMin() { return ((NumericRangeQuery<T>)query).includesMin(); }+ public boolean includesMin() { return query.includesMin(); }/** Returns <code>true</code> if the upper endpoint is inclusive */- @SuppressWarnings("unchecked")- public boolean includesMax() { return ((NumericRangeQuery<T>)query).includesMax(); }+ public boolean includesMax() { return query.includesMax(); }/** Returns the lower value of this range filter */- @SuppressWarnings("unchecked")- public T getMin() { return ((NumericRangeQuery<T>)query).getMin(); }+ public T getMin() { return query.getMin(); }/** Returns the upper value of this range filter */- @SuppressWarnings("unchecked")- public T getMax() { return ((NumericRangeQuery<T>)query).getMax(); }+ public T getMax() { return query.getMax(); }}diff --git a/src/java/org/apache/lucene/search/PrefixFilter.java b/src/java/org/apache/lucene/search/PrefixFilter.javaindex a754240ed3f..bb4b455d92d 100755--- a/src/java/org/apache/lucene/search/PrefixFilter.java+++ b/src/java/org/apache/lucene/search/PrefixFilter.java@@ -23,13 +23,13 @@ import org.apache.lucene.index.Term;* A Filter that restricts search results to values that have a matching prefix in a given* field.*/-public class PrefixFilter extends MultiTermQueryWrapperFilter {+public class PrefixFilter extends MultiTermQueryWrapperFilter<PrefixQuery> {public PrefixFilter(Term prefix) {super(new PrefixQuery(prefix));}- public Term getPrefix() { return ((PrefixQuery)query).getPrefix(); }+ public Term getPrefix() { return query.getPrefix(); }/** Prints a user-readable version of this query. */public String toString () {diff --git a/src/java/org/apache/lucene/search/TermRangeFilter.java b/src/java/org/apache/lucene/search/TermRangeFilter.javaindex 4a86fb64aa4..923fb8be049 100644--- a/src/java/org/apache/lucene/search/TermRangeFilter.java+++ b/src/java/org/apache/lucene/search/TermRangeFilter.java@@ -32,7 +32,7 @@ import java.text.Collator;* same field, {@link FieldCacheRangeFilter} may have significantly better performance.* @since 2.9*/-public class TermRangeFilter extends MultiTermQueryWrapperFilter {+public class TermRangeFilter extends MultiTermQueryWrapperFilter<TermRangeQuery> {/*** @param fieldName The field this range applies to@@ -89,20 +89,20 @@ public class TermRangeFilter extends MultiTermQueryWrapperFilter {}/** Returns the field name for this filter */- public String getField() { return ((TermRangeQuery) query).getField(); }+ public String getField() { return query.getField(); }/** Returns the lower value of this range filter */- public String getLowerTerm() { return ((TermRangeQuery) query).getLowerTerm(); }+ public String getLowerTerm() { return query.getLowerTerm(); }/** Returns the upper value of this range filter */- public String getUpperTerm() { return ((TermRangeQuery) query).getUpperTerm(); }+ public String getUpperTerm() { return query.getUpperTerm(); }/** Returns <code>true</code> if the lower endpoint is inclusive */- public boolean includesLower() { return ((TermRangeQuery) query).includesLower(); }+ public boolean includesLower() { return query.includesLower(); }/** Returns <code>true</code> if the upper endpoint is inclusive */- public boolean includesUpper() { return ((TermRangeQuery) query).includesUpper(); }+ public boolean includesUpper() { return query.includesUpper(); }/** Returns the collator used to determine range inclusion, if any. */- public Collator getCollator() { return ((TermRangeQuery) query).getCollator(); }+ public Collator getCollator() { return query.getCollator(); }}diff --git a/src/java/org/apache/lucene/search/TopDocsCollector.java b/src/java/org/apache/lucene/search/TopDocsCollector.javaindex dec3374df83..63f792e450d 100644--- a/src/java/org/apache/lucene/search/TopDocsCollector.java+++ b/src/java/org/apache/lucene/search/TopDocsCollector.java@@ -17,6 +17,7 @@ package org.apache.lucene.search;* limitations under the License.*/+import org.apache.lucene.util.PriorityQueue;/**@@ -27,7 +28,7 @@ import org.apache.lucene.util.PriorityQueue;* Extending classes can override {@link #topDocs(int, int)} and* {@link #getTotalHits()} in order to provide their own implementation.*/-public abstract class TopDocsCollector extends Collector {+public abstract class TopDocsCollector<T> extends Collector {// This is used in case topDocs() is called with illegal parameters, or there// simply aren't (enough) results.@@ -39,12 +40,12 @@ public abstract class TopDocsCollector extends Collector {* HitQueue for example aggregates the top scoring documents, while other PQ* implementations may hold documents sorted by other criteria.*/- protected PriorityQueue pq;+ protected PriorityQueue<T> pq;/** The total number of documents that the collector encountered. */protected int totalHits;- protected TopDocsCollector(PriorityQueue pq) {+ protected TopDocsCollector(PriorityQueue<T> pq) {this.pq = pq;}diff --git a/src/java/org/apache/lucene/search/TopFieldCollector.java b/src/java/org/apache/lucene/search/TopFieldCollector.javaindex eb1839fcbdd..2c1b6d73957 100644--- a/src/java/org/apache/lucene/search/TopFieldCollector.java+++ b/src/java/org/apache/lucene/search/TopFieldCollector.java@@ -33,7 +33,7 @@ import org.apache.lucene.util.PriorityQueue;* <p><b>NOTE:</b> This API is experimental and might change in* incompatible ways in the next release.</p>*/-public abstract class TopFieldCollector extends TopDocsCollector {+public abstract class TopFieldCollector extends TopDocsCollector<Entry> {// TODO: one optimization we could do is to pre-fill// the queue with sentinel value that guaranteed to@@ -60,7 +60,7 @@ public abstract class TopFieldCollector extends TopDocsCollector {final void updateBottom(int doc) {// bottom.score is already set to Float.NaN in add().bottom.docID = docBase + doc;- bottom = (Entry) pq.updateTop();+ bottom = pq.updateTop();}public void collect(int doc) throws IOException {@@ -161,7 +161,7 @@ public abstract class TopFieldCollector extends TopDocsCollector {final void updateBottom(int doc, float score) {bottom.docID = docBase + doc;bottom.score = score;- bottom = (Entry) pq.updateTop();+ bottom = pq.updateTop();}public void collect(int doc) throws IOException {@@ -273,7 +273,7 @@ public abstract class TopFieldCollector extends TopDocsCollector {final void updateBottom(int doc, float score) {bottom.docID = docBase + doc;bottom.score = score;- bottom = (Entry) pq.updateTop();+ bottom = pq.updateTop();}public void collect(int doc) throws IOException {@@ -380,7 +380,7 @@ public abstract class TopFieldCollector extends TopDocsCollector {final void updateBottom(int doc) {// bottom.score is already set to Float.NaN in add().bottom.docID = docBase + doc;- bottom = (Entry) pq.updateTop();+ bottom = pq.updateTop();}public void collect(int doc) throws IOException {@@ -529,7 +529,7 @@ public abstract class TopFieldCollector extends TopDocsCollector {final void updateBottom(int doc, float score) {bottom.docID = docBase + doc;bottom.score = score;- bottom = (Entry) pq.updateTop();+ bottom = pq.updateTop();}public void collect(int doc) throws IOException {@@ -675,7 +675,7 @@ public abstract class TopFieldCollector extends TopDocsCollector {final void updateBottom(int doc, float score) {bottom.docID = docBase + doc;bottom.score = score;- bottom = (Entry) pq.updateTop();+ bottom = pq.updateTop();}public void collect(int doc) throws IOException {@@ -833,7 +833,7 @@ public abstract class TopFieldCollector extends TopDocsCollector {// internal versions. If someone will define a constructor with any other// visibility, then anyone will be able to extend the class, which is not what// we want.- private TopFieldCollector(PriorityQueue pq, int numHits, boolean fillFields) {+ private TopFieldCollector(PriorityQueue<Entry> pq, int numHits, boolean fillFields) {super(pq);this.numHits = numHits;this.fillFields = fillFields;@@ -925,7 +925,7 @@ public abstract class TopFieldCollector extends TopDocsCollector {}final void add(int slot, int doc, float score) {- bottom = (Entry) pq.add(new Entry(slot, docBase + doc, score));+ bottom = pq.add(new Entry(slot, docBase + doc, score));queueFull = totalHits == numHits;}@@ -943,7 +943,7 @@ public abstract class TopFieldCollector extends TopDocsCollector {}} else {for (int i = howMany - 1; i >= 0; i--) {- Entry entry = (Entry) pq.pop();+ Entry entry = pq.pop();results[i] = new FieldDoc(entry.docID, entry.score);}}diff --git a/src/java/org/apache/lucene/search/TopScoreDocCollector.java b/src/java/org/apache/lucene/search/TopScoreDocCollector.javaindex c9cd4e639a3..f65f00d5e15 100644--- a/src/java/org/apache/lucene/search/TopScoreDocCollector.java+++ b/src/java/org/apache/lucene/search/TopScoreDocCollector.java@@ -34,7 +34,7 @@ import org.apache.lucene.index.IndexReader;* not valid scores. This collector will not properly* collect hits with such scores.*/-public abstract class TopScoreDocCollector extends TopDocsCollector {+public abstract class TopScoreDocCollector extends TopDocsCollector<ScoreDoc> {// Assumes docs are scored in order.private static class InOrderTopScoreDocCollector extends TopScoreDocCollector {@@ -53,7 +53,7 @@ public abstract class TopScoreDocCollector extends TopDocsCollector {}pqTop.doc = doc + docBase;pqTop.score = score;- pqTop = (ScoreDoc) pq.updateTop();+ pqTop = pq.updateTop();}public boolean acceptsDocsOutOfOrder() {@@ -76,7 +76,7 @@ public abstract class TopScoreDocCollector extends TopDocsCollector {}pqTop.doc = doc;pqTop.score = score;- pqTop = (ScoreDoc) pq.updateTop();+ pqTop = pq.updateTop();}public boolean acceptsDocsOutOfOrder() {@@ -113,7 +113,7 @@ public abstract class TopScoreDocCollector extends TopDocsCollector {super(new HitQueue(numHits, true));// HitQueue implements getSentinelObject to return a ScoreDoc, so we know// that at this point top() is already initialized.- pqTop = (ScoreDoc) pq.top();+ pqTop = pq.top();}protected TopDocs newTopDocs(ScoreDoc[] results, int start) {@@ -130,7 +130,7 @@ public abstract class TopScoreDocCollector extends TopDocsCollector {maxScore = results[0].score;} else {for (int i = pq.size(); i > 1; i--) { pq.pop(); }- maxScore = ((ScoreDoc) pq.pop()).score;+ maxScore = pq.pop().score;}return new TopDocs(totalHits, results, maxScore);