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 d6f7e68a941 (chores) convert core/camel-util to use pattern matching
for instanceof
d6f7e68a941 is described below
commit d6f7e68a941f410974666d49eb83767e983c762d
Author: Otavio Rodolfo Piske <[email protected]>
AuthorDate: Wed Aug 21 13:29:09 2024 +0200
(chores) convert core/camel-util to use pattern matching for instanceof
---
.../main/java/org/apache/camel/util/IOHelper.java | 16 +++----
.../java/org/apache/camel/util/ObjectHelper.java | 51 +++++++++++-----------
.../main/java/org/apache/camel/util/Scanner.java | 4 +-
.../java/org/apache/camel/util/URIScanner.java | 4 +-
.../java/org/apache/camel/util/URISupport.java | 3 +-
.../RejectableScheduledThreadPoolExecutor.java | 4 +-
.../concurrent/RejectableThreadPoolExecutor.java | 4 +-
.../concurrent/SizedScheduledExecutorService.java | 4 +-
.../util/concurrent/ThreadPoolRejectedPolicy.java | 4 +-
9 files changed, 46 insertions(+), 48 deletions(-)
diff --git a/core/camel-util/src/main/java/org/apache/camel/util/IOHelper.java
b/core/camel-util/src/main/java/org/apache/camel/util/IOHelper.java
index a6be1287b83..4048d442660 100644
--- a/core/camel-util/src/main/java/org/apache/camel/util/IOHelper.java
+++ b/core/camel-util/src/main/java/org/apache/camel/util/IOHelper.java
@@ -84,7 +84,7 @@ public final class IOHelper {
* @return the passed <code>in</code> decorated through a {@link
BufferedInputStream} object as wrapper
*/
public static BufferedInputStream buffered(InputStream in) {
- return (in instanceof BufferedInputStream) ? (BufferedInputStream) in
: new BufferedInputStream(in);
+ return (in instanceof BufferedInputStream bi) ? bi : new
BufferedInputStream(in);
}
/**
@@ -96,7 +96,7 @@ public final class IOHelper {
* @return the passed <code>out</code> decorated through a {@link
BufferedOutputStream} object as wrapper
*/
public static BufferedOutputStream buffered(OutputStream out) {
- return (out instanceof BufferedOutputStream) ? (BufferedOutputStream)
out : new BufferedOutputStream(out);
+ return (out instanceof BufferedOutputStream bo) ? bo : new
BufferedOutputStream(out);
}
/**
@@ -108,7 +108,7 @@ public final class IOHelper {
* @return the passed <code>reader</code> decorated through a
{@link BufferedReader} object as wrapper
*/
public static BufferedReader buffered(Reader reader) {
- return (reader instanceof BufferedReader) ? (BufferedReader) reader :
new BufferedReader(reader);
+ return (reader instanceof BufferedReader br) ? br : new
BufferedReader(reader);
}
/**
@@ -120,7 +120,7 @@ public final class IOHelper {
* @return the passed <code>writer</code> decorated through a
{@link BufferedWriter} object as wrapper
*/
public static BufferedWriter buffered(Writer writer) {
- return (writer instanceof BufferedWriter) ? (BufferedWriter) writer :
new BufferedWriter(writer);
+ return (writer instanceof BufferedWriter bw) ? bw : new
BufferedWriter(writer);
}
public static String toString(Reader reader) throws IOException {
@@ -494,11 +494,11 @@ public final class IOHelper {
}
public static void closeIterator(Object it) throws IOException {
- if (it instanceof Closeable) {
- IOHelper.closeWithException((Closeable) it);
+ if (it instanceof Closeable closeable) {
+ IOHelper.closeWithException(closeable);
}
- if (it instanceof java.util.Scanner) {
- IOException ioException = ((java.util.Scanner) it).ioException();
+ if (it instanceof java.util.Scanner scanner) {
+ IOException ioException = scanner.ioException();
if (ioException != null) {
throw ioException;
}
diff --git
a/core/camel-util/src/main/java/org/apache/camel/util/ObjectHelper.java
b/core/camel-util/src/main/java/org/apache/camel/util/ObjectHelper.java
index fdb9d126700..e82dc4e21c1 100644
--- a/core/camel-util/src/main/java/org/apache/camel/util/ObjectHelper.java
+++ b/core/camel-util/src/main/java/org/apache/camel/util/ObjectHelper.java
@@ -84,8 +84,8 @@ public final class ObjectHelper {
}
if (ignoreCase) {
- if (a instanceof String && b instanceof String) {
- return ((String) a).equalsIgnoreCase((String) b);
+ if (a instanceof String strA && b instanceof String strB) {
+ return strA.equalsIgnoreCase(strB);
}
}
@@ -118,24 +118,23 @@ public final class ObjectHelper {
}
public static Boolean toBoolean(Object value) {
- if (value instanceof Boolean) {
- return (Boolean) value;
+ if (value instanceof Boolean booleanValue) {
+ return booleanValue;
}
- if (value instanceof byte[]) {
- String str = new String((byte[]) value);
+ if (value instanceof byte[] bytes) {
+ String str = new String(bytes);
if (isBoolean(str)) {
return Boolean.valueOf(str);
}
}
- if (value instanceof String) {
+ if (value instanceof String str) {
// we only want to accept true or false as accepted values
- String str = (String) value;
if (isBoolean(str)) {
return Boolean.valueOf(str);
}
}
- if (value instanceof Integer) {
- return (Integer) value > 0 ? Boolean.TRUE : Boolean.FALSE;
+ if (value instanceof Integer integer) {
+ return integer > 0 ? Boolean.TRUE : Boolean.FALSE;
}
return null;
}
@@ -230,12 +229,12 @@ public final class ObjectHelper {
public static <T> boolean isEmpty(T value) {
if (value == null) {
return true;
- } else if (value instanceof String) {
- return isEmpty((String) value);
- } else if (value instanceof Collection) {
- return isEmpty((Collection<?>) value);
- } else if (value instanceof Map) {
- return isEmpty((Map<?, ?>) value);
+ } else if (value instanceof String str) {
+ return isEmpty(str);
+ } else if (value instanceof Collection<?> collection) {
+ return isEmpty(collection);
+ } else if (value instanceof Map<?, ?> valueMap) {
+ return isEmpty(valueMap);
} else {
return false;
}
@@ -328,13 +327,13 @@ public final class ObjectHelper {
* Returns the predicate matching boolean on a {@link List} result set
where if the first element is a boolean its
* value is used otherwise this method returns true if the collection is
not empty
*
- * @return <tt>true</tt> if the first element is a boolean and its value
is true or if the list is non empty
+ * @return <tt>true</tt> if the first element is a boolean, and its value
is true or if the list is non-empty
*/
public static boolean matches(List<?> list) {
if (!list.isEmpty()) {
Object value = list.get(0);
- if (value instanceof Boolean) {
- return (Boolean) value;
+ if (value instanceof Boolean booleanValue) {
+ return booleanValue;
} else {
// lets assume non-empty results are true
return true;
@@ -1167,15 +1166,15 @@ public final class ObjectHelper {
* the value is not null
*/
public static boolean evaluateValuePredicate(Object value) {
- if (value instanceof Boolean) {
- return (Boolean) value;
- } else if (value instanceof String) {
- return evaluateString((String) value);
+ if (value instanceof Boolean booleanValue) {
+ return booleanValue;
+ } else if (value instanceof String str) {
+ return evaluateString(str);
} else if (value instanceof NodeList) {
return evaluateNodeList(value);
- } else if (value instanceof Collection) {
+ } else if (value instanceof Collection<?> collection) {
// is it an empty collection
- return !((Collection<?>) value).isEmpty();
+ return !collection.isEmpty();
}
return value != null;
}
@@ -1195,7 +1194,7 @@ public final class ObjectHelper {
private static boolean evaluateNodeList(Object value) {
// is it an empty dom with empty attributes
- if (value instanceof Node && ((Node) value).hasAttributes()) {
+ if (value instanceof Node node && node.hasAttributes()) {
return true;
}
NodeList list = (NodeList) value;
diff --git a/core/camel-util/src/main/java/org/apache/camel/util/Scanner.java
b/core/camel-util/src/main/java/org/apache/camel/util/Scanner.java
index d423e9b4475..23cba9b6e3e 100644
--- a/core/camel-util/src/main/java/org/apache/camel/util/Scanner.java
+++ b/core/camel-util/src/main/java/org/apache/camel/util/Scanner.java
@@ -303,9 +303,9 @@ public final class Scanner implements Iterator<String>,
Closeable {
public void close() throws IOException {
if (!closed) {
closed = true;
- if (source instanceof Closeable) {
+ if (source instanceof Closeable closeable) {
try {
- ((Closeable) source).close();
+ closeable.close();
} catch (IOException e) {
lastIOException = e;
}
diff --git
a/core/camel-util/src/main/java/org/apache/camel/util/URIScanner.java
b/core/camel-util/src/main/java/org/apache/camel/util/URIScanner.java
index b53cef0158d..bb0b85ce3fd 100644
--- a/core/camel-util/src/main/java/org/apache/camel/util/URIScanner.java
+++ b/core/camel-util/src/main/java/org/apache/camel/util/URIScanner.java
@@ -172,8 +172,8 @@ class URIScanner {
// to hold the multiple values
Object existing = answer.get(name);
List<String> list;
- if (existing instanceof List) {
- list = CastUtils.cast((List<?>) existing);
+ if (existing instanceof List<?> existingList) {
+ list = CastUtils.cast(existingList);
} else {
// create a new list to hold the multiple values
list = new ArrayList<>();
diff --git
a/core/camel-util/src/main/java/org/apache/camel/util/URISupport.java
b/core/camel-util/src/main/java/org/apache/camel/util/URISupport.java
index fefc333727d..f3bb9a35586 100644
--- a/core/camel-util/src/main/java/org/apache/camel/util/URISupport.java
+++ b/core/camel-util/src/main/java/org/apache/camel/util/URISupport.java
@@ -339,8 +339,7 @@ public final class URISupport {
}
// if the value is a list then we need to iterate
Object value = entry.getValue();
- if (value instanceof List) {
- List list = (List) value;
+ if (value instanceof List list) {
for (int i = 0; i < list.size(); i++) {
Object obj = list.get(i);
if (obj == null) {
diff --git
a/core/camel-util/src/main/java/org/apache/camel/util/concurrent/RejectableScheduledThreadPoolExecutor.java
b/core/camel-util/src/main/java/org/apache/camel/util/concurrent/RejectableScheduledThreadPoolExecutor.java
index b8d0e0286c5..5d4ec6968f6 100644
---
a/core/camel-util/src/main/java/org/apache/camel/util/concurrent/RejectableScheduledThreadPoolExecutor.java
+++
b/core/camel-util/src/main/java/org/apache/camel/util/concurrent/RejectableScheduledThreadPoolExecutor.java
@@ -89,8 +89,8 @@ public class RejectableScheduledThreadPoolExecutor extends
ScheduledThreadPoolEx
@Override
public String toString() {
// the thread factory often have more precise details what the thread
pool is used for
- if (getThreadFactory() instanceof CamelThreadFactory) {
- String name = ((CamelThreadFactory) getThreadFactory()).getName();
+ if (getThreadFactory() instanceof CamelThreadFactory
camelThreadFactory) {
+ String name = camelThreadFactory.getName();
return super.toString() + "[" + name + "]";
} else {
return super.toString();
diff --git
a/core/camel-util/src/main/java/org/apache/camel/util/concurrent/RejectableThreadPoolExecutor.java
b/core/camel-util/src/main/java/org/apache/camel/util/concurrent/RejectableThreadPoolExecutor.java
index 907bc496619..793312b6e4e 100644
---
a/core/camel-util/src/main/java/org/apache/camel/util/concurrent/RejectableThreadPoolExecutor.java
+++
b/core/camel-util/src/main/java/org/apache/camel/util/concurrent/RejectableThreadPoolExecutor.java
@@ -95,8 +95,8 @@ public class RejectableThreadPoolExecutor extends
ThreadPoolExecutor {
@Override
public String toString() {
// the thread factory often have more precise details what the thread
pool is used for
- if (getThreadFactory() instanceof CamelThreadFactory) {
- String name = ((CamelThreadFactory) getThreadFactory()).getName();
+ if (getThreadFactory() instanceof CamelThreadFactory
camelThreadFactory) {
+ String name = camelThreadFactory.getName();
return super.toString() + "[" + name + "]";
} else {
return super.toString();
diff --git
a/core/camel-util/src/main/java/org/apache/camel/util/concurrent/SizedScheduledExecutorService.java
b/core/camel-util/src/main/java/org/apache/camel/util/concurrent/SizedScheduledExecutorService.java
index 2f7ca5094cf..1302d0a17f6 100644
---
a/core/camel-util/src/main/java/org/apache/camel/util/concurrent/SizedScheduledExecutorService.java
+++
b/core/camel-util/src/main/java/org/apache/camel/util/concurrent/SizedScheduledExecutorService.java
@@ -307,8 +307,8 @@ public class SizedScheduledExecutorService implements
ScheduledExecutorService {
@Override
public String toString() {
// the thread factory often have more precise details what the thread
pool is used for
- if (delegate.getThreadFactory() instanceof CamelThreadFactory) {
- String name = ((CamelThreadFactory)
delegate.getThreadFactory()).getName();
+ if (delegate.getThreadFactory() instanceof CamelThreadFactory
camelThreadFactory) {
+ String name = camelThreadFactory.getName();
return super.toString() + "[" + name + "]";
} else {
return super.toString();
diff --git
a/core/camel-util/src/main/java/org/apache/camel/util/concurrent/ThreadPoolRejectedPolicy.java
b/core/camel-util/src/main/java/org/apache/camel/util/concurrent/ThreadPoolRejectedPolicy.java
index 49fdf02e25e..1d0f47a5bb0 100644
---
a/core/camel-util/src/main/java/org/apache/camel/util/concurrent/ThreadPoolRejectedPolicy.java
+++
b/core/camel-util/src/main/java/org/apache/camel/util/concurrent/ThreadPoolRejectedPolicy.java
@@ -38,8 +38,8 @@ public enum ThreadPoolRejectedPolicy {
return new RejectedExecutionHandler() {
@Override
public void rejectedExecution(Runnable r, ThreadPoolExecutor
executor) {
- if (r instanceof Rejectable) {
- ((Rejectable) r).reject();
+ if (r instanceof Rejectable rejectable) {
+ rejectable.reject();
} else {
throw new RejectedExecutionException("Task " +
r.toString() + " rejected from " + executor.toString());
}