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

orpiske pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/main by this push:
     new 0b1c782bcb3 (chores) convert core/camel-api to use pattern matching 
for instanceof
0b1c782bcb3 is described below

commit 0b1c782bcb366a566a1ddcafac7a75540f7a0da6
Author: Otavio Rodolfo Piske <[email protected]>
AuthorDate: Wed Aug 14 14:28:02 2024 +0200

    (chores) convert core/camel-api to use pattern matching for instanceof
---
 .../java/org/apache/camel/CamelContextAware.java   |  4 +-
 .../org/apache/camel/CamelExecutionException.java  |  4 +-
 .../src/main/java/org/apache/camel/Component.java  |  4 +-
 .../java/org/apache/camel/LineNumberAware.java     |  6 +-
 .../org/apache/camel/RuntimeCamelException.java    |  8 +--
 .../java/org/apache/camel/resume/Serializable.java | 12 ++--
 .../main/java/org/apache/camel/spi/DataType.java   |  7 +-
 .../java/org/apache/camel/spi/ResourceAware.java   |  4 +-
 .../java/org/apache/camel/spi/RoutesLoader.java    |  8 +--
 .../camel/support/jsse/SSLContextParameters.java   |  4 +-
 .../camel/support/service/ServiceHelper.java       | 82 ++++++++++------------
 11 files changed, 68 insertions(+), 75 deletions(-)

diff --git 
a/core/camel-api/src/main/java/org/apache/camel/CamelContextAware.java 
b/core/camel-api/src/main/java/org/apache/camel/CamelContextAware.java
index e94ab034425..42f3275c711 100644
--- a/core/camel-api/src/main/java/org/apache/camel/CamelContextAware.java
+++ b/core/camel-api/src/main/java/org/apache/camel/CamelContextAware.java
@@ -27,8 +27,8 @@ public interface CamelContextAware extends HasCamelContext {
      * Set the {@link CamelContext} context if the object is an instance of 
{@link CamelContextAware}.
      */
     static <T> T trySetCamelContext(T object, CamelContext camelContext) {
-        if (camelContext != null && object instanceof CamelContextAware) {
-            ((CamelContextAware) object).setCamelContext(camelContext);
+        if (camelContext != null && object instanceof CamelContextAware 
camelContextAware) {
+            camelContextAware.setCamelContext(camelContext);
         }
 
         return object;
diff --git 
a/core/camel-api/src/main/java/org/apache/camel/CamelExecutionException.java 
b/core/camel-api/src/main/java/org/apache/camel/CamelExecutionException.java
index 08136d8a9a4..8de9f979276 100644
--- a/core/camel-api/src/main/java/org/apache/camel/CamelExecutionException.java
+++ b/core/camel-api/src/main/java/org/apache/camel/CamelExecutionException.java
@@ -38,9 +38,9 @@ public class CamelExecutionException extends 
RuntimeExchangeException {
      * @return   the wrapper exception
      */
     public static CamelExecutionException wrapCamelExecutionException(Exchange 
exchange, Throwable e) {
-        if (e instanceof CamelExecutionException) {
+        if (e instanceof CamelExecutionException ce) {
             // don't double wrap
-            return (CamelExecutionException) e;
+            return ce;
         } else {
             return new CamelExecutionException("Exception occurred during 
execution", exchange, e);
         }
diff --git a/core/camel-api/src/main/java/org/apache/camel/Component.java 
b/core/camel-api/src/main/java/org/apache/camel/Component.java
index d80d5fc2870..e4a01005e94 100644
--- a/core/camel-api/src/main/java/org/apache/camel/Component.java
+++ b/core/camel-api/src/main/java/org/apache/camel/Component.java
@@ -106,8 +106,8 @@ public interface Component extends CamelContextAware, 
Service {
      * Set the {@link Component} context if the component is an instance of 
{@link ComponentAware}.
      */
     static <T> T trySetComponent(T object, Component component) {
-        if (object instanceof ComponentAware) {
-            ((ComponentAware) object).setComponent(component);
+        if (object instanceof ComponentAware componentAware) {
+            componentAware.setComponent(component);
         }
 
         return object;
diff --git a/core/camel-api/src/main/java/org/apache/camel/LineNumberAware.java 
b/core/camel-api/src/main/java/org/apache/camel/LineNumberAware.java
index bc3631241e9..e917848ea09 100644
--- a/core/camel-api/src/main/java/org/apache/camel/LineNumberAware.java
+++ b/core/camel-api/src/main/java/org/apache/camel/LineNumberAware.java
@@ -50,9 +50,9 @@ public interface LineNumberAware {
      * Set the {@link LineNumberAware} if the object is an instance of {@link 
LineNumberAware}.
      */
     static <T> T trySetLineNumberAware(T object, LineNumberAware source) {
-        if (source != null && object instanceof LineNumberAware) {
-            ((LineNumberAware) object).setLineNumber(source.getLineNumber());
-            ((LineNumberAware) object).setLocation(source.getLocation());
+        if (source != null && object instanceof LineNumberAware 
lineNumberAware) {
+            lineNumberAware.setLineNumber(source.getLineNumber());
+            lineNumberAware.setLocation(source.getLocation());
         }
         return object;
     }
diff --git 
a/core/camel-api/src/main/java/org/apache/camel/RuntimeCamelException.java 
b/core/camel-api/src/main/java/org/apache/camel/RuntimeCamelException.java
index eb76ae93493..8a467f975b3 100644
--- a/core/camel-api/src/main/java/org/apache/camel/RuntimeCamelException.java
+++ b/core/camel-api/src/main/java/org/apache/camel/RuntimeCamelException.java
@@ -46,9 +46,9 @@ public class RuntimeCamelException extends RuntimeException {
      * @return   the wrapper exception
      */
     public static RuntimeCamelException wrapRuntimeCamelException(Throwable e) 
{
-        if (e instanceof RuntimeCamelException) {
+        if (e instanceof RuntimeCamelException re) {
             // don't double wrap
-            return (RuntimeCamelException) e;
+            return re;
         } else {
             return new RuntimeCamelException(e);
         }
@@ -61,9 +61,9 @@ public class RuntimeCamelException extends RuntimeException {
      * @return   the wrapper exception
      */
     public static RuntimeException wrapRuntimeException(Throwable e) {
-        if (e instanceof RuntimeException) {
+        if (e instanceof RuntimeException re) {
             // don't double wrap
-            return (RuntimeException) e;
+            return re;
         } else {
             return new RuntimeCamelException(e);
         }
diff --git 
a/core/camel-api/src/main/java/org/apache/camel/resume/Serializable.java 
b/core/camel-api/src/main/java/org/apache/camel/resume/Serializable.java
index d3e1092aabe..0103d57e883 100644
--- a/core/camel-api/src/main/java/org/apache/camel/resume/Serializable.java
+++ b/core/camel-api/src/main/java/org/apache/camel/resume/Serializable.java
@@ -43,16 +43,16 @@ public interface Serializable {
     default ByteBuffer serialize(Object obj) {
         ObjectHelper.notNull(obj, "Cannot perform serialization on a null 
object");
 
-        if (obj instanceof Long) {
+        if (obj instanceof Long value) {
             ByteBuffer buffer = ByteBuffer.allocate(Integer.BYTES + 
Long.BYTES);
             buffer.putInt(TYPE_LONG);
 
-            long data = ((Long) obj).longValue();
+            long data = value.longValue();
             buffer.putLong(data);
             return buffer;
         }
-        if (obj instanceof String) {
-            byte[] data = ((String) obj).getBytes();
+        if (obj instanceof String str) {
+            byte[] data = str.getBytes();
 
             ByteBuffer buffer = ByteBuffer.allocate(Integer.BYTES + 
data.length);
             buffer.putInt(TYPE_STRING);
@@ -60,9 +60,9 @@ public interface Serializable {
 
             return buffer;
         }
-        if (obj instanceof File) {
+        if (obj instanceof File file) {
 
-            byte[] data = ((File) obj).getPath().getBytes();
+            byte[] data = file.getPath().getBytes();
             ByteBuffer buffer = ByteBuffer.allocate(Integer.BYTES + 
data.length);
 
             buffer.putInt(TYPE_FILE);
diff --git a/core/camel-api/src/main/java/org/apache/camel/spi/DataType.java 
b/core/camel-api/src/main/java/org/apache/camel/spi/DataType.java
index 333c262b47f..eff7ab184a1 100644
--- a/core/camel-api/src/main/java/org/apache/camel/spi/DataType.java
+++ b/core/camel-api/src/main/java/org/apache/camel/spi/DataType.java
@@ -106,10 +106,9 @@ public class DataType {
 
     @Override
     public boolean equals(Object target) {
-        if (target instanceof DataType) {
-            DataType targetdt = (DataType) target;
-            String targetScheme = targetdt.getScheme();
-            String targetName = targetdt.getName();
+        if (target instanceof DataType targetDt) {
+            String targetScheme = targetDt.getScheme();
+            String targetName = targetDt.getName();
             if (targetScheme == null) {
                 return false;
             } else if (targetName == null) {
diff --git 
a/core/camel-api/src/main/java/org/apache/camel/spi/ResourceAware.java 
b/core/camel-api/src/main/java/org/apache/camel/spi/ResourceAware.java
index f02b8dc8ab4..3469b4ef000 100644
--- a/core/camel-api/src/main/java/org/apache/camel/spi/ResourceAware.java
+++ b/core/camel-api/src/main/java/org/apache/camel/spi/ResourceAware.java
@@ -25,8 +25,8 @@ public interface ResourceAware {
      * Set the {@link Resource} resource if the object is an instance of 
{@link ResourceAware}.
      */
     static <T> T trySetResource(T object, Resource resource) {
-        if (resource != null && object instanceof ResourceAware) {
-            ((ResourceAware) object).setResource(resource);
+        if (resource != null && object instanceof ResourceAware resourceAware) 
{
+            resourceAware.setResource(resource);
         }
 
         return object;
diff --git 
a/core/camel-api/src/main/java/org/apache/camel/spi/RoutesLoader.java 
b/core/camel-api/src/main/java/org/apache/camel/spi/RoutesLoader.java
index 59694f32742..a215ee78171 100644
--- a/core/camel-api/src/main/java/org/apache/camel/spi/RoutesLoader.java
+++ b/core/camel-api/src/main/java/org/apache/camel/spi/RoutesLoader.java
@@ -63,8 +63,8 @@ public interface RoutesLoader extends CamelContextAware {
         Collection<RoutesBuilder> builders = findRoutesBuilders(resources);
         // add configuration first before the routes
         for (RoutesBuilder builder : builders) {
-            if (builder instanceof RouteConfigurationsBuilder) {
-                
getCamelContext().addRoutesConfigurations((RouteConfigurationsBuilder) builder);
+            if (builder instanceof RouteConfigurationsBuilder rcb) {
+                getCamelContext().addRoutesConfigurations(rcb);
             }
         }
         for (RoutesBuilder builder : builders) {
@@ -85,8 +85,8 @@ public interface RoutesLoader extends CamelContextAware {
         Collection<RoutesBuilder> builders = findRoutesBuilders(resources);
         // add configuration first before the routes
         for (RoutesBuilder builder : builders) {
-            if (builder instanceof RouteConfigurationsBuilder) {
-                
getCamelContext().addRoutesConfigurations((RouteConfigurationsBuilder) builder);
+            if (builder instanceof RouteConfigurationsBuilder rcb) {
+                getCamelContext().addRoutesConfigurations(rcb);
             }
         }
         for (RoutesBuilder builder : builders) {
diff --git 
a/core/camel-api/src/main/java/org/apache/camel/support/jsse/SSLContextParameters.java
 
b/core/camel-api/src/main/java/org/apache/camel/support/jsse/SSLContextParameters.java
index f4dab7a2d9a..3ea967d6082 100644
--- 
a/core/camel-api/src/main/java/org/apache/camel/support/jsse/SSLContextParameters.java
+++ 
b/core/camel-api/src/main/java/org/apache/camel/support/jsse/SSLContextParameters.java
@@ -281,11 +281,11 @@ public class SSLContextParameters extends 
BaseSSLContextParameters {
 
         if (this.getCertAlias() != null && keyManagers != null) {
             for (int idx = 0; idx < keyManagers.length; idx++) {
-                if (keyManagers[idx] instanceof X509KeyManager) {
+                if (keyManagers[idx] instanceof X509KeyManager x509KeyManager) 
{
                     try {
                         keyManagers[idx] = new AliasedX509ExtendedKeyManager(
                                 this.parsePropertyValue(this.getCertAlias()),
-                                (X509KeyManager) keyManagers[idx]);
+                                x509KeyManager);
                     } catch (Exception e) {
                         throw new GeneralSecurityException(e);
                     }
diff --git 
a/core/camel-api/src/main/java/org/apache/camel/support/service/ServiceHelper.java
 
b/core/camel-api/src/main/java/org/apache/camel/support/service/ServiceHelper.java
index e1424fb3b43..a435dc80b11 100644
--- 
a/core/camel-api/src/main/java/org/apache/camel/support/service/ServiceHelper.java
+++ 
b/core/camel-api/src/main/java/org/apache/camel/support/service/ServiceHelper.java
@@ -51,10 +51,10 @@ public final class ServiceHelper {
      * Calling this method has no effect if {@code value} is {@code null}.
      */
     public static void buildService(Object value) {
-        if (value instanceof Service) {
-            ((Service) value).build();
-        } else if (value instanceof Iterable) {
-            for (Object o : (Iterable) value) {
+        if (value instanceof Service service) {
+            service.build();
+        } else if (value instanceof Iterable iterable) {
+            for (Object o : iterable) {
                 buildService(o);
             }
         }
@@ -80,10 +80,10 @@ public final class ServiceHelper {
      * Calling this method has no effect if {@code value} is {@code null}.
      */
     public static void initService(Object value) {
-        if (value instanceof Service) {
-            ((Service) value).init();
-        } else if (value instanceof Iterable) {
-            for (Object o : (Iterable) value) {
+        if (value instanceof Service service) {
+            service.init();
+        } else if (value instanceof Iterable iterable) {
+            for (Object o : iterable) {
                 initService(o);
             }
         }
@@ -109,10 +109,10 @@ public final class ServiceHelper {
      * Calling this method has no effect if {@code value} is {@code null}.
      */
     public static void startService(Object value) {
-        if (value instanceof Service) {
-            startService((Service) value);
-        } else if (value instanceof Iterable) {
-            startService((Iterable<?>) value);
+        if (value instanceof Service service) {
+            startService(service);
+        } else if (value instanceof Iterable iterable) {
+            startService(iterable);
         }
     }
 
@@ -181,10 +181,10 @@ public final class ServiceHelper {
      * @see #stopService(Collection)
      */
     public static void stopService(Object value) {
-        if (value instanceof Service) {
-            stopService((Service) value);
-        } else if (value instanceof Iterable) {
-            stopService((Iterable<?>) value);
+        if (value instanceof Service service) {
+            stopService(service);
+        } else if (value instanceof Iterable iterable) {
+            stopService(iterable);
         }
     }
 
@@ -278,9 +278,8 @@ public final class ServiceHelper {
         stopService(value);
 
         // then try to shutdown
-        if (value instanceof ShutdownableService) {
-            ShutdownableService service = (ShutdownableService) value;
-            LOG.trace("Shutting down service {}", value);
+        if (value instanceof ShutdownableService service) {
+            LOG.trace("Shutting down service {}", service);
             service.shutdown();
         }
     }
@@ -351,8 +350,7 @@ public final class ServiceHelper {
         }
         RuntimeException firstException = null;
         for (Object value : services) {
-            if (value instanceof Service) {
-                Service service = (Service) value;
+            if (value instanceof Service service) {
                 try {
                     resumeService(service);
                 } catch (RuntimeException e) {
@@ -389,8 +387,7 @@ public final class ServiceHelper {
      * @see              #startService(Object)
      */
     public static boolean resumeService(Object service) {
-        if (service instanceof Suspendable && service instanceof 
SuspendableService) {
-            SuspendableService ss = (SuspendableService) service;
+        if (service instanceof Suspendable && service instanceof 
SuspendableService ss) {
             if (ss.isSuspended()) {
                 LOG.debug("Resuming service {}", service);
                 ss.resume();
@@ -419,8 +416,7 @@ public final class ServiceHelper {
         }
         RuntimeException firstException = null;
         for (Object value : services) {
-            if (value instanceof Service) {
-                Service service = (Service) value;
+            if (value instanceof Service service) {
                 try {
                     suspendService(service);
                 } catch (RuntimeException e) {
@@ -457,8 +453,7 @@ public final class ServiceHelper {
      * @see              #stopService(Object)
      */
     public static boolean suspendService(Object service) {
-        if (service instanceof Suspendable && service instanceof 
SuspendableService) {
-            SuspendableService ss = (SuspendableService) service;
+        if (service instanceof Suspendable && service instanceof 
SuspendableService ss) {
             if (!ss.isSuspended()) {
                 LOG.trace("Suspending service {}", service);
                 ss.suspend();
@@ -480,8 +475,8 @@ public final class ServiceHelper {
      * @see    StatefulService#isStopped()
      */
     public static boolean isStopped(Object value) {
-        if (value instanceof StatefulService) {
-            return isStopped((StatefulService) value);
+        if (value instanceof StatefulService statefulService) {
+            return isStopped(statefulService);
         }
 
         return false;
@@ -510,8 +505,8 @@ public final class ServiceHelper {
      * @see    StatefulService#isStarted()
      */
     public static boolean isStarted(Object value) {
-        if (value instanceof StatefulService) {
-            return isStarted((StatefulService) value);
+        if (value instanceof StatefulService statefulService) {
+            return isStarted(statefulService);
         }
 
         return false;
@@ -540,8 +535,8 @@ public final class ServiceHelper {
      * @see    StatefulService#isSuspended()
      */
     public static boolean isSuspended(Object value) {
-        if (value instanceof StatefulService) {
-            return isSuspended((StatefulService) value);
+        if (value instanceof StatefulService statefulService) {
+            return isSuspended(statefulService);
         }
 
         return false;
@@ -589,26 +584,25 @@ public final class ServiceHelper {
 
     private static void doGetChildServices(Set<Service> services, Service 
service, boolean includeErrorHandler) {
         services.add(service);
-        if (service instanceof Navigate) {
-            Navigate<?> nav = (Navigate<?>) service;
+        if (service instanceof Navigate nav) {
             if (nav.hasNext()) {
                 List<?> children = nav.next();
                 for (Object child : children) {
-                    if (child instanceof Channel) {
+                    if (child instanceof Channel channel) {
                         if (includeErrorHandler) {
                             // special for error handler as they are tied to 
the Channel
-                            Processor errorHandler = ((Channel) 
child).getErrorHandler();
-                            if (errorHandler instanceof Service) {
-                                services.add((Service) errorHandler);
+                            Processor errorHandler = channel.getErrorHandler();
+                            if (errorHandler instanceof Service errService) {
+                                services.add(errService);
                             }
                         }
-                        Processor next = ((Channel) child).getNextProcessor();
-                        if (next instanceof Service) {
-                            services.add((Service) next);
+                        Processor next = channel.getNextProcessor();
+                        if (next instanceof Service nextService) {
+                            services.add(nextService);
                         }
                     }
-                    if (child instanceof Service) {
-                        doGetChildServices(services, (Service) child, 
includeErrorHandler);
+                    if (child instanceof Service childService) {
+                        doGetChildServices(services, childService, 
includeErrorHandler);
                     }
                 }
             }

Reply via email to