Skip to content

Commit

Permalink
Explicitly define option behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
ME1312 committed Jun 5, 2021
1 parent 236d657 commit e519373
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 80 deletions.
60 changes: 20 additions & 40 deletions GalaxiEngine/src/net/ME1312/Galaxi/Engine/GalaxiOption.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,29 @@
public final class GalaxiOption<T> extends Value<T> {

// Directories
public static final GalaxiOption<File> APPDATA_DIRECTORY = $(Platform.getSystem().getAppDataDirectory());
public static final GalaxiOption<File> RUNTIME_DIRECTORY = $("user.dir", File::new, (File) null, USR);
public static final GalaxiOption<File> LOG_DIRECTORY = $(new File(RUNTIME_DIRECTORY.value(), "Logs"));
public static final GalaxiOption<File> APPDATA_DIRECTORY = $(APP);
public static final GalaxiOption<File> RUNTIME_DIRECTORY = $("user.dir", File::new, USR);
public static final GalaxiOption<File> LOG_DIRECTORY = $(new File(RUNTIME_DIRECTORY.value(), "Logs"), APP_DEF);

// Logging
public static final GalaxiOption<Boolean> USE_LOG_FILE = $("galaxi.log", Boolean::parseBoolean, true);
public static final GalaxiOption<Boolean> USE_RAW_LOG = $("galaxi.log.raw", Boolean::parseBoolean, GraphicsEnvironment.isHeadless());
public static final GalaxiOption<Boolean> USE_RAW_LOG_ANSI = $("galaxi.log.raw.ansi", Boolean::parseBoolean, (Boolean) null);
public static final GalaxiOption<Boolean> SHOW_DEBUG_MESSAGES = $("galaxi.log.debug", Boolean::parseBoolean, false);
public static final GalaxiOption<Boolean> COLOR_LOG_LEVELS = $("galaxi.log.color_levels", Boolean::parseBoolean, true);
public static final GalaxiOption<Boolean> USE_LOG_FILE = $("galaxi.log", Boolean::parseBoolean, true, USR_APP_DEF);
public static final GalaxiOption<Boolean> USE_RAW_LOG = $("galaxi.log.raw", Boolean::parseBoolean, GraphicsEnvironment.isHeadless(), USR_APP_DEF);
public static final GalaxiOption<Boolean> USE_RAW_LOG_ANSI = $("galaxi.log.raw.ansi", Boolean::parseBoolean, USR_APP);
public static final GalaxiOption<Boolean> SHOW_DEBUG_MESSAGES = $("galaxi.log.debug", Boolean::parseBoolean, false, USR_APP_DEF);
public static final GalaxiOption<Boolean> COLOR_LOG_LEVELS = $("galaxi.log.color_levels", Boolean::parseBoolean, true, USR_APP_DEF);

// Console
public static final GalaxiOption<Boolean> SHOW_CONSOLE_WINDOW = $("galaxi.console", Boolean::parseBoolean, System.console() == null);
public static final GalaxiOption<Boolean> PARSE_CONSOLE_VARIABLES = $("galaxi.console.parse_vars", Boolean::parseBoolean, false);
public static final GalaxiOption<Integer> MAX_CONSOLE_WINDOW_SCROLLBACK = $("galaxi.console.max_scrollback", Integer::parseInt, 15000);
public static final GalaxiOption<Double> CONSOLE_WINDOW_SIZE = $("galaxi.console.scaling", Double::parseDouble, (Double) null, USR);
public static final GalaxiOption<Boolean> SHOW_CONSOLE_WINDOW = $("galaxi.console", Boolean::parseBoolean, System.console() == null, USR_APP_DEF);
public static final GalaxiOption<Boolean> PARSE_CONSOLE_VARIABLES = $("galaxi.console.parse_vars", Boolean::parseBoolean, false, USR_APP_DEF);
public static final GalaxiOption<Integer> MAX_CONSOLE_WINDOW_SCROLLBACK = $("galaxi.console.max_scrollback", Integer::parseInt, 15000, USR_APP_DEF);
public static final GalaxiOption<Double> CONSOLE_WINDOW_SIZE = $("galaxi.console.scaling", Double::parseDouble, USR);

// Terminal
public static final GalaxiOption<Boolean> USE_ANSI = $("galaxi.terminal.ansi", Boolean::parseBoolean, true, USR_DEF);
public static final GalaxiOption<Boolean> USE_JLINE = $("galaxi.terminal.jline", Boolean::parseBoolean, true, USR_DEF);

// Everything Else
public static final GalaxiOption<Boolean> ENABLE_RELOAD = $(false);
public static final GalaxiOption<Boolean> ENABLE_RELOAD = $(false, APP_DEF);

private T app;
private T value;
Expand Down Expand Up @@ -103,49 +103,29 @@ public T usr() {
public T value() {
if (uninitialized) {
uninitialized = false;
switch (mode.select(usr, app, def)) {
case USR:
value = usr;
break;
case APP:
value = app;
break;
case DEF:
value = def;
break;
}
value = mode.select(usr, app, def);
}
return value;
}


// These are all shorthand constructors
private static <T> GalaxiOption<T> $(T def) {
return $(def, APP_DEF);
private static <T> GalaxiOption<T> $(GalaxiOptionMode mode) {
return $((T) null, mode);
}
private static <T> GalaxiOption<T> $(T def, GalaxiOptionMode mode) {
return new GalaxiOption<T>(null, def, mode);
}
private static <T> GalaxiOption<T> $(ExceptionReturnRunnable<T> def) {
return $(def, APP_DEF);
}
private static <T> GalaxiOption<T> $(ExceptionReturnRunnable<T> def, GalaxiOptionMode mode) {
return $(Util.getDespiteException(def, null), mode);
}
private static <T> GalaxiOption<T> $(String usr, ExceptionReturnCallback<String, T> type, T def) {
return $(usr, type, def, USR_APP_DEF);
private static <T> GalaxiOption<T> $(String usr, ExceptionReturnCallback<String, T> type, GalaxiOptionMode mode) {
return $(usr, type, (T) null, mode);
}
private static <T> GalaxiOption<T> $(String usr, ExceptionReturnCallback<String, T> type, T def, GalaxiOptionMode mode) {
return new GalaxiOption<T>(parse(usr, type), def, mode);
}
private static <T> GalaxiOption<T> $(String usr, ExceptionReturnCallback<String, T> type, ExceptionReturnRunnable<T> def) {
return $(usr, type, def, USR_APP_DEF);
String value = System.getProperty(usr);
return new GalaxiOption<T>((value != null)?Util.getDespiteException(() -> type.run(value), null):null, def, mode);
}
private static <T> GalaxiOption<T> $(String usr, ExceptionReturnCallback<String, T> type, ExceptionReturnRunnable<T> def, GalaxiOptionMode mode) {
return $(usr, type, Util.getDespiteException(def, null), mode);
}
private static <T> T parse(String usr, ExceptionReturnCallback<String, T> type) {
String value = System.getProperty(usr);
return (value != null)? Util.getDespiteException(() -> type.run(value), null): null;
}
}
93 changes: 53 additions & 40 deletions GalaxiEngine/src/net/ME1312/Galaxi/Engine/GalaxiOptionMode.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,59 +11,72 @@ enum GalaxiOptionMode {
APP(null),
DEF(null),

USR_DEF((usr, app, def) -> select(new Object[]{usr, def}, new GalaxiOptionMode[]{USR, DEF})),
DEF_USR((usr, app, def) -> select(new Object[]{def, usr}, new GalaxiOptionMode[]{DEF, USR})),
APP_DEF((usr, app, def) -> select(new Object[]{app, def}, new GalaxiOptionMode[]{APP, DEF})),
DEF_APP((usr, app, def) -> select(new Object[]{def, app}, new GalaxiOptionMode[]{DEF, APP})),
USR_DEF((usr, app, def) -> new Object[] {usr, def}, USR, DEF),
DEF_USR((usr, app, def) -> new Object[] {def, usr}, DEF, USR),
APP_DEF((usr, app, def) -> new Object[] {app, def}, APP, DEF),
DEF_APP((usr, app, def) -> new Object[] {def, app}, DEF, APP),
USR_APP((usr, app, def) -> new Object[] {usr, app}, USR, APP),

USR_APP_DEF((usr, app, def) -> select(new Object[]{usr, app, def}, new GalaxiOptionMode[]{USR, APP, DEF})),
USR_DEF_APP((usr, app, def) -> select(new Object[]{usr, def, app}, new GalaxiOptionMode[]{USR, DEF, APP})),
DEF_USR_APP((usr, app, def) -> select(new Object[]{def, usr, app}, new GalaxiOptionMode[]{DEF, USR, APP})),
USR_APP_DEF((usr, app, def) -> new Object[] {usr, app, def}, USR, APP, DEF),
USR_DEF_APP((usr, app, def) -> new Object[] {usr, def, app}, USR, DEF, APP),
DEF_USR_APP((usr, app, def) -> new Object[] {def, usr, app}, DEF, USR, APP),
;
private final OptionSelector selector;
private GalaxiOptionMode(OptionSelector selector) {
this.selector = selector;
private final Reorderer reorderer;
private final GalaxiOptionMode[] order;
private GalaxiOptionMode(Reorderer reorderer, GalaxiOptionMode... order) {
this.reorderer = reorderer;
this.order = order;
}

private interface OptionSelector {
GalaxiOptionMode select(Object usr, Object app, Object def);
private interface Reorderer {
Object[] reorder(Object usr, Object app, Object def);
}

public GalaxiOptionMode select(Object usr, Object app, Object def) {
return (selector == null)? this : selector.select(usr, app, def);
}

private static GalaxiOptionMode select(Object[] objects, GalaxiOptionMode[] results) {
if (objects.length != results.length) throw new IllegalArgumentException();
@SuppressWarnings("unchecked")
public <T> T select(T usr, T app, T def) {
if (reorderer == null) {
switch (this) {
case USR:
return usr;
case APP:
return app;
case DEF:
return def;
default:
return null;
}
} else {
Object[] objects = reorderer.reorder(usr, app, def);

Object l = objects[0];
GalaxiOptionMode lt = results[0];
for (int i = 1; i < objects.length && (l == null || lt == DEF); ++i) {
Object c = objects[i];
GalaxiOptionMode ct = results[i];
Object l = objects[0];
GalaxiOptionMode lt = order[0];
for (int i = 1; i < objects.length && (l == null || lt == DEF); ++i) {
Object c = objects[i];
GalaxiOptionMode ct = order[i];

if (lt == DEF) {
if (l instanceof Boolean && c instanceof Boolean) {
if (((Boolean) l).compareTo((Boolean) c) > 0) {
l = c;
lt = ct;
if (lt == DEF) {
if (l instanceof Boolean && c instanceof Boolean) {
if (((Boolean) l).compareTo((Boolean) c) > 0) {
l = c;
lt = ct;
break;
}
} else if (l != null) {
break;
}
} else if (l != null) {
break;
}
}

if (c == null) {
// no input
} else if (c instanceof Number && ((Number) c).longValue() < 0) {
// invalid input
} else {
l = c;
lt = ct;
if (c == null) {
// no input
} else if (c instanceof Number && ((Number) c).longValue() < 0) {
// invalid input
} else {
l = c;
lt = ct;
}
}
}

return lt;
return (T) l;
}
}
}

0 comments on commit e519373

Please sign in to comment.