Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
RetGal committed Dec 28, 2023
1 parent 9b612ba commit 69ad81f
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 59 deletions.
2 changes: 1 addition & 1 deletion src/main/java/mpo/dayon/assistant/gui/Assistant.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/mpo/dayon/common/gui/common/BaseFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,7 @@ private List<FileMetaData> getMetaData(List<File> files, String basePath) {
private void extractFileMetaData(File node, List<FileMetaData> 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));
}
}
Expand Down
83 changes: 41 additions & 42 deletions src/main/java/mpo/dayon/common/network/message/NetworkMessage.java
Original file line number Diff line number Diff line change
@@ -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 <T extends Enum<T>> void marshallEnum(ObjectOutputStream out, Enum<T> value) throws IOException {
out.write(value.ordinal());
}

public static <T extends Enum<T>> T unmarshallEnum(ObjectInputStream in, Class<T> 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 <T extends Enum<T>> void marshallEnum(ObjectOutputStream out, Enum<T> value) throws IOException {
out.write(value.ordinal());
}

public static <T extends Enum<T>> T unmarshallEnum(ObjectInputStream in, Class<T> 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();
}
9 changes: 4 additions & 5 deletions src/main/java/mpo/dayon/common/utils/SystemUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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";

Expand Down Expand Up @@ -87,7 +87,7 @@ public static List<String> 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() {
Expand Down Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

class NetworkAssistantEngineTest {

NetworkAssistantEngine engine;
NetworkAssistantEngineListener listener;
private NetworkAssistantEngine engine;
private NetworkAssistantEngineListener listener;

@BeforeEach
void init() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

class NetworkAssistedEngineTest {

NetworkAssistedEngine engine;
NetworkAssistedEngineListener listener;
private NetworkAssistedEngine engine;
private NetworkAssistedEngineListener listener;

@BeforeEach
void init() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 69ad81f

Please sign in to comment.