You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by or...@apache.org on 2023/04/11 08:34:02 UTC

[camel] 02/02: (chores) camel-box: cleanup of duplicated code for checking parameters

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 8247b19d7f6c08cd528bf76dfb7d3b424a3c0281
Author: Otavio Rodolfo Piske <an...@gmail.com>
AuthorDate: Tue Apr 11 09:35:49 2023 +0200

    (chores) camel-box: cleanup of duplicated code for checking parameters
---
 .../camel/component/box/api/BoxFilesManager.java   | 183 ++++++---------------
 1 file changed, 51 insertions(+), 132 deletions(-)

diff --git a/components/camel-box/camel-box-api/src/main/java/org/apache/camel/component/box/api/BoxFilesManager.java b/components/camel-box/camel-box-api/src/main/java/org/apache/camel/component/box/api/BoxFilesManager.java
index cc188daad22..535a45c1abf 100644
--- a/components/camel-box/camel-box-api/src/main/java/org/apache/camel/component/box/api/BoxFilesManager.java
+++ b/components/camel-box/camel-box-api/src/main/java/org/apache/camel/component/box/api/BoxFilesManager.java
@@ -61,6 +61,12 @@ public class BoxFilesManager {
         this.boxConnection = boxConnection;
     }
 
+    private static <T> void notNull(T value, String name) {
+        if (value == null) {
+            throw new IllegalArgumentException("Parameter '" + name + "' cannot be null");
+        }
+    }
+
     /**
      * Get file information.
      *
@@ -71,9 +77,7 @@ public class BoxFilesManager {
     public BoxFile.Info getFileInfo(String fileId, String... fields) {
         try {
             LOG.debug("Getting info for file(id={})", fileId);
-            if (fileId == null) {
-                throw new IllegalArgumentException("Parameter 'fileId' can not be null");
-            }
+            notNull(fileId, "fileId");
 
             BoxFile file = new BoxFile(boxConnection, fileId);
 
@@ -97,12 +101,9 @@ public class BoxFilesManager {
     public BoxFile updateFileInfo(String fileId, BoxFile.Info info) {
         try {
             LOG.debug("Updating info for file(id={})", fileId);
-            if (fileId == null) {
-                throw new IllegalArgumentException("Parameter 'fileId' can not be null");
-            }
-            if (info == null) {
-                throw new IllegalArgumentException("Parameter 'info' can not be null");
-            }
+            notNull(fileId, "fileId");
+            notNull(info, "info");
+
             BoxFile file = new BoxFile(boxConnection, fileId);
             file.updateInfo(info);
             return file;
@@ -130,15 +131,9 @@ public class BoxFilesManager {
             Long size, Boolean check, ProgressListener listener) {
         try {
             LOG.debug("Uploading file with name '{}}' to parent_folder(id={}})", fileName, parentFolderId);
-            if (parentFolderId == null) {
-                throw new IllegalArgumentException("Parameter 'parentFolderId' can not be null");
-            }
-            if (content == null) {
-                throw new IllegalArgumentException("Parameter 'content' can not be null");
-            }
-            if (fileName == null) {
-                throw new IllegalArgumentException("Parameter 'fileName' can not be null");
-            }
+            notNull(parentFolderId, "parentFolderId");
+            notNull(content, "content");
+            notNull(fileName, "fileName");
             BoxFile boxFile = null;
             boolean uploadNewFile = true;
             if (check != null && check) {
@@ -238,12 +233,8 @@ public class BoxFilesManager {
             ProgressListener listener) {
         try {
             LOG.debug("Uploading new version of file(id={})", fileId);
-            if (fileId == null) {
-                throw new IllegalArgumentException("Parameter 'fileId' can not be null");
-            }
-            if (fileContent == null) {
-                throw new IllegalArgumentException("Parameter 'fileContent' can not be null");
-            }
+            notNull(fileId, "fileId");
+            notNull(fileContent, "fileContent");
 
             BoxFile file = new BoxFile(boxConnection, fileId);
 
@@ -272,9 +263,7 @@ public class BoxFilesManager {
     public Collection<BoxFileVersion> getFileVersions(String fileId) {
         try {
             LOG.debug("Getting versions of file(id={})", fileId);
-            if (fileId == null) {
-                throw new IllegalArgumentException("Parameter 'fileId' can not be null");
-            }
+            notNull(fileId, "fileId");
 
             BoxFile file = new BoxFile(boxConnection, fileId);
 
@@ -308,12 +297,8 @@ public class BoxFilesManager {
             ProgressListener listener) {
         try {
             LOG.debug("Downloading file(id={})", fileId);
-            if (fileId == null) {
-                throw new IllegalArgumentException("Parameter 'fileId' can not be null");
-            }
-            if (output == null) {
-                throw new IllegalArgumentException("Parameter 'output' can not be null");
-            }
+            notNull(fileId, "fileId");
+            notNull(output, "output");
             BoxFile file = new BoxFile(boxConnection, fileId);
 
             if (listener != null) {
@@ -351,15 +336,9 @@ public class BoxFilesManager {
             ProgressListener listener) {
         try {
             LOG.debug("Downloading file(id={}, version={})", fileId, version);
-            if (fileId == null) {
-                throw new IllegalArgumentException("Parameter 'fileId' can not be null");
-            }
-            if (version == null) {
-                throw new IllegalArgumentException("Parameter 'version' can not be null");
-            }
-            if (output == null) {
-                throw new IllegalArgumentException("Parameter 'output' can not be null");
-            }
+            notNull(fileId, "fileId");
+            notNull(version, "version");
+            notNull(output, "output");
             BoxFile file = new BoxFile(boxConnection, fileId);
 
             List<BoxFileVersion> fileVersions = (List<BoxFileVersion>) file.getVersions();
@@ -387,12 +366,8 @@ public class BoxFilesManager {
     public BoxFileVersion promoteFileVersion(String fileId, Integer version) {
         try {
             LOG.debug("Promoting file(id={}, version={})", fileId, version);
-            if (fileId == null) {
-                throw new IllegalArgumentException("Parameter 'fileId' can not be null");
-            }
-            if (version == null) {
-                throw new IllegalArgumentException("Parameter 'version' can not be null");
-            }
+            notNull(fileId, "fileId");
+            notNull(version, "version");
             BoxFile file = new BoxFile(boxConnection, fileId);
 
             List<BoxFileVersion> fileVersions = (List<BoxFileVersion>) file.getVersions();
@@ -419,12 +394,8 @@ public class BoxFilesManager {
             LOG.debug("Copying file(id={}) to destination_folder(id={}) {}",
                     fileId, destinationFolderId,
                     newName == null ? "" : " with new name '" + newName + "'");
-            if (fileId == null) {
-                throw new IllegalArgumentException("Parameter 'fileId' can not be null");
-            }
-            if (destinationFolderId == null) {
-                throw new IllegalArgumentException("Parameter 'destinationFolderId' can not be null");
-            }
+            notNull(fileId, "fileId");
+            notNull(destinationFolderId, "version");
             BoxFile fileToCopy = new BoxFile(boxConnection, fileId);
             BoxFolder destinationFolder = new BoxFolder(boxConnection, destinationFolderId);
             if (newName == null) {
@@ -451,12 +422,8 @@ public class BoxFilesManager {
             LOG.debug("Moving file(id={}) to destination_folder(id={}) {}",
                     fileId, destinationFolderId,
                     newName == null ? "" : " with new name '" + newName + "'");
-            if (fileId == null) {
-                throw new IllegalArgumentException("Parameter 'fileId' can not be null");
-            }
-            if (destinationFolderId == null) {
-                throw new IllegalArgumentException("Parameter 'destinationFolderId' can not be null");
-            }
+            notNull(fileId, "fileId");
+            notNull(destinationFolderId, "version");
             BoxFile fileToMove = new BoxFile(boxConnection, fileId);
             BoxFolder destinationFolder = new BoxFolder(boxConnection, destinationFolderId);
             if (newName == null) {
@@ -479,12 +446,8 @@ public class BoxFilesManager {
     public BoxFile renameFile(String fileId, String newFileName) {
         try {
             LOG.debug("Renaming file(id={}) to '{}'", fileId, newFileName);
-            if (fileId == null) {
-                throw new IllegalArgumentException("Parameter 'fileId' can not be null");
-            }
-            if (newFileName == null) {
-                throw new IllegalArgumentException("Parameter 'newName' can not be null");
-            }
+            notNull(fileId, "fileId");
+            notNull(newFileName, "version");
             BoxFile fileToRename = new BoxFile(boxConnection, fileId);
             fileToRename.rename(newFileName);
             return fileToRename;
@@ -501,9 +464,7 @@ public class BoxFilesManager {
     public void deleteFile(String fileId) {
         try {
             LOG.debug("Deleting file(id={})", fileId);
-            if (fileId == null) {
-                throw new IllegalArgumentException("Parameter 'fileId' can not be null");
-            }
+            notNull(fileId, "fileId");
             BoxFile file = new BoxFile(boxConnection, fileId);
             file.delete();
         } catch (BoxAPIException e) {
@@ -521,12 +482,8 @@ public class BoxFilesManager {
     public void deleteFileVersion(String fileId, Integer version) {
         try {
             LOG.debug("Deleting file(id={}, version={})", fileId, version);
-            if (fileId == null) {
-                throw new IllegalArgumentException("Parameter 'fileId' can not be null");
-            }
-            if (version == null) {
-                throw new IllegalArgumentException("Parameter 'version' can not be null");
-            }
+            notNull(fileId, "fileId");
+            notNull(version, "version");
 
             BoxFile file = new BoxFile(boxConnection, fileId);
             List<BoxFileVersion> versions = (List<BoxFileVersion>) file.getVersions();
@@ -559,12 +516,8 @@ public class BoxFilesManager {
                             : " unsharedDate=" + DateFormat.getDateTimeInstance().format(unshareDate)
                               + " permissions=" + permissions);
 
-            if (fileId == null) {
-                throw new IllegalArgumentException("Parameter 'fileId' can not be null");
-            }
-            if (access == null) {
-                throw new IllegalArgumentException("Parameter 'access' can not be null");
-            }
+            notNull(fileId, "fileId");
+            notNull(access, "access");
 
             BoxFile file = new BoxFile(boxConnection, fileId);
             return file.createSharedLink(access, unshareDate, permissions);
@@ -583,9 +536,7 @@ public class BoxFilesManager {
     public URL getDownloadURL(String fileId) {
         try {
             LOG.debug("Getting download URL for file(id={})", fileId);
-            if (fileId == null) {
-                throw new IllegalArgumentException("Parameter 'fileId' can not be null");
-            }
+            notNull(fileId, "fileId");
 
             BoxFile file = new BoxFile(boxConnection, fileId);
 
@@ -607,9 +558,7 @@ public class BoxFilesManager {
         try {
             LOG.debug("Getting preview link for file(id={})", fileId);
 
-            if (fileId == null) {
-                throw new IllegalArgumentException("Parameter 'fileId' can not be null");
-            }
+            notNull(fileId, "fileId");
 
             BoxFile file = new BoxFile(boxConnection, fileId);
             return file.getPreviewLink();
@@ -638,24 +587,12 @@ public class BoxFilesManager {
             LOG.debug("Getting thumbnail for file(id={}) fileType={} minWidth={} minHeight={} maxWidth={} maxHeight={}",
                     fileId, fileType, minWidth, minHeight, maxWidth, maxHeight);
 
-            if (fileId == null) {
-                throw new IllegalArgumentException("Parameter 'fileId' can not be null");
-            }
-            if (fileType == null) {
-                throw new IllegalArgumentException("Parameter 'fileType' can not be null");
-            }
-            if (minWidth == null) {
-                throw new IllegalArgumentException("Parameter 'minWidth' can not be null");
-            }
-            if (minHeight == null) {
-                throw new IllegalArgumentException("Parameter 'minHeight' can not be null");
-            }
-            if (maxWidth == null) {
-                throw new IllegalArgumentException("Parameter 'maxWidth' can not be null");
-            }
-            if (maxHeight == null) {
-                throw new IllegalArgumentException("Parameter 'maxHeight' can not be null");
-            }
+            notNull(fileId, "fileId");
+            notNull(fileType, "fileType");
+            notNull(minWidth, "minWidth");
+            notNull(minHeight, "minHeight");
+            notNull(maxWidth, "maxWidth");
+            notNull(maxHeight, "maxHeight");
 
             BoxFile file = new BoxFile(boxConnection, fileId);
             return file.getThumbnail(fileType, minWidth, minHeight, maxWidth, maxHeight);
@@ -677,12 +614,8 @@ public class BoxFilesManager {
         try {
             LOG.debug("Creating metadata for file(id={})", fileId);
 
-            if (fileId == null) {
-                throw new IllegalArgumentException("Parameter 'fileId' can not be null");
-            }
-            if (metadata == null) {
-                throw new IllegalArgumentException("Parameter 'metadata' can not be null");
-            }
+            notNull(fileId, "fileId");
+            notNull(metadata, "metadata");
 
             BoxFile file = new BoxFile(boxConnection, fileId);
 
@@ -708,9 +641,7 @@ public class BoxFilesManager {
         try {
             LOG.debug("Get metadata for file(id={})", fileId);
 
-            if (fileId == null) {
-                throw new IllegalArgumentException("Parameter 'fileId' can not be null");
-            }
+            notNull(fileId, "fileId");
 
             BoxFile file = new BoxFile(boxConnection, fileId);
 
@@ -735,12 +666,8 @@ public class BoxFilesManager {
     public Metadata updateFileMetadata(String fileId, Metadata metadata) {
         try {
             LOG.debug("Updating metadata for file(id={})", fileId);
-            if (fileId == null) {
-                throw new IllegalArgumentException("Parameter 'fileId' can not be null");
-            }
-            if (metadata == null) {
-                throw new IllegalArgumentException("Parameter 'metadata' can not be null");
-            }
+            notNull(fileId, "fileId");
+            notNull(metadata, "metadata");
             BoxFile file = new BoxFile(boxConnection, fileId);
             return file.updateMetadata(metadata);
         } catch (BoxAPIException e) {
@@ -756,9 +683,7 @@ public class BoxFilesManager {
     public void deleteFileMetadata(String fileId) {
         try {
             LOG.debug("Deleting metadata for file(id={})", fileId);
-            if (fileId == null) {
-                throw new IllegalArgumentException("Parameter 'fileId' can not be null");
-            }
+            notNull(fileId, "fileId");
             BoxFile file = new BoxFile(boxConnection, fileId);
             file.deleteMetadata();
         } catch (BoxAPIException e) {
@@ -778,15 +703,9 @@ public class BoxFilesManager {
     public void checkUpload(String fileName, String parentFolderId, Long size) {
         try {
             LOG.debug("Preflight check file with name '{}' to parent_folder(id={})", fileName, parentFolderId);
-            if (parentFolderId == null) {
-                throw new IllegalArgumentException("Parameter 'parentFolderId' can not be null");
-            }
-            if (fileName == null) {
-                throw new IllegalArgumentException("Parameter 'fileName' can not be null");
-            }
-            if (size == null) {
-                throw new IllegalArgumentException("Parameter 'size' can not be null");
-            }
+            notNull(parentFolderId, "parentFolderId");
+            notNull(fileName, "fileName");
+            notNull(size, "size");
 
             BoxFolder parentFolder = new BoxFolder(boxConnection, parentFolderId);
             parentFolder.canUpload(fileName, size);