Converted from Optional to ActionResult

Moved Logger creation to LoggerFactory
Fixed creating more than 1 recording thread for each livestream
And more optimizations!
This commit is contained in:
kohlerpop1
2024-02-19 14:55:59 -05:00
parent 6b22154c82
commit 1b2a8bad93
9 changed files with 150 additions and 206 deletions

View File

@@ -2,11 +2,12 @@ package io.github.jwdeveloper.tiktok.common;
import lombok.Data;
import java.util.Optional;
import java.util.function.Function;
@Data
public class ActionResult<T>
{
public class ActionResult<T> {
private boolean success = true;
private T content;
private String message;
@@ -29,6 +30,10 @@ public class ActionResult<T>
return new ActionResultBuilder<>(content);
}
public static <T> ActionResult<T> of(Optional<T> optional) {
return new ActionResult<>(optional.orElse(null), optional.isPresent());
}
public boolean isFailure() {
return !isSuccess();
}
@@ -36,48 +41,47 @@ public class ActionResult<T>
public boolean hasMessage() {
return message != null;
}
public String toStack() {
return hasMessage() ? " - "+message : "";
}
public boolean hasContent() {
return content != null;
}
public static <T> ActionResult<T> success() {
return new ActionResult<>(null, true);
}
public <Output> ActionResult<Output> cast(Output output) {
return new ActionResult<>(output, this.isSuccess(), this.getMessage());
}
public <Output> ActionResult<Output> cast() {
return new ActionResult<>(null, this.isSuccess(), this.getMessage());
return cast(null);
}
public <U> ActionResult<U> map(Function<? super T, ? extends U> mapper) {
return hasContent() ? cast(mapper.apply(content)) : cast();
}
public static <Input, Output> ActionResult<Output> cast(ActionResult<Input> action, Output output) {
return new ActionResult<>(output, action.isSuccess(), action.getMessage());
}
public static <T> ActionResult<T> success(T payload) {
return new ActionResult<>(payload, true);
}
public static <T> ActionResult<T> success(T payload, String message) {
return new ActionResult<>(payload, true, message);
}
public static <T> ActionResult<T> failure() {
return new ActionResult<>(null, false);
public static <T> ActionResult<T> success(T payload) {
return success(payload, null);
}
public static <T> ActionResult<T> failure(String message) {
return new ActionResult<>(null, false, message);
public static <T> ActionResult<T> success() {
return success(null);
}
public static <T> ActionResult<T> failure(T target, String message) {
return new ActionResult<>(target, false, message);
}
public static <T> ActionResult<T> failure(String message) {
return failure(null, message);
}
public static <T> ActionResult<T> failure() {
return failure(null);
}
}

View File

@@ -9,23 +9,25 @@ public class LoggerFactory
{
public static Logger create(String name, LiveClientSettings settings) {
Logger logger = Logger.getLogger(name);
var handler = new ConsoleHandler();
handler.setFormatter(new Formatter() {
@Override
public String format(LogRecord record) {
var sb = new StringBuilder();
sb.append(ConsoleColors.GREEN).append("[").append(record.getLoggerName()).append("] ");
sb.append(ConsoleColors.GREEN).append("[").append(record.getLevel()).append("]: ");
sb.append(ConsoleColors.WHITE_BRIGHT).append(record.getMessage());
sb.append(ConsoleColors.RESET).append("\n");
return sb.toString();
}
});
logger.setUseParentHandlers(false);
logger.addHandler(handler);
logger.setLevel(settings.getLogLevel());
if (!settings.isPrintToConsole()) {
logger.setLevel(Level.OFF);
if (logger.getHandlers().length == 0) {
var handler = new ConsoleHandler();
handler.setFormatter(new Formatter()
{
@Override
public String format(LogRecord record) {
var sb = new StringBuilder();
sb.append(ConsoleColors.GREEN).append("[").append(record.getLoggerName()).append("] ");
sb.append(ConsoleColors.GREEN).append("[").append(record.getLevel()).append("]: ");
sb.append(ConsoleColors.WHITE_BRIGHT).append(record.getMessage());
sb.append(ConsoleColors.RESET).append("\n");
return sb.toString();
}
});
logger.setUseParentHandlers(false);
logger.addHandler(handler);
logger.setLevel(settings.getLogLevel());
if (!settings.isPrintToConsole())
logger.setLevel(Level.OFF);
}
return logger;
}