From 69ad81fb9ebc7e1ed3271c836954be94af706f27 Mon Sep 17 00:00:00 2001 From: retgal Date: Fri, 29 Dec 2023 00:11:52 +0100 Subject: [PATCH] Cleanup --- .../mpo/dayon/assistant/gui/Assistant.java | 2 +- .../dayon/common/gui/common/BaseFrame.java | 2 +- .../message/NetworkClipboardFilesMessage.java | 3 +- .../network/message/NetworkMessage.java | 83 +++++++++---------- .../dayon/common/utils/SystemUtilities.java | 9 +- .../network/NetworkAssistantEngineTest.java | 4 +- .../assistant/utils/NetworkUtilitiesTest.java | 2 +- ...RobotNetworkControlMessageHandlerTest.java | 4 +- .../network/NetworkAssistedEngineTest.java | 4 +- .../common/network/NetworkSenderTest.java | 2 +- 10 files changed, 56 insertions(+), 59 deletions(-) diff --git a/src/main/java/mpo/dayon/assistant/gui/Assistant.java b/src/main/java/mpo/dayon/assistant/gui/Assistant.java index 15e0f37e..a1de67ae 100644 --- a/src/main/java/mpo/dayon/assistant/gui/Assistant.java +++ b/src/main/java/mpo/dayon/assistant/gui/Assistant.java @@ -275,7 +275,7 @@ private JMenuItem getJMenuItemHelp() { if (desktop.isSupported(Desktop.Action.BROWSE)) { desktop.browse(getQuickStartURI(QUICKSTART_PAGE, ASSISTANT.getPrefix())); } else if (isFlat()) { - new ProcessBuilder(FLATPACK_BROWSER, getQuickStartURI(QUICKSTART_PAGE, ASSISTANT.getPrefix()).toString()).start(); + new ProcessBuilder(FLATPAK_BROWSER, getQuickStartURI(QUICKSTART_PAGE, ASSISTANT.getPrefix()).toString()).start(); } } } catch (IOException ex) { diff --git a/src/main/java/mpo/dayon/common/gui/common/BaseFrame.java b/src/main/java/mpo/dayon/common/gui/common/BaseFrame.java index 1fbe3fb2..0b2868eb 100644 --- a/src/main/java/mpo/dayon/common/gui/common/BaseFrame.java +++ b/src/main/java/mpo/dayon/common/gui/common/BaseFrame.java @@ -247,7 +247,7 @@ private static void browse(URI uri) { if (desktop.isSupported(Desktop.Action.BROWSE)) { desktop.browse(uri); } else if (isFlat()) { - new ProcessBuilder(FLATPACK_BROWSER, uri.toString()).start(); + new ProcessBuilder(FLATPAK_BROWSER, uri.toString()).start(); } } } catch (IOException ex) { diff --git a/src/main/java/mpo/dayon/common/network/message/NetworkClipboardFilesMessage.java b/src/main/java/mpo/dayon/common/network/message/NetworkClipboardFilesMessage.java index 6d71ae23..8d2ee27f 100644 --- a/src/main/java/mpo/dayon/common/network/message/NetworkClipboardFilesMessage.java +++ b/src/main/java/mpo/dayon/common/network/message/NetworkClipboardFilesMessage.java @@ -89,8 +89,7 @@ private List getMetaData(List files, String basePath) { private void extractFileMetaData(File node, List fileMetaDatas, String basePath) { if (node.isFile()) { fileMetaDatas.add(new FileMetaData(node.getPath(), node.length(), basePath)); - } - if (node.isDirectory()) { + } else if (node.isDirectory()) { Arrays.stream(Objects.requireNonNull(node.listFiles())).parallel().forEachOrdered(file -> extractFileMetaData(file, fileMetaDatas, basePath)); } } diff --git a/src/main/java/mpo/dayon/common/network/message/NetworkMessage.java b/src/main/java/mpo/dayon/common/network/message/NetworkMessage.java index 629eb4fb..966223f2 100644 --- a/src/main/java/mpo/dayon/common/network/message/NetworkMessage.java +++ b/src/main/java/mpo/dayon/common/network/message/NetworkMessage.java @@ -1,48 +1,47 @@ package mpo.dayon.common.network.message; import java.io.*; +import java.util.Arrays; public abstract class NetworkMessage { - private static final byte MAGIC_NUMBER = (byte) 170; - - NetworkMessage() { - } - - public abstract NetworkMessageType getType(); - - /** - * Take into account some extra-info sent over the network with the actual - * payload ... - */ - public abstract int getWireSize(); - - public abstract void marshall(ObjectOutputStream out) throws IOException; - - public static void marshallMagicNumber(ObjectOutputStream out) throws IOException { - out.writeByte(NetworkMessage.MAGIC_NUMBER); - } - - public static void unmarshallMagicNumber(ObjectInputStream in) throws IOException { - if (NetworkMessage.MAGIC_NUMBER != in.readByte()) { - throw new IOException("Protocol error!"); - } - } - - static > void marshallEnum(ObjectOutputStream out, Enum value) throws IOException { - out.write(value.ordinal()); - } - - public static > T unmarshallEnum(ObjectInputStream in, Class enumClass) throws IOException { - final byte ordinal = in.readByte(); - final T[] xenums = enumClass.getEnumConstants(); - for (final T xenum : xenums) { - if (xenum.ordinal() == ordinal) { - return xenum; - } - } - throw new IllegalArgumentException("Unknown " + enumClass.getSimpleName() + " [" + ordinal + "] enum!"); - } - - @Override - public abstract String toString(); + private static final byte MAGIC_NUMBER = (byte) 170; + + NetworkMessage() { + } + + public abstract NetworkMessageType getType(); + + /** + * Take into account some extra-info sent over the network with the actual + * payload ... + */ + public abstract int getWireSize(); + + public abstract void marshall(ObjectOutputStream out) throws IOException; + + public static void marshallMagicNumber(ObjectOutputStream out) throws IOException { + out.writeByte(NetworkMessage.MAGIC_NUMBER); + } + + public static void unmarshallMagicNumber(ObjectInputStream in) throws IOException { + if (NetworkMessage.MAGIC_NUMBER != in.readByte()) { + throw new IOException("Protocol error!"); + } + } + + static > void marshallEnum(ObjectOutputStream out, Enum value) throws IOException { + out.write(value.ordinal()); + } + + public static > T unmarshallEnum(ObjectInputStream in, Class enumClass) throws IOException { + final byte ordinal = in.readByte(); + final T[] xenums = enumClass.getEnumConstants(); + return Arrays.stream(xenums) + .filter(xenum -> xenum.ordinal() == ordinal) + .findFirst() + .orElseThrow(() -> new IllegalArgumentException("Unknown " + enumClass.getSimpleName() + " [" + ordinal + "] enum!")); + } + + @Override + public abstract String toString(); } diff --git a/src/main/java/mpo/dayon/common/utils/SystemUtilities.java b/src/main/java/mpo/dayon/common/utils/SystemUtilities.java index d20499ce..975fc987 100644 --- a/src/main/java/mpo/dayon/common/utils/SystemUtilities.java +++ b/src/main/java/mpo/dayon/common/utils/SystemUtilities.java @@ -6,7 +6,6 @@ import java.io.*; import java.math.BigInteger; import java.net.*; -import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; @@ -18,11 +17,12 @@ import static java.lang.String.format; import static java.lang.System.getProperty; +import static java.nio.charset.StandardCharsets.UTF_8; public final class SystemUtilities { public static final String JAVA_CLASS_PATH = "java.class.path"; - public static final String FLATPACK_BROWSER = "/app/bin/dayon.browser"; + public static final String FLATPAK_BROWSER = "/app/bin/dayon.browser"; private static final String JAVA_VENDOR = "java.vendor"; private static final String TOKEN_SERVER_URL = "https://fensterkitt.ch/dayon/?token=%s"; @@ -87,7 +87,7 @@ public static List getSystemProperties() { } public static String getSystemPropertiesEx() { - return getSystemProperties().stream().map(line -> line + getProperty("line.separator")).collect(Collectors.joining()); + return getSystemProperties().stream().map(line -> line + System.lineSeparator()).collect(Collectors.joining()); } public static String getRamInfo() { @@ -209,7 +209,6 @@ public static String resolveToken(String token) throws IOException { conn.setInstanceFollowRedirects(false); conn.setReadTimeout(3000); conn.disconnect(); - return new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8)) - .readLine().trim(); + return new BufferedReader(new InputStreamReader(conn.getInputStream(), UTF_8)).readLine().trim(); } } diff --git a/src/test/java/mpo/dayon/assistant/network/NetworkAssistantEngineTest.java b/src/test/java/mpo/dayon/assistant/network/NetworkAssistantEngineTest.java index 6f15ddae..72309474 100644 --- a/src/test/java/mpo/dayon/assistant/network/NetworkAssistantEngineTest.java +++ b/src/test/java/mpo/dayon/assistant/network/NetworkAssistantEngineTest.java @@ -7,8 +7,8 @@ class NetworkAssistantEngineTest { - NetworkAssistantEngine engine; - NetworkAssistantEngineListener listener; + private NetworkAssistantEngine engine; + private NetworkAssistantEngineListener listener; @BeforeEach void init() { diff --git a/src/test/java/mpo/dayon/assistant/utils/NetworkUtilitiesTest.java b/src/test/java/mpo/dayon/assistant/utils/NetworkUtilitiesTest.java index 764059cb..47ee1207 100644 --- a/src/test/java/mpo/dayon/assistant/utils/NetworkUtilitiesTest.java +++ b/src/test/java/mpo/dayon/assistant/utils/NetworkUtilitiesTest.java @@ -18,6 +18,6 @@ void getInetAddresses() { // then assertFalse(inetAddresses.isEmpty()); - assertEquals(loopBack, inetAddresses.get(inetAddresses.size()-1).toString()); + assertEquals(loopBack, inetAddresses.get(inetAddresses.size()-1)); } } \ No newline at end of file diff --git a/src/test/java/mpo/dayon/assisted/control/RobotNetworkControlMessageHandlerTest.java b/src/test/java/mpo/dayon/assisted/control/RobotNetworkControlMessageHandlerTest.java index d0bcec0c..ce265734 100644 --- a/src/test/java/mpo/dayon/assisted/control/RobotNetworkControlMessageHandlerTest.java +++ b/src/test/java/mpo/dayon/assisted/control/RobotNetworkControlMessageHandlerTest.java @@ -15,8 +15,8 @@ class RobotNetworkControlMessageHandlerTest { - Robot robot = mock(Robot.class); - RobotNetworkControlMessageHandler controlMessageHandler = new RobotNetworkControlMessageHandler(robot); + private final Robot robot = mock(Robot.class); + private final RobotNetworkControlMessageHandler controlMessageHandler = new RobotNetworkControlMessageHandler(robot); @Test void testHandleMessagePress0() { diff --git a/src/test/java/mpo/dayon/assisted/network/NetworkAssistedEngineTest.java b/src/test/java/mpo/dayon/assisted/network/NetworkAssistedEngineTest.java index 01e0d975..6ff270a3 100644 --- a/src/test/java/mpo/dayon/assisted/network/NetworkAssistedEngineTest.java +++ b/src/test/java/mpo/dayon/assisted/network/NetworkAssistedEngineTest.java @@ -8,8 +8,8 @@ class NetworkAssistedEngineTest { - NetworkAssistedEngine engine; - NetworkAssistedEngineListener listener; + private NetworkAssistedEngine engine; + private NetworkAssistedEngineListener listener; @BeforeEach void init() { diff --git a/src/test/java/mpo/dayon/common/network/NetworkSenderTest.java b/src/test/java/mpo/dayon/common/network/NetworkSenderTest.java index 3f27a587..048b2091 100644 --- a/src/test/java/mpo/dayon/common/network/NetworkSenderTest.java +++ b/src/test/java/mpo/dayon/common/network/NetworkSenderTest.java @@ -34,7 +34,7 @@ class NetworkSenderTest { private NetworkSender sender; @BeforeEach - void init() throws IOException { + void init() { outMock = Mockito.mock(ObjectOutputStream.class); valueCaptor = ArgumentCaptor.forClass(int.class); sender = new NetworkSender(outMock);