Skip to content

Instantly share code, notes, and snippets.

@Matt-MX
Last active November 22, 2022 03:13
Show Gist options
  • Save Matt-MX/72062a50f50f20e583baed3321cdc9d0 to your computer and use it in GitHub Desktop.
Save Matt-MX/72062a50f50f20e583baed3321cdc9d0 to your computer and use it in GitHub Desktop.
Safe Check Utils (Console apps)
import java.util.function.Consumer;
public class AttemptResult<I, O> {
private O value;
public AttemptResult(O result) {
this.value = result;
}
public boolean isSuccessful() {
return value != null;
}
public AttemptResult<I, O> ifSuccessful(Consumer<O> consumer) {
if (isSuccessful()) consumer.accept(value);
return this;
}
public AttemptResult<I, O> notSuccessful(Runnable runnable) {
if (!isSuccessful()) runnable.run();
return this;
}
public AttemptResult<I, O> orElse(O otherwise) {
this.value = otherwise;
return this;
}
public O value() {
return value;
}
public O valueOr(O otherwise) {
return isSuccessful() ? value : otherwise;
}
}
public class ConsoleColors {
public static void main(String[] args) {
System.out.println("\033[38;2;222;43;222mColor");
System.out.println(color("#FF9811bHello world #23BB29bbruh"));
System.out.println("No color");
}
/**
* Regex to help decode color values.
* #[Six Hex values][b or f (background or foreground)]
*/
private static final Pattern pattern = Pattern.compile("#[a-fA-F0-9]{6}[b|f]");
/**
* Applies color to a string, only use in console.
*
* @param s string to encode color onto
* @return the encoded and formatted string
*/
public static String color (String s) {
Matcher match = pattern.matcher(s);
while (match.find()) {
String color = s.substring(match.start(), match.end() - 1);
Color col = Color.decode(color);
String bgOrFg = color.toCharArray()[color.length() - 1] == 'b' ? "48" : "38";
String color1 = s.substring(match.start(), match.end());
s = s.replace(color1, "\033[" + bgOrFg + ";2;" + col.getRed() + ";" + col.getGreen() + ";" + col.getBlue() + "m");
match = pattern.matcher(s);
}
return s + "\033[0m";
}
}
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
public class SafeUtils {
public static <I, O> AttemptResult<I, O> attempt(I in, Function<I, O> operation) {
try {
return new AttemptResult<>(operation.apply(in));
} catch (Exception ignored) {}
return new AttemptResult<>(null);
}
public static <I, O> O attemptOrNull(I in, Function<I, O> operation) {
return attempt(in, operation).value();
}
public static <I, O> AttemptResult<I, O> attemptUntil(Supplier<I> in, Function<I, O> operation) {
return attemptUntil(in, operation, null, -1);
}
public static <I, O> AttemptResult<I, O> attemptUntil(Supplier<I> in, Function<I, O> operation, Consumer<Integer> failure) {
return attemptUntil(in, operation, failure, -1);
}
public static <I, O> AttemptResult<I, O> attemptUntil(Supplier<I> in, Function<I, O> operation, Consumer<Integer> failure, int maxAttempts) {
boolean stop = false;
int current = 0;
while (!stop) {
AttemptResult<I, O> result = attempt(in.get(), operation);
if (result.isSuccessful()) return result;
else failure.accept(maxAttempts - current);
current++;
if (maxAttempts > 0 && current >= maxAttempts) stop = true;
}
return new AttemptResult<>(null);
}
}
import java.util.Scanner;
public class ScannerExample {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Input an integer");
SafeUtils.attempt(scanner.next(), Integer::parseInt)
.ifSuccessful(i -> System.out.println("You inputted " + i + "!"))
.notSuccessful(() -> System.out.println("You didn't input an integer!"));
int i = SafeUtils.attemptUntil(scanner::next, Integer::parseInt, () -> System.out.println("Input a valid integer"), 2).valueOr(-1);
System.out.println("You inputted " + i);
scanner.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment