♻️ Refactor the FileEntry classes
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 13 Feb 2023 09:22:14 +0000 (10:22 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 13 Feb 2023 09:22:14 +0000 (10:22 +0100)
And by that I mean “remove most of them.” The generalised FileEntry that
exposes a Map and an InputStream is absolutely adequate and removes the
instanceof check in ClientPutComplexDir.

src/main/java/net/pterodactylus/fcp/ClientPutComplexDir.java
src/main/java/net/pterodactylus/fcp/FileEntry.java
src/test/java/net/pterodactylus/fcp/ClientPutComplexDirTest.java

index 4bb503d..40a7cb8 100644 (file)
@@ -25,8 +25,6 @@ import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
 
-import net.pterodactylus.fcp.FileEntry.DirectFileEntry;
-
 /**
  * The “ClientPutComplexDir” lets you upload a directory with different sources
  * for each file.
@@ -39,8 +37,8 @@ public class ClientPutComplexDir extends FcpMessage {
        /** The index for added file entries. */
        private int fileIndex = 0;
 
-       /** The input streams from {@link DirectFileEntry}s. */
-       private final List<InputStream> directFileInputStreams = new ArrayList<InputStream>();
+       /** The input streams from {@link FileEntry}s. */
+       private final List<InputStream> directFileInputStreams = new ArrayList<>();
 
        /**
         * Creates a new “ClientPutComplexDir” with the given identifier and URI.
index 43b12aa..46940f1 100644 (file)
@@ -22,90 +22,77 @@ import java.io.InputStream;
 import java.util.HashMap;
 import java.util.Map;
 
+import static net.pterodactylus.fcp.UploadFrom.direct;
+import static net.pterodactylus.fcp.UploadFrom.disk;
+import static net.pterodactylus.fcp.UploadFrom.redirect;
+
 /**
  * Container class for file entry data.
  *
- * @see ClientPutComplexDir#addFileEntry(FileEntry)
  * @author David ‘Bombe’ Roden &lt;bombe@freenetproject.org&gt;
+ * @see ClientPutComplexDir#addFileEntry(FileEntry)
  */
-public abstract class FileEntry {
-
-       /** The name of the file. */
-       protected final String name;
-
-       /** The upload source of the file. */
-       protected final UploadFrom uploadFrom;
-
-       /**
-        * Creates a new file entry with the given name and upload source.
-        *
-        * @param name
-        *            The name of the file
-        * @param uploadFrom
-        *            The upload source of the file
-        */
-       protected FileEntry(String name, UploadFrom uploadFrom) {
-               this.name = name;
-               this.uploadFrom = uploadFrom;
-       }
+public class FileEntry {
 
        /**
         * Creates a new file entry for a file that should be transmitted to the
         * node in the payload of the message.
         *
-        * @param name
-        *            The name of the file
-        * @param contentType
-        *            The content type of the file, or <code>null</code> to let the
-        *            node auto-detect it
-        * @param length
-        *            The length of the file
-        * @param dataInputStream
-        *            The input stream of the file
+        * @param name            The name of the file
+        * @param contentType     The content type of the file, or <code>null</code> to let the
+        *                        node auto-detect it
+        * @param length          The length of the file
+        * @param dataInputStream The input stream of the file
         * @return A file entry
         */
        public static FileEntry createDirectFileEntry(String name, String contentType, long length, InputStream dataInputStream) {
-               return new DirectFileEntry(name, contentType, length, dataInputStream);
+               FileEntry directFileEntry = new FileEntry(name, direct) {
+                       @Override
+                       public InputStream getInputStream() {
+                               return dataInputStream;
+                       }
+               };
+               directFileEntry.fields.put("DataLength", String.valueOf(length));
+               if (contentType != null) {
+                       directFileEntry.fields.put("Metadata.ContentType", contentType);
+               }
+               return directFileEntry;
        }
 
        /**
         * Creates a new file entry for a file that should be uploaded from disk.
         *
-        * @param name
-        *            The name of the file
-        * @param filename
-        *            The name of the file on disk
-        * @param contentType
-        *            The content type of the file, or <code>null</code> to let the
-        *            node auto-detect it
-        * @param length
-        *            The length of the file, or <code>-1</code> to not specify a
-        *            size
+        * @param name        The name of the file
+        * @param filename    The name of the file on disk
+        * @param contentType The content type of the file, or <code>null</code> to let the
+        *                    node auto-detect it
         * @return A file entry
         */
-       public static FileEntry createDiskFileEntry(String name, String filename, String contentType, long length) {
-               return new DiskFileEntry(name, filename, contentType, length);
+       public static FileEntry createDiskFileEntry(String name, String filename, String contentType) {
+               FileEntry fileEntry = new FileEntry(name, disk);
+               fileEntry.fields.put("Filename", filename);
+               if (contentType != null) {
+                       fileEntry.fields.put("Metadata.ContentType", contentType);
+               }
+               return fileEntry;
        }
 
        /**
         * Creates a new file entry for a file that redirects to another URI.
         *
-        * @param name
-        *            The name of the file
-        * @param targetURI
-        *            The target URI of the redirect
+        * @param name      The name of the file
+        * @param targetURI The target URI of the redirect
         * @return A file entry
         */
        public static FileEntry createRedirectFileEntry(String name, String targetURI) {
-               return new RedirectFileEntry(name, targetURI);
+               FileEntry fileEntry = new FileEntry(name, redirect);
+               fileEntry.fields.put("TargetURI", targetURI);
+               return fileEntry;
        }
 
-       /**
-        * Returns the fields for this file entry.
-        *
-        * @return The fields for this file entry
-        */
-       protected abstract Map<String, String> getFields();
+       public Map<String, String> getFields() {
+               return fields;
+       }
 
        /**
         * Returns an {@link InputStream} delivering the content of this file. If
@@ -120,188 +107,16 @@ public abstract class FileEntry {
        }
 
        /**
-        * A file entry for a file that should be transmitted in the payload of the
-        * {@link ClientPutComplexDir} message.
-        *
-        * @author David ‘Bombe’ Roden &lt;bombe@freenetproject.org&gt;
-        */
-       static class DirectFileEntry extends FileEntry {
-
-               /** The content type of the data. */
-               private final String contentType;
-
-               /** The length of the data. */
-               private final long length;
-
-               /** The input stream of the data. */
-               private final InputStream inputStream;
-
-               /**
-                * Creates a new direct file entry with content type auto-detection.
-                *
-                * @param name
-                *            The name of the file
-                * @param length
-                *            The length of the file
-                * @param inputStream
-                *            The input stream of the file
-                */
-               public DirectFileEntry(String name, long length, InputStream inputStream) {
-                       this(name, null, length, inputStream);
-               }
-
-               /**
-                * Creates a new direct file entry.
-                *
-                * @param name
-                *            The name of the file
-                * @param contentType
-                *            The content type of the file, or <code>null</code> to let
-                *            the node auto-detect it
-                * @param length
-                *            The length of the file
-                * @param inputStream
-                *            The input stream of the file
-                */
-               public DirectFileEntry(String name, String contentType, long length, InputStream inputStream) {
-                       super(name, UploadFrom.direct);
-                       this.contentType = contentType;
-                       this.length = length;
-                       this.inputStream = inputStream;
-               }
-
-               /**
-                * {@inheritDoc}
-                */
-               @Override
-               protected Map<String, String> getFields() {
-                       Map<String, String> fields = new HashMap<String, String>();
-                       fields.put("Name", name);
-                       fields.put("UploadFrom", String.valueOf(uploadFrom));
-                       fields.put("DataLength", String.valueOf(length));
-                       if (contentType != null) {
-                               fields.put("Metadata.ContentType", contentType);
-                       }
-                       return fields;
-               }
-
-               /**
-                * Returns the input stream of the file.
-                *
-                * @return The input stream of the file
-                */
-               @Override
-               public InputStream getInputStream() {
-                       return inputStream;
-               }
-
-       }
-
-       /**
-        * A file entry for a file that should be uploaded from the disk.
+        * Creates a new file entry with the given name and upload source.
         *
-        * @author David ‘Bombe’ Roden &lt;bombe@freenetproject.org&gt;
+        * @param name       The name of the file
+        * @param uploadFrom The upload source of the file
         */
-       static class DiskFileEntry extends FileEntry {
-
-               /** The name of the on-disk file. */
-               private final String filename;
-
-               /** The content type of the file. */
-               private final String contentType;
-
-               /** The length of the file. */
-               private final long length;
-
-               /**
-                * Creates a new disk file entry.
-                *
-                * @param name
-                *            The name of the file
-                * @param filename
-                *            The name of the on-disk file
-                * @param length
-                *            The length of the file
-                */
-               public DiskFileEntry(String name, String filename, long length) {
-                       this(name, filename, null, length);
-               }
-
-               /**
-                * Creates a new disk file entry.
-                *
-                * @param name
-                *            The name of the file
-                * @param filename
-                *            The name of the on-disk file
-                * @param contentType
-                *            The content type of the file, or <code>null</code> to let
-                *            the node auto-detect it
-                * @param length
-                *            The length of the file
-                */
-               public DiskFileEntry(String name, String filename, String contentType, long length) {
-                       super(name, UploadFrom.disk);
-                       this.filename = filename;
-                       this.contentType = contentType;
-                       this.length = length;
-               }
-
-               /**
-                * {@inheritDoc}
-                */
-               @Override
-               protected Map<String, String> getFields() {
-                       Map<String, String> fields = new HashMap<String, String>();
-                       fields.put("Name", name);
-                       fields.put("UploadFrom", String.valueOf(uploadFrom));
-                       fields.put("Filename", filename);
-                       if (length > -1) {
-                               fields.put("DataSize", String.valueOf(length));
-                       }
-                       if (contentType != null) {
-                               fields.put("Metadata.ContentType", contentType);
-                       }
-                       return fields;
-               }
-
+       private FileEntry(String name, UploadFrom uploadFrom) {
+               fields.put("Name", name);
+               fields.put("UploadFrom", String.valueOf(uploadFrom));
        }
 
-       /**
-        * A file entry for a file that redirects to another URI.
-        *
-        * @author David ‘Bombe’ Roden &lt;bombe@freenetproject.org&gt;
-        */
-       static class RedirectFileEntry extends FileEntry {
-
-               /** The target URI of the redirect. */
-               private String targetURI;
-
-               /**
-                * Creates a new redirect file entry.
-                *
-                * @param name
-                *            The name of the file
-                * @param targetURI
-                *            The target URI of the redirect
-                */
-               public RedirectFileEntry(String name, String targetURI) {
-                       super(name, UploadFrom.redirect);
-                       this.targetURI = targetURI;
-               }
-
-               /**
-                * {@inheritDoc}
-                */
-               @Override
-               protected Map<String, String> getFields() {
-                       Map<String, String> fields = new HashMap<String, String>();
-                       fields.put("Name", name);
-                       fields.put("UploadFrom", String.valueOf(uploadFrom));
-                       fields.put("TargetURI", targetURI);
-                       return fields;
-               }
-
-       }
+       private final Map<String, String> fields = new HashMap<>();
 
 }
index 63ade6f..c5d9c61 100644 (file)
@@ -134,7 +134,7 @@ public class ClientPutComplexDirTest {
 
        @Test
        public void diskFileEntryIsAddedCorrectly() {
-               clientPutComplexDir.addFileEntry(FileEntry.createDiskFileEntry("file1.dat", "/file/name", "text/plain1", 10));
+               clientPutComplexDir.addFileEntry(FileEntry.createDiskFileEntry("file1.dat", "/file/name", "text/plain1"));
                assertThat(clientPutComplexDir, isDataMessage("ClientPutComplexDir", allOf(
                                hasItem("Files.0.Name=file1.dat"),
                                hasItem("Files.0.UploadFrom=disk"),