You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by ja...@apache.org on 2025/08/14 18:50:41 UTC

(lucene) branch branch_10x updated: Use switch expression for optimized type checks in PointRangeQueryBuilder (#15050)

This is an automated email from the ASF dual-hosted git repository.

jainankitk pushed a commit to branch branch_10x
in repository https://gitbox.apache.org/repos/asf/lucene.git


The following commit(s) were added to refs/heads/branch_10x by this push:
     new 3a99fae956f Use switch expression for optimized type checks in PointRangeQueryBuilder (#15050)
3a99fae956f is described below

commit 3a99fae956f9b848677f831c9c87030ea5113d55
Author: Sandesh Kumar <sa...@gmail.com>
AuthorDate: Thu Aug 14 11:46:49 2025 -0700

    Use switch expression for optimized type checks in PointRangeQueryBuilder (#15050)
    
    ---------
    
    Signed-off-by: Sandesh Kumar <sa...@gmail.com>
---
 lucene/CHANGES.txt                                 |  2 +
 .../xml/builders/PointRangeQueryBuilder.java       | 50 +++++++++++-----------
 2 files changed, 28 insertions(+), 24 deletions(-)

diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 08577e0b826..521328a1d18 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -199,6 +199,8 @@ Other
 
 * GITHUB#14839: Add support for sparse SortedDocValues to LuceneTestCase.assertDocValuesEquals (Parker Timmins)
 
+* GITHUB#15050: Use switch expression for optimized type checks in PointRangeQueryBuilder (Sandesh Kumar)
+
 ======================= Lucene 10.2.2 =======================
 
 Bug Fixes
diff --git a/lucene/queryparser/src/java/org/apache/lucene/queryparser/xml/builders/PointRangeQueryBuilder.java b/lucene/queryparser/src/java/org/apache/lucene/queryparser/xml/builders/PointRangeQueryBuilder.java
index 25df87628fa..489baf1436c 100644
--- a/lucene/queryparser/src/java/org/apache/lucene/queryparser/xml/builders/PointRangeQueryBuilder.java
+++ b/lucene/queryparser/src/java/org/apache/lucene/queryparser/xml/builders/PointRangeQueryBuilder.java
@@ -16,6 +16,7 @@
  */
 package org.apache.lucene.queryparser.xml.builders;
 
+import java.util.Locale;
 import org.apache.lucene.document.DoublePoint;
 import org.apache.lucene.document.FloatPoint;
 import org.apache.lucene.document.IntPoint;
@@ -75,32 +76,33 @@ public class PointRangeQueryBuilder implements QueryBuilder {
     String field = DOMUtils.getAttributeWithInheritanceOrFail(e, "fieldName");
     final String lowerTerm = DOMUtils.getAttribute(e, "lowerTerm", null);
     final String upperTerm = DOMUtils.getAttribute(e, "upperTerm", null);
+    String type = DOMUtils.getAttribute(e, "type", "int").toLowerCase(Locale.getDefault());
 
-    String type = DOMUtils.getAttribute(e, "type", "int");
     try {
-      if (type.equalsIgnoreCase("int")) {
-        return IntPoint.newRangeQuery(
-            field,
-            (lowerTerm == null ? Integer.MIN_VALUE : Integer.parseInt(lowerTerm)),
-            (upperTerm == null ? Integer.MAX_VALUE : Integer.parseInt(upperTerm)));
-      } else if (type.equalsIgnoreCase("long")) {
-        return LongPoint.newRangeQuery(
-            field,
-            (lowerTerm == null ? Long.MIN_VALUE : Long.parseLong(lowerTerm)),
-            (upperTerm == null ? Long.MAX_VALUE : Long.parseLong(upperTerm)));
-      } else if (type.equalsIgnoreCase("double")) {
-        return DoublePoint.newRangeQuery(
-            field,
-            (lowerTerm == null ? Double.NEGATIVE_INFINITY : Double.parseDouble(lowerTerm)),
-            (upperTerm == null ? Double.POSITIVE_INFINITY : Double.parseDouble(upperTerm)));
-      } else if (type.equalsIgnoreCase("float")) {
-        return FloatPoint.newRangeQuery(
-            field,
-            (lowerTerm == null ? Float.NEGATIVE_INFINITY : Float.parseFloat(lowerTerm)),
-            (upperTerm == null ? Float.POSITIVE_INFINITY : Float.parseFloat(upperTerm)));
-      } else {
-        throw new ParserException("type attribute must be one of: [long, int, double, float]");
-      }
+      return switch (type) {
+        case "int" ->
+            IntPoint.newRangeQuery(
+                field,
+                (lowerTerm == null ? Integer.MIN_VALUE : Integer.parseInt(lowerTerm)),
+                (upperTerm == null ? Integer.MAX_VALUE : Integer.parseInt(upperTerm)));
+        case "long" ->
+            LongPoint.newRangeQuery(
+                field,
+                (lowerTerm == null ? Long.MIN_VALUE : Long.parseLong(lowerTerm)),
+                (upperTerm == null ? Long.MAX_VALUE : Long.parseLong(upperTerm)));
+        case "double" ->
+            DoublePoint.newRangeQuery(
+                field,
+                (lowerTerm == null ? Double.NEGATIVE_INFINITY : Double.parseDouble(lowerTerm)),
+                (upperTerm == null ? Double.POSITIVE_INFINITY : Double.parseDouble(upperTerm)));
+        case "float" ->
+            FloatPoint.newRangeQuery(
+                field,
+                (lowerTerm == null ? Float.NEGATIVE_INFINITY : Float.parseFloat(lowerTerm)),
+                (upperTerm == null ? Float.POSITIVE_INFINITY : Float.parseFloat(upperTerm)));
+        default ->
+            throw new ParserException("type attribute must be one of: [long, int, double, float]");
+      };
     } catch (NumberFormatException nfe) {
       throw new ParserException("Could not parse lowerTerm or upperTerm into a number", nfe);
     }