Skip to content
This repository was archived by the owner on Nov 26, 2019. It is now read-only.

Commit dbfbc21

Browse files
committed
# Replace usage of JUnit#ThrowingSupplier with own implementation
1 parent 9a0c120 commit dbfbc21

File tree

4 files changed

+34
-9
lines changed

4 files changed

+34
-9
lines changed

client/src/main/java/com/msc/serverbrowser/util/basic/OptionalUtility.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
import java.util.Optional;
55
import java.util.function.Supplier;
66

7-
import org.junit.jupiter.api.function.ThrowingSupplier;
8-
97
/**
108
* Contains methods for working with {@link Optional}.
119
*
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.msc.serverbrowser.util.basic;
2+
3+
import java.util.function.Supplier;
4+
5+
/**
6+
* Serves the same purpose as Javas {@link Supplier}, but it its {@link #get()} function may always
7+
* throw a {@link Throwable}.
8+
*
9+
* @author Marcel
10+
* @since 01.03.2018
11+
* @param <T> Type of return value
12+
*/
13+
@FunctionalInterface
14+
public interface ThrowingSupplier<T> {
15+
16+
/**
17+
* @return the return value of Type T returned by the given implementatiom
18+
* @throws Throwable Any exceptions which might be thrown by the implementation
19+
*/
20+
T get() throws Throwable;
21+
}

client/src/main/java/com/msc/serverbrowser/util/samp/GTAController.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public final class GTAController {
4242
/**
4343
* Holds the users username.
4444
*/
45-
public static StringProperty usernameProperty = new SimpleStringProperty(retrieveUsernameFromRegistry());
45+
public static StringProperty usernameProperty = new SimpleStringProperty(retrieveUsernameFromRegistry().orElse(""));
4646

4747
private GTAController() {
4848
// Constructor to prevent instantiation
@@ -53,11 +53,17 @@ private GTAController() {
5353
*/
5454
public static void applyUsername() {
5555
if (!OSUtility.isWindows()) {
56+
// TODO Localize
57+
final Alert alert = new Alert(AlertType.ERROR, "The feature you are trying to use is not available on your operating system.", ButtonType.OK);
58+
Client.insertAlertOwner(alert);
59+
alert.setTitle("Apply username");
60+
alert.showAndWait();
5661
return;
5762
}
5863

5964
killSAMP();
60-
PastUsernames.addPastUsername(retrieveUsernameFromRegistry());
65+
retrieveUsernameFromRegistry().ifPresent(PastUsernames::addPastUsername);
66+
6167
try {
6268
WindowsRegistry.getInstance().writeStringValue(HKey.HKCU, "SOFTWARE\\SAMP", "PlayerName", usernameProperty.get());
6369
}
@@ -73,17 +79,17 @@ public static void applyUsername() {
7379
*
7480
* @return Username or "404 name not found"
7581
*/
76-
static String retrieveUsernameFromRegistry() {
82+
protected static Optional<String> retrieveUsernameFromRegistry() {
7783
if (!OSUtility.isWindows()) {
78-
return "You are on Linux ;D";
84+
return Optional.empty();
7985
}
8086

8187
try {
82-
return WindowsRegistry.getInstance().readString(HKey.HKCU, "SOFTWARE\\SAMP", "PlayerName");
88+
return Optional.ofNullable(WindowsRegistry.getInstance().readString(HKey.HKCU, "SOFTWARE\\SAMP", "PlayerName"));
8389
}
8490
catch (final RegistryException exception) {
8591
Logging.warn("Couldn't retrieve Username from registry.", exception);
86-
return "404 Name not found";
92+
return Optional.empty();
8793
}
8894
}
8995

client/src/main/java/com/msc/serverbrowser/util/samp/SAMPLauncher.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ private static List<String> buildLaunchingArguments(final String address, final
123123
arguments.add("-n");
124124

125125
// TODO Solve better
126-
arguments.add(Optional.ofNullable(GTAController.retrieveUsernameFromRegistry()).orElse("CHOOSE_NAME"));
126+
arguments.add(GTAController.retrieveUsernameFromRegistry().orElse("CHOOSE_NAME"));
127127

128128
passwordOptional.ifPresent(password -> {
129129
if (!password.isEmpty()) {

0 commit comments

Comments
 (0)