Get rid of static methods by introducing custom SystemUtils.java class

MYC-Refactor
Hammy 3 years ago
parent 98fa50142c
commit b9bd99b79b

@ -5,16 +5,16 @@ import java.io.File;
import java.util.List; import java.util.List;
import me.goudham.event.ClipboardEvent; import me.goudham.event.ClipboardEvent;
import me.goudham.exception.UnsupportedSystemException; import me.goudham.exception.UnsupportedSystemException;
import org.apache.commons.lang3.SystemUtils;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
/** /**
* Entry Class for User to interact with the System Clipboard * Entry Class for User to interact with the System Clipboard
* * <p>
* The abstract class {@link ClipboardListener} is responsible for handling all operations * The abstract class {@link ClipboardListener} is responsible for handling all operations
*/ */
public class MyClipboard { public class MyClipboard {
private final @NotNull ClipboardListener clipboardListener; private @NotNull ClipboardListener clipboardListener;
private static SystemUtils systemUtils = new SystemUtils();
/** /**
* Creates an instance of {@link MyClipboard} * Creates an instance of {@link MyClipboard}
@ -35,12 +35,12 @@ public class MyClipboard {
public static MyClipboard getSystemClipboard() throws UnsupportedSystemException { public static MyClipboard getSystemClipboard() throws UnsupportedSystemException {
ClipboardListener clipboardListener; ClipboardListener clipboardListener;
if (isMac()) { if (systemUtils.isMac()) {
clipboardListener = new MacClipboardListener(); clipboardListener = new MacClipboardListener();
} else if (isWindows() || isUnix()) { } else if (systemUtils.isWindows() || systemUtils.isUnix()) {
clipboardListener = new WindowsOrUnixClipboardListener(); clipboardListener = new WindowsOrUnixClipboardListener();
} else { } else {
throw new UnsupportedSystemException("Your Operating System: " + System.getProperty("os.name") + "is not supported"); throw new UnsupportedSystemException("Your Operating System: '" + System.getProperty("os.name") + "' is not supported");
} }
return new MyClipboard(clipboardListener); return new MyClipboard(clipboardListener);
@ -146,15 +146,19 @@ public class MyClipboard {
clipboardListener.setFileMonitored(fileMonitored); clipboardListener.setFileMonitored(fileMonitored);
} }
private static boolean isMac() { public @NotNull ClipboardListener getClipboardListener() {
return SystemUtils.IS_OS_MAC; return clipboardListener;
}
public void setClipboardListener(@NotNull ClipboardListener clipboardListener) {
this.clipboardListener = clipboardListener;
} }
private static boolean isUnix() { protected static SystemUtils getSystemUtils() {
return SystemUtils.IS_OS_UNIX || SystemUtils.IS_OS_LINUX; return systemUtils;
} }
private static boolean isWindows() { protected static void setSystemUtils(SystemUtils systemUtils) {
return SystemUtils.IS_OS_WINDOWS; MyClipboard.systemUtils = systemUtils;
} }
} }

@ -0,0 +1,15 @@
package me.goudham;
class SystemUtils {
boolean isMac() {
return org.apache.commons.lang3.SystemUtils.IS_OS_MAC;
}
boolean isUnix() {
return org.apache.commons.lang3.SystemUtils.IS_OS_UNIX || org.apache.commons.lang3.SystemUtils.IS_OS_LINUX;
}
boolean isWindows() {
return org.apache.commons.lang3.SystemUtils.IS_OS_WINDOWS;
}
}
Loading…
Cancel
Save