From 6f03fb87a6d4db3b5e267720f8eb1b0f2fdd06d3 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Mon, 13 Feb 2023 10:22:14 +0100 Subject: [PATCH] =?utf8?q?=E2=99=BB=EF=B8=8F=20Refactor=20the=20FileEntry?= =?utf8?q?=20classes?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 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. --- .../net/pterodactylus/fcp/ClientPutComplexDir.java | 6 +- src/main/java/net/pterodactylus/fcp/FileEntry.java | 281 ++++----------------- .../pterodactylus/fcp/ClientPutComplexDirTest.java | 2 +- 3 files changed, 51 insertions(+), 238 deletions(-) diff --git a/src/main/java/net/pterodactylus/fcp/ClientPutComplexDir.java b/src/main/java/net/pterodactylus/fcp/ClientPutComplexDir.java index 4bb503d..40a7cb8 100644 --- a/src/main/java/net/pterodactylus/fcp/ClientPutComplexDir.java +++ b/src/main/java/net/pterodactylus/fcp/ClientPutComplexDir.java @@ -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 directFileInputStreams = new ArrayList(); + /** The input streams from {@link FileEntry}s. */ + private final List directFileInputStreams = new ArrayList<>(); /** * Creates a new “ClientPutComplexDir” with the given identifier and URI. diff --git a/src/main/java/net/pterodactylus/fcp/FileEntry.java b/src/main/java/net/pterodactylus/fcp/FileEntry.java index 43b12aa..46940f1 100644 --- a/src/main/java/net/pterodactylus/fcp/FileEntry.java +++ b/src/main/java/net/pterodactylus/fcp/FileEntry.java @@ -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 <bombe@freenetproject.org> + * @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 null 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 null 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 null to let the - * node auto-detect it - * @param length - * The length of the file, or -1 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 null 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 getFields(); + public Map 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 <bombe@freenetproject.org> - */ - 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 null 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 getFields() { - Map fields = new HashMap(); - 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 <bombe@freenetproject.org> + * @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 null 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 getFields() { - Map fields = new HashMap(); - 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 <bombe@freenetproject.org> - */ - 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 getFields() { - Map fields = new HashMap(); - fields.put("Name", name); - fields.put("UploadFrom", String.valueOf(uploadFrom)); - fields.put("TargetURI", targetURI); - return fields; - } - - } + private final Map fields = new HashMap<>(); } diff --git a/src/test/java/net/pterodactylus/fcp/ClientPutComplexDirTest.java b/src/test/java/net/pterodactylus/fcp/ClientPutComplexDirTest.java index 63ade6f..c5d9c61 100644 --- a/src/test/java/net/pterodactylus/fcp/ClientPutComplexDirTest.java +++ b/src/test/java/net/pterodactylus/fcp/ClientPutComplexDirTest.java @@ -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"), -- 2.7.4