You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@lucene.apache.org by "gaobinlong (via GitHub)" <gi...@apache.org> on 2026/03/20 06:12:43 UTC

[PR] Replace explicit type conversion with instanceof pattern variable in part non-core modules [lucene]

gaobinlong opened a new pull request, #15842:
URL: https://github.com/apache/lucene/pull/15842

   ### Description
   
   As a follow-up of https://github.com/apache/lucene/pull/15837, replace all explicit type conversions with instanceof pattern variable in part non-core modules like analysis, codecs, facets, grouping, highlighter etc, using the modern style aims to improve the code readability and reduces potential ClassCastException risks.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org


Re: [PR] Replace explicit type conversion with instanceof pattern variable in part non-core modules [lucene]

Posted by "dweiss (via GitHub)" <gi...@apache.org>.
dweiss merged PR #15842:
URL: https://github.com/apache/lucene/pull/15842


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org


Re: [PR] Replace explicit type conversion with instanceof pattern variable in part non-core modules [lucene]

Posted by "dweiss (via GitHub)" <gi...@apache.org>.
dweiss commented on PR #15842:
URL: https://github.com/apache/lucene/pull/15842#issuecomment-4170823400

   Also, conflict needs to be resolved.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org


Re: [PR] Replace explicit type conversion with instanceof pattern variable in part non-core modules [lucene]

Posted by "gaobinlong (via GitHub)" <gi...@apache.org>.
gaobinlong commented on code in PR #15842:
URL: https://github.com/apache/lucene/pull/15842#discussion_r3026060612


##########
lucene/analysis/common/src/java/org/apache/lucene/analysis/compound/hyphenation/PatternParser.java:
##########
@@ -202,11 +202,11 @@ protected String getExceptionWord(ArrayList<?> ex) {
     StringBuilder res = new StringBuilder();
     for (int i = 0; i < ex.size(); i++) {
       Object item = ex.get(i);
-      if (item instanceof String) {
-        res.append((String) item);
+      if (item instanceof String s) {
+        res.append(s);
       } else {
-        if (((Hyphen) item).noBreak != null) {
-          res.append(((Hyphen) item).noBreak);
+        if (item instanceof Hyphen h && h.noBreak != null) {

Review Comment:
   Thanks, reverted that back.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org


Re: [PR] Replace explicit type conversion with instanceof pattern variable in part non-core modules [lucene]

Posted by "gaobinlong (via GitHub)" <gi...@apache.org>.
gaobinlong commented on code in PR #15842:
URL: https://github.com/apache/lucene/pull/15842#discussion_r3026061214


##########
lucene/monitor/src/java/org/apache/lucene/monitor/RegexpQueryHandler.java:
##########
@@ -94,36 +94,35 @@ public TokenStream wrapTermStream(String field, TokenStream ts) {
 
   @Override
   public QueryTree handleQuery(Query q, TermWeightor termWeightor) {
-    if (q instanceof RegexpQuery == false) {
-      return null;
+    if (q instanceof RegexpQuery query) {
+      String regexp = parseOutRegexp(query.toString(""));
+      String selected = selectLongestSubstring(regexp);

Review Comment:
   Changed that, thanks!



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org


Re: [PR] Replace explicit type conversion with instanceof pattern variable in part non-core modules [lucene]

Posted by "dweiss (via GitHub)" <gi...@apache.org>.
dweiss commented on code in PR #15842:
URL: https://github.com/apache/lucene/pull/15842#discussion_r3022751309


##########
lucene/analysis/common/src/java/org/apache/lucene/analysis/compound/hyphenation/PatternParser.java:
##########
@@ -202,11 +202,11 @@ protected String getExceptionWord(ArrayList<?> ex) {
     StringBuilder res = new StringBuilder();
     for (int i = 0; i < ex.size(); i++) {
       Object item = ex.get(i);
-      if (item instanceof String) {
-        res.append((String) item);
+      if (item instanceof String s) {
+        res.append(s);
       } else {
-        if (((Hyphen) item).noBreak != null) {
-          res.append(((Hyphen) item).noBreak);
+        if (item instanceof Hyphen h && h.noBreak != null) {

Review Comment:
   this is subtly different - the refactored version will silently pass when item is not a Hyphen (previously, it'd throw ClassCastException. I don't think this matters.



##########
lucene/monitor/src/java/org/apache/lucene/monitor/RegexpQueryHandler.java:
##########
@@ -94,36 +94,35 @@ public TokenStream wrapTermStream(String field, TokenStream ts) {
 
   @Override
   public QueryTree handleQuery(Query q, TermWeightor termWeightor) {
-    if (q instanceof RegexpQuery == false) {
-      return null;
+    if (q instanceof RegexpQuery query) {
+      String regexp = parseOutRegexp(query.toString(""));
+      String selected = selectLongestSubstring(regexp);

Review Comment:
   I think I liked the previous version better - the short-circuit at the front instead of the long conditional block.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org