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
commit 0a07757a0e54c4d4ab73ecb4510fca2ca85e8afd Author: Otavio Rodolfo Piske <[email protected]> AuthorDate: Mon Aug 26 11:58:04 2024 +0200 (chores) camel-file simplify type checks - to use pattern matching for instanceof - use simpler assertion checks --- .../java/org/apache/camel/component/file/FileConsumer.java | 8 ++++---- .../org/apache/camel/component/file/FileOperations.java | 4 ++-- .../apache/camel/component/file/GenericFileConsumer.java | 4 ++-- .../apache/camel/component/file/GenericFileConverter.java | 6 ++---- .../org/apache/camel/component/file/GenericFileMessage.java | 4 ++-- .../apache/camel/component/file/GenericFileProducer.java | 4 ++-- .../adapters/DefaultDirectoryEntriesResumeAdapter.java | 4 ++-- .../consumer/adapters/DefaultFileOffsetResumeAdapter.java | 8 ++++---- .../file/consumer/adapters/FileResumeAdapterDelegate.java | 8 ++++---- .../file/strategy/GenericFileProcessStrategySupport.java | 4 ++-- .../file/strategy/GenericFileRenameProcessStrategy.java | 13 ++++--------- 11 files changed, 30 insertions(+), 37 deletions(-) diff --git a/components/camel-file/src/main/java/org/apache/camel/component/file/FileConsumer.java b/components/camel-file/src/main/java/org/apache/camel/component/file/FileConsumer.java index 8ea6d2d7440..1cc3327ef1c 100644 --- a/components/camel-file/src/main/java/org/apache/camel/component/file/FileConsumer.java +++ b/components/camel-file/src/main/java/org/apache/camel/component/file/FileConsumer.java @@ -115,9 +115,9 @@ public class FileConsumer extends GenericFileConsumer<File> implements ResumeAwa if (resumeStrategy != null) { final ResumeAdapter adapter = setupResumeStrategy(gf); - if (adapter instanceof DirectoryEntriesResumeAdapter) { + if (adapter instanceof DirectoryEntriesResumeAdapter directoryEntriesResumeAdapter) { LOG.trace("Running the resume process for file {}", file); - if (((DirectoryEntriesResumeAdapter) adapter).resume(file)) { + if (directoryEntriesResumeAdapter.resume(file)) { LOG.trace("Skipping file {} because it has been marked previously consumed", file); continue; } @@ -175,9 +175,9 @@ public class FileConsumer extends GenericFileConsumer<File> implements ResumeAwa private ResumeAdapter setupResumeStrategy(GenericFile<File> gf) { ResumeAdapter adapter = resumeStrategy.getAdapter(); LOG.trace("Checking the resume adapter: {}", adapter); - if (adapter instanceof FileOffsetResumeAdapter) { + if (adapter instanceof FileOffsetResumeAdapter fileOffsetResumeAdapter) { LOG.trace("The resume adapter is for offsets: {}", adapter); - ((FileOffsetResumeAdapter) adapter).setResumePayload(gf); + fileOffsetResumeAdapter.setResumePayload(gf); adapter.resume(); } return adapter; diff --git a/components/camel-file/src/main/java/org/apache/camel/component/file/FileOperations.java b/components/camel-file/src/main/java/org/apache/camel/component/file/FileOperations.java index 18d8a320d86..f08c96485ef 100644 --- a/components/camel-file/src/main/java/org/apache/camel/component/file/FileOperations.java +++ b/components/camel-file/src/main/java/org/apache/camel/component/file/FileOperations.java @@ -278,8 +278,8 @@ public class FileOperations implements GenericFileOperations<File> { // if no charset and not in appending mode, then we can try // using file directly (optimized) final Object body = extractBodyFromExchange(exchange); - if (body instanceof File) { - source = (File) body; + if (body instanceof File fileBody) { + source = fileBody; fileBased = true; } } diff --git a/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFileConsumer.java b/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFileConsumer.java index 0bd9a45a9c9..aa1baceee75 100644 --- a/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFileConsumer.java +++ b/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFileConsumer.java @@ -828,8 +828,8 @@ public abstract class GenericFileConsumer<T> extends ScheduledBatchPollingConsum protected void doInit() throws Exception { super.doInit(); // inject CamelContext before starting as it may be needed - if (processStrategy instanceof CamelContextAware) { - ((CamelContextAware) processStrategy).setCamelContext(getEndpoint().getCamelContext()); + if (processStrategy instanceof CamelContextAware camelContextAware) { + camelContextAware.setCamelContext(getEndpoint().getCamelContext()); } } diff --git a/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFileConverter.java b/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFileConverter.java index 0e85b1e5ef0..b87bb720815 100644 --- a/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFileConverter.java +++ b/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFileConverter.java @@ -109,9 +109,8 @@ public final class GenericFileConverter { @Converter public static InputStream genericFileToInputStream(GenericFile<?> file, Exchange exchange) throws IOException { - if (file.getFile() instanceof File) { + if (file.getFile() instanceof File f) { // prefer to use a file input stream if its a java.io.File - File f = (File) file.getFile(); // the file must exists if (f.exists()) { // read the file using the specified charset @@ -178,9 +177,8 @@ public final class GenericFileConverter { @Converter public static Reader genericFileToReader(GenericFile<?> file, Exchange exchange) throws IOException { - if (file.getFile() instanceof File) { + if (file.getFile() instanceof File f) { // prefer to use a file input stream if its a java.io.File - File f = (File) file.getFile(); // the file must exists if (!f.exists()) { return null; diff --git a/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFileMessage.java b/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFileMessage.java index 1d2d87cd872..a5282cf7e74 100644 --- a/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFileMessage.java +++ b/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFileMessage.java @@ -64,8 +64,8 @@ public class GenericFileMessage<T> extends DefaultMessage { public void copyFrom(Message that) { super.copyFrom(that); - if (that instanceof GenericFileMessage) { - setGenericFile(((GenericFileMessage) that).getGenericFile()); + if (that instanceof GenericFileMessage genericFileMessage) { + setGenericFile(genericFileMessage.getGenericFile()); } } diff --git a/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFileProducer.java b/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFileProducer.java index 5c549b1f5ed..8543f7f4e4c 100644 --- a/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFileProducer.java +++ b/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFileProducer.java @@ -369,8 +369,8 @@ public class GenericFileProducer<T> extends DefaultProducer { // expression support Expression expression = endpoint.getFileName(); - if (value instanceof Expression) { - expression = (Expression) value; + if (value instanceof Expression expression1) { + expression = expression1; } // evaluate the name as a String from the value diff --git a/components/camel-file/src/main/java/org/apache/camel/component/file/consumer/adapters/DefaultDirectoryEntriesResumeAdapter.java b/components/camel-file/src/main/java/org/apache/camel/component/file/consumer/adapters/DefaultDirectoryEntriesResumeAdapter.java index dc9f7afdfc5..a1c05439a5a 100644 --- a/components/camel-file/src/main/java/org/apache/camel/component/file/consumer/adapters/DefaultDirectoryEntriesResumeAdapter.java +++ b/components/camel-file/src/main/java/org/apache/camel/component/file/consumer/adapters/DefaultDirectoryEntriesResumeAdapter.java @@ -33,10 +33,10 @@ class DefaultDirectoryEntriesResumeAdapter extends AbstractFileResumeAdapter imp private static final Logger LOG = LoggerFactory.getLogger(DefaultDirectoryEntriesResumeAdapter.class); protected boolean add(Object key, Object offset) { - if (offset instanceof File) { + if (offset instanceof File fileBasedOffset) { FileSet fileSet = (FileSet) cache.computeIfAbsent((File) key, k -> new FileSet()); - fileSet.update((File) offset); + fileSet.update(fileBasedOffset); } else { throw new UnsupportedOperationException("This adapter cannot be used for file offsets"); } diff --git a/components/camel-file/src/main/java/org/apache/camel/component/file/consumer/adapters/DefaultFileOffsetResumeAdapter.java b/components/camel-file/src/main/java/org/apache/camel/component/file/consumer/adapters/DefaultFileOffsetResumeAdapter.java index 332c42ea242..e925a34a577 100644 --- a/components/camel-file/src/main/java/org/apache/camel/component/file/consumer/adapters/DefaultFileOffsetResumeAdapter.java +++ b/components/camel-file/src/main/java/org/apache/camel/component/file/consumer/adapters/DefaultFileOffsetResumeAdapter.java @@ -46,10 +46,10 @@ class DefaultFileOffsetResumeAdapter extends AbstractFileResumeAdapter implement } public boolean add(Object key, Object offset) { - if (offset instanceof Long) { + if (offset instanceof Long longOffset) { FileOffset fileOffset = (FileOffset) cache.computeIfAbsent((File) key, k -> new FileOffset()); - fileOffset.update((Long) offset); + fileOffset.update(longOffset); } else { throw new UnsupportedOperationException("This adapter cannot be used for directory entries"); } @@ -74,8 +74,8 @@ class DefaultFileOffsetResumeAdapter extends AbstractFileResumeAdapter implement return; } - if (offsetObj instanceof Long) { - genericFile.updateLastOffsetValue((Long) offsetObj); + if (offsetObj instanceof Long longOffsetObj) { + genericFile.updateLastOffsetValue(longOffsetObj); } else { // This should never happen LOG.warn("Cannot perform a resume operation of an object of unhandled type: {}", offsetObj.getClass()); diff --git a/components/camel-file/src/main/java/org/apache/camel/component/file/consumer/adapters/FileResumeAdapterDelegate.java b/components/camel-file/src/main/java/org/apache/camel/component/file/consumer/adapters/FileResumeAdapterDelegate.java index 78bf97643a4..9527eee5640 100644 --- a/components/camel-file/src/main/java/org/apache/camel/component/file/consumer/adapters/FileResumeAdapterDelegate.java +++ b/components/camel-file/src/main/java/org/apache/camel/component/file/consumer/adapters/FileResumeAdapterDelegate.java @@ -72,12 +72,12 @@ public class FileResumeAdapterDelegate Object keyObj = deserializeKey(keyBuffer); Object valueObj = deserializeValue(valueBuffer); - if (valueObj instanceof File) { - directoryEntriesResumeAdapter.deserializeFileEntry((File) keyObj, (File) valueObj); + if (valueObj instanceof File file) { + directoryEntriesResumeAdapter.deserializeFileEntry((File) keyObj, file); } - if (valueObj instanceof Long) { - fileOffsetResumeAdapter.deserializeFileOffset((File) keyObj, (Long) valueObj); + if (valueObj instanceof Long aLong) { + fileOffsetResumeAdapter.deserializeFileOffset((File) keyObj, aLong); } return add(OffsetKeys.of(keyObj), Offsets.of(valueObj)); diff --git a/components/camel-file/src/main/java/org/apache/camel/component/file/strategy/GenericFileProcessStrategySupport.java b/components/camel-file/src/main/java/org/apache/camel/component/file/strategy/GenericFileProcessStrategySupport.java index d5cf22e8cb3..4eeaaa2bb8e 100644 --- a/components/camel-file/src/main/java/org/apache/camel/component/file/strategy/GenericFileProcessStrategySupport.java +++ b/components/camel-file/src/main/java/org/apache/camel/component/file/strategy/GenericFileProcessStrategySupport.java @@ -165,8 +165,8 @@ public abstract class GenericFileProcessStrategySupport<T> extends ServiceSuppor @Override protected void doStart() throws Exception { - if (exclusiveReadLockStrategy instanceof CamelContextAware) { - ((CamelContextAware) exclusiveReadLockStrategy).setCamelContext(camelContext); + if (exclusiveReadLockStrategy instanceof CamelContextAware camelContextAware) { + camelContextAware.setCamelContext(camelContext); } ServiceHelper.startService(exclusiveReadLockStrategy); } diff --git a/components/camel-file/src/main/java/org/apache/camel/component/file/strategy/GenericFileRenameProcessStrategy.java b/components/camel-file/src/main/java/org/apache/camel/component/file/strategy/GenericFileRenameProcessStrategy.java index 74716a066c5..b15d271e35e 100644 --- a/components/camel-file/src/main/java/org/apache/camel/component/file/strategy/GenericFileRenameProcessStrategy.java +++ b/components/camel-file/src/main/java/org/apache/camel/component/file/strategy/GenericFileRenameProcessStrategy.java @@ -45,9 +45,8 @@ public class GenericFileRenameProcessStrategy<T> extends GenericFileProcessStrat if (beginRenamer != null) { GenericFile<T> newName = beginRenamer.renameFile(operations, exchange, file); GenericFile<T> to = renameFile(operations, file, newName); - FileEndpoint fe; - if (endpoint instanceof FileEndpoint) { - fe = (FileEndpoint) endpoint; + + if (endpoint instanceof FileEndpoint fe) { if (to != null) { to.bindToExchange(exchange, fe.isProbeContentType()); } @@ -73,9 +72,7 @@ public class GenericFileRenameProcessStrategy<T> extends GenericFileProcessStrat // create a copy and bind the file to the exchange to be used by // the renamer to evaluate the file name Exchange copy = ExchangeHelper.createCopy(exchange, true); - FileEndpoint fe; - if (endpoint instanceof FileEndpoint) { - fe = (FileEndpoint) endpoint; + if (endpoint instanceof FileEndpoint fe) { file.bindToExchange(copy, fe.isProbeContentType()); } else { file.bindToExchange(copy); @@ -106,9 +103,7 @@ public class GenericFileRenameProcessStrategy<T> extends GenericFileProcessStrat // create a copy and bind the file to the exchange to be used by // the renamer to evaluate the file name Exchange copy = ExchangeHelper.createCopy(exchange, true); - FileEndpoint fe; - if (endpoint instanceof FileEndpoint) { - fe = (FileEndpoint) endpoint; + if (endpoint instanceof FileEndpoint fe) { file.bindToExchange(copy, fe.isProbeContentType()); } else { file.bindToExchange(copy);
