Skip to content

Apache Lucene open-source search software

LUCENE-1257: FieldCacheRangeFilter generified

Uwe Schindler9b1746ab820b
diff --git a/src/java/org/apache/lucene/search/FieldCacheRangeFilter.java b/src/java/org/apache/lucene/search/FieldCacheRangeFilter.java
index fc0bee8eee0..8f782408641 100644
--- a/src/java/org/apache/lucene/search/FieldCacheRangeFilter.java
+++ b/src/java/org/apache/lucene/search/FieldCacheRangeFilter.java
@@ -52,15 +52,15 @@ import org.apache.lucene.document.NumericField; // for javadocs
* that create a correct instance for different data types supported by {@link FieldCache}.
*/
-public abstract class FieldCacheRangeFilter extends Filter {
+public abstract class FieldCacheRangeFilter<T> extends Filter {
final String field;
final FieldCache.Parser parser;
- final Object lowerVal;
- final Object upperVal;
+ final T lowerVal;
+ final T upperVal;
final boolean includeLower;
final boolean includeUpper;
- private FieldCacheRangeFilter(String field, FieldCache.Parser parser, Object lowerVal, Object upperVal, boolean includeLower, boolean includeUpper) {
+ private FieldCacheRangeFilter(String field, FieldCache.Parser parser, T lowerVal, T upperVal, boolean includeLower, boolean includeUpper) {
this.field = field;
this.parser = parser;
this.lowerVal = lowerVal;
@@ -77,12 +77,12 @@ public abstract class FieldCacheRangeFilter extends Filter {
* fields containing zero or one term in the field. The range can be half-open by setting one
* of the values to <code>null</code>.
*/
- public static FieldCacheRangeFilter newStringRange(String field, String lowerVal, String upperVal, boolean includeLower, boolean includeUpper) {
- return new FieldCacheRangeFilter(field, null, lowerVal, upperVal, includeLower, includeUpper) {
+ public static FieldCacheRangeFilter<String> newStringRange(String field, String lowerVal, String upperVal, boolean includeLower, boolean includeUpper) {
+ return new FieldCacheRangeFilter<String>(field, null, lowerVal, upperVal, includeLower, includeUpper) {
public DocIdSet getDocIdSet(IndexReader reader) throws IOException {
final FieldCache.StringIndex fcsi = FieldCache.DEFAULT.getStringIndex(reader, field);
- final int lowerPoint = fcsi.binarySearchLookup((String) lowerVal);
- final int upperPoint = fcsi.binarySearchLookup((String) upperVal);
+ final int lowerPoint = fcsi.binarySearchLookup(lowerVal);
+ final int upperPoint = fcsi.binarySearchLookup(upperVal);
final int inclusiveLowerPoint, inclusiveUpperPoint;
@@ -133,7 +133,7 @@ public abstract class FieldCacheRangeFilter extends Filter {
* byte fields containing exactly one numeric term in the field. The range can be half-open by setting one
* of the values to <code>null</code>.
*/
- public static FieldCacheRangeFilter newByteRange(String field, Byte lowerVal, Byte upperVal, boolean includeLower, boolean includeUpper) {
+ public static FieldCacheRangeFilter<Byte> newByteRange(String field, Byte lowerVal, Byte upperVal, boolean includeLower, boolean includeUpper) {
return newByteRange(field, null, lowerVal, upperVal, includeLower, includeUpper);
}
@@ -142,12 +142,12 @@ public abstract class FieldCacheRangeFilter extends Filter {
* byte fields containing exactly one numeric term in the field. The range can be half-open by setting one
* of the values to <code>null</code>.
*/
- public static FieldCacheRangeFilter newByteRange(String field, FieldCache.ByteParser parser, Byte lowerVal, Byte upperVal, boolean includeLower, boolean includeUpper) {
- return new FieldCacheRangeFilter(field, parser, lowerVal, upperVal, includeLower, includeUpper) {
+ public static FieldCacheRangeFilter<Byte> newByteRange(String field, FieldCache.ByteParser parser, Byte lowerVal, Byte upperVal, boolean includeLower, boolean includeUpper) {
+ return new FieldCacheRangeFilter<Byte>(field, parser, lowerVal, upperVal, includeLower, includeUpper) {
public DocIdSet getDocIdSet(IndexReader reader) throws IOException {
final byte inclusiveLowerPoint, inclusiveUpperPoint;
if (lowerVal != null) {
- final byte i = ((Number) lowerVal).byteValue();
+ final byte i = lowerVal.byteValue();
if (!includeLower && i == Byte.MAX_VALUE)
return DocIdSet.EMPTY_DOCIDSET;
inclusiveLowerPoint = (byte) (includeLower ? i : (i + 1));
@@ -155,7 +155,7 @@ public abstract class FieldCacheRangeFilter extends Filter {
inclusiveLowerPoint = Byte.MIN_VALUE;
}
if (upperVal != null) {
- final byte i = ((Number) upperVal).byteValue();
+ final byte i = upperVal.byteValue();
if (!includeUpper && i == Byte.MIN_VALUE)
return DocIdSet.EMPTY_DOCIDSET;
inclusiveUpperPoint = (byte) (includeUpper ? i : (i - 1));
@@ -182,7 +182,7 @@ public abstract class FieldCacheRangeFilter extends Filter {
* short fields containing exactly one numeric term in the field. The range can be half-open by setting one
* of the values to <code>null</code>.
*/
- public static FieldCacheRangeFilter newShortRange(String field, Short lowerVal, Short upperVal, boolean includeLower, boolean includeUpper) {
+ public static FieldCacheRangeFilter<Short> newShortRange(String field, Short lowerVal, Short upperVal, boolean includeLower, boolean includeUpper) {
return newShortRange(field, null, lowerVal, upperVal, includeLower, includeUpper);
}
@@ -191,12 +191,12 @@ public abstract class FieldCacheRangeFilter extends Filter {
* short fields containing exactly one numeric term in the field. The range can be half-open by setting one
* of the values to <code>null</code>.
*/
- public static FieldCacheRangeFilter newShortRange(String field, FieldCache.ShortParser parser, Short lowerVal, Short upperVal, boolean includeLower, boolean includeUpper) {
- return new FieldCacheRangeFilter(field, parser, lowerVal, upperVal, includeLower, includeUpper) {
+ public static FieldCacheRangeFilter<Short> newShortRange(String field, FieldCache.ShortParser parser, Short lowerVal, Short upperVal, boolean includeLower, boolean includeUpper) {
+ return new FieldCacheRangeFilter<Short>(field, parser, lowerVal, upperVal, includeLower, includeUpper) {
public DocIdSet getDocIdSet(IndexReader reader) throws IOException {
final short inclusiveLowerPoint, inclusiveUpperPoint;
if (lowerVal != null) {
- short i = ((Number) lowerVal).shortValue();
+ short i = lowerVal.shortValue();
if (!includeLower && i == Short.MAX_VALUE)
return DocIdSet.EMPTY_DOCIDSET;
inclusiveLowerPoint = (short) (includeLower ? i : (i + 1));
@@ -204,7 +204,7 @@ public abstract class FieldCacheRangeFilter extends Filter {
inclusiveLowerPoint = Short.MIN_VALUE;
}
if (upperVal != null) {
- short i = ((Number) upperVal).shortValue();
+ short i = upperVal.shortValue();
if (!includeUpper && i == Short.MIN_VALUE)
return DocIdSet.EMPTY_DOCIDSET;
inclusiveUpperPoint = (short) (includeUpper ? i : (i - 1));
@@ -231,7 +231,7 @@ public abstract class FieldCacheRangeFilter extends Filter {
* int fields containing exactly one numeric term in the field. The range can be half-open by setting one
* of the values to <code>null</code>.
*/
- public static FieldCacheRangeFilter newIntRange(String field, Integer lowerVal, Integer upperVal, boolean includeLower, boolean includeUpper) {
+ public static FieldCacheRangeFilter<Integer> newIntRange(String field, Integer lowerVal, Integer upperVal, boolean includeLower, boolean includeUpper) {
return newIntRange(field, null, lowerVal, upperVal, includeLower, includeUpper);
}
@@ -240,12 +240,12 @@ public abstract class FieldCacheRangeFilter extends Filter {
* int fields containing exactly one numeric term in the field. The range can be half-open by setting one
* of the values to <code>null</code>.
*/
- public static FieldCacheRangeFilter newIntRange(String field, FieldCache.IntParser parser, Integer lowerVal, Integer upperVal, boolean includeLower, boolean includeUpper) {
- return new FieldCacheRangeFilter(field, parser, lowerVal, upperVal, includeLower, includeUpper) {
+ public static FieldCacheRangeFilter<Integer> newIntRange(String field, FieldCache.IntParser parser, Integer lowerVal, Integer upperVal, boolean includeLower, boolean includeUpper) {
+ return new FieldCacheRangeFilter<Integer>(field, parser, lowerVal, upperVal, includeLower, includeUpper) {
public DocIdSet getDocIdSet(IndexReader reader) throws IOException {
final int inclusiveLowerPoint, inclusiveUpperPoint;
if (lowerVal != null) {
- int i = ((Number) lowerVal).intValue();
+ int i = lowerVal.intValue();
if (!includeLower && i == Integer.MAX_VALUE)
return DocIdSet.EMPTY_DOCIDSET;
inclusiveLowerPoint = includeLower ? i : (i + 1);
@@ -253,7 +253,7 @@ public abstract class FieldCacheRangeFilter extends Filter {
inclusiveLowerPoint = Integer.MIN_VALUE;
}
if (upperVal != null) {
- int i = ((Number) upperVal).intValue();
+ int i = upperVal.intValue();
if (!includeUpper && i == Integer.MIN_VALUE)
return DocIdSet.EMPTY_DOCIDSET;
inclusiveUpperPoint = includeUpper ? i : (i - 1);
@@ -280,7 +280,7 @@ public abstract class FieldCacheRangeFilter extends Filter {
* long fields containing exactly one numeric term in the field. The range can be half-open by setting one
* of the values to <code>null</code>.
*/
- public static FieldCacheRangeFilter newLongRange(String field, Long lowerVal, Long upperVal, boolean includeLower, boolean includeUpper) {
+ public static FieldCacheRangeFilter<Long> newLongRange(String field, Long lowerVal, Long upperVal, boolean includeLower, boolean includeUpper) {
return newLongRange(field, null, lowerVal, upperVal, includeLower, includeUpper);
}
@@ -289,12 +289,12 @@ public abstract class FieldCacheRangeFilter extends Filter {
* long fields containing exactly one numeric term in the field. The range can be half-open by setting one
* of the values to <code>null</code>.
*/
- public static FieldCacheRangeFilter newLongRange(String field, FieldCache.LongParser parser, Long lowerVal, Long upperVal, boolean includeLower, boolean includeUpper) {
- return new FieldCacheRangeFilter(field, parser, lowerVal, upperVal, includeLower, includeUpper) {
+ public static FieldCacheRangeFilter<Long> newLongRange(String field, FieldCache.LongParser parser, Long lowerVal, Long upperVal, boolean includeLower, boolean includeUpper) {
+ return new FieldCacheRangeFilter<Long>(field, parser, lowerVal, upperVal, includeLower, includeUpper) {
public DocIdSet getDocIdSet(IndexReader reader) throws IOException {
final long inclusiveLowerPoint, inclusiveUpperPoint;
if (lowerVal != null) {
- long i = ((Number) lowerVal).longValue();
+ long i = lowerVal.longValue();
if (!includeLower && i == Long.MAX_VALUE)
return DocIdSet.EMPTY_DOCIDSET;
inclusiveLowerPoint = includeLower ? i : (i + 1L);
@@ -302,7 +302,7 @@ public abstract class FieldCacheRangeFilter extends Filter {
inclusiveLowerPoint = Long.MIN_VALUE;
}
if (upperVal != null) {
- long i = ((Number) upperVal).longValue();
+ long i = upperVal.longValue();
if (!includeUpper && i == Long.MIN_VALUE)
return DocIdSet.EMPTY_DOCIDSET;
inclusiveUpperPoint = includeUpper ? i : (i - 1L);
@@ -329,7 +329,7 @@ public abstract class FieldCacheRangeFilter extends Filter {
* float fields containing exactly one numeric term in the field. The range can be half-open by setting one
* of the values to <code>null</code>.
*/
- public static FieldCacheRangeFilter newFloatRange(String field, Float lowerVal, Float upperVal, boolean includeLower, boolean includeUpper) {
+ public static FieldCacheRangeFilter<Float> newFloatRange(String field, Float lowerVal, Float upperVal, boolean includeLower, boolean includeUpper) {
return newFloatRange(field, null, lowerVal, upperVal, includeLower, includeUpper);
}
@@ -338,14 +338,14 @@ public abstract class FieldCacheRangeFilter extends Filter {
* float fields containing exactly one numeric term in the field. The range can be half-open by setting one
* of the values to <code>null</code>.
*/
- public static FieldCacheRangeFilter newFloatRange(String field, FieldCache.FloatParser parser, Float lowerVal, Float upperVal, boolean includeLower, boolean includeUpper) {
- return new FieldCacheRangeFilter(field, parser, lowerVal, upperVal, includeLower, includeUpper) {
+ public static FieldCacheRangeFilter<Float> newFloatRange(String field, FieldCache.FloatParser parser, Float lowerVal, Float upperVal, boolean includeLower, boolean includeUpper) {
+ return new FieldCacheRangeFilter<Float>(field, parser, lowerVal, upperVal, includeLower, includeUpper) {
public DocIdSet getDocIdSet(IndexReader reader) throws IOException {
// we transform the floating point numbers to sortable integers
// using NumericUtils to easier find the next bigger/lower value
final float inclusiveLowerPoint, inclusiveUpperPoint;
if (lowerVal != null) {
- float f = ((Number) lowerVal).floatValue();
+ float f = lowerVal.floatValue();
if (!includeUpper && f > 0.0f && Float.isInfinite(f))
return DocIdSet.EMPTY_DOCIDSET;
int i = NumericUtils.floatToSortableInt(f);
@@ -354,7 +354,7 @@ public abstract class FieldCacheRangeFilter extends Filter {
inclusiveLowerPoint = Float.NEGATIVE_INFINITY;
}
if (upperVal != null) {
- float f = ((Number) upperVal).floatValue();
+ float f = upperVal.floatValue();
if (!includeUpper && f < 0.0f && Float.isInfinite(f))
return DocIdSet.EMPTY_DOCIDSET;
int i = NumericUtils.floatToSortableInt(f);
@@ -382,7 +382,7 @@ public abstract class FieldCacheRangeFilter extends Filter {
* double fields containing exactly one numeric term in the field. The range can be half-open by setting one
* of the values to <code>null</code>.
*/
- public static FieldCacheRangeFilter newDoubleRange(String field, Double lowerVal, Double upperVal, boolean includeLower, boolean includeUpper) {
+ public static FieldCacheRangeFilter<Double> newDoubleRange(String field, Double lowerVal, Double upperVal, boolean includeLower, boolean includeUpper) {
return newDoubleRange(field, null, lowerVal, upperVal, includeLower, includeUpper);
}
@@ -391,14 +391,14 @@ public abstract class FieldCacheRangeFilter extends Filter {
* double fields containing exactly one numeric term in the field. The range can be half-open by setting one
* of the values to <code>null</code>.
*/
- public static FieldCacheRangeFilter newDoubleRange(String field, FieldCache.DoubleParser parser, Double lowerVal, Double upperVal, boolean includeLower, boolean includeUpper) {
- return new FieldCacheRangeFilter(field, parser, lowerVal, upperVal, includeLower, includeUpper) {
+ public static FieldCacheRangeFilter<Double> newDoubleRange(String field, FieldCache.DoubleParser parser, Double lowerVal, Double upperVal, boolean includeLower, boolean includeUpper) {
+ return new FieldCacheRangeFilter<Double>(field, parser, lowerVal, upperVal, includeLower, includeUpper) {
public DocIdSet getDocIdSet(IndexReader reader) throws IOException {
// we transform the floating point numbers to sortable integers
// using NumericUtils to easier find the next bigger/lower value
final double inclusiveLowerPoint, inclusiveUpperPoint;
if (lowerVal != null) {
- double f = ((Number) lowerVal).doubleValue();
+ double f = lowerVal.doubleValue();
if (!includeUpper && f > 0.0 && Double.isInfinite(f))
return DocIdSet.EMPTY_DOCIDSET;
long i = NumericUtils.doubleToSortableLong(f);
@@ -407,7 +407,7 @@ public abstract class FieldCacheRangeFilter extends Filter {
inclusiveLowerPoint = Double.NEGATIVE_INFINITY;
}
if (upperVal != null) {
- double f = ((Number) upperVal).doubleValue();
+ double f = upperVal.doubleValue();
if (!includeUpper && f < 0.0 && Double.isInfinite(f))
return DocIdSet.EMPTY_DOCIDSET;
long i = NumericUtils.doubleToSortableLong(f);
@@ -464,6 +464,24 @@ public abstract class FieldCacheRangeFilter extends Filter {
h ^= (includeLower ? 1549299360 : -365038026) ^ (includeUpper ? 1721088258 : 1948649653);
return h;
}
+
+ /** Returns the field name for this query */
+ public String getField() { return field; }
+
+ /** Returns <code>true</code> if the lower endpoint is inclusive */
+ public boolean includesLower() { return includeLower; }
+
+ /** Returns <code>true</code> if the upper endpoint is inclusive */
+ public boolean includesUpper() { return includeUpper; }
+
+ /** Returns the lower value of this range query */
+ public T getLowerVal() { return lowerVal; }
+
+ /** Returns the upper value of this range query */
+ public T getUpperVal() { return upperVal; }
+
+ /** Returns the current numeric parser ({@code null} for {@code T} is {@code String}} */
+ public FieldCache.Parser getParser() { return parser; }
static abstract class FieldCacheDocIdSet extends DocIdSet {
private final IndexReader reader;