You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
69 lines
1.9 KiB
69 lines
1.9 KiB
package model;
|
|
|
|
public class ResultContainer<T> {
|
|
private final T result;
|
|
private final boolean success;
|
|
private final String message;
|
|
private final long timestamp;
|
|
|
|
private ResultContainer(T result, boolean success, String message) {
|
|
this.result = result;
|
|
this.success = success;
|
|
this.message = message;
|
|
this.timestamp = System.currentTimeMillis();
|
|
}
|
|
|
|
public static <T> ResultContainer<T> success(T result) {
|
|
if (result == null) {
|
|
throw new IllegalArgumentException("Result cannot be null for success container");
|
|
}
|
|
return new ResultContainer<>(result, true, "Success");
|
|
}
|
|
|
|
public static <T> ResultContainer<T> success(T result, String message) {
|
|
if (result == null) {
|
|
throw new IllegalArgumentException("Result cannot be null for success container");
|
|
}
|
|
return new ResultContainer<>(result, true, message);
|
|
}
|
|
|
|
public static <T> ResultContainer<T> failure(String message) {
|
|
return new ResultContainer<>(null, false, message);
|
|
}
|
|
|
|
public static <T> ResultContainer<T> failure(String message, Throwable cause) {
|
|
return new ResultContainer<>(null, false, message + ": " + cause.getMessage());
|
|
}
|
|
|
|
public T getResult() {
|
|
if (!success) {
|
|
throw new IllegalStateException("Cannot get result from failed container");
|
|
}
|
|
return result;
|
|
}
|
|
|
|
public T getResultOrDefault(T defaultValue) {
|
|
return success ? result : defaultValue;
|
|
}
|
|
|
|
public boolean isSuccess() {
|
|
return success;
|
|
}
|
|
|
|
public boolean isFailure() {
|
|
return !success;
|
|
}
|
|
|
|
public String getMessage() {
|
|
return message;
|
|
}
|
|
|
|
public long getTimestamp() {
|
|
return timestamp;
|
|
}
|
|
|
|
public String toString() {
|
|
return String.format("ResultContainer{success=%s, message='%s', timestamp=%d}",
|
|
success, message, timestamp);
|
|
}
|
|
}
|