英文:
How to transform a sealed Kotlin class into a Java class
问题
我想知道在Java代码中是否有与这个Kotlin密封类等效的写法:
public abstract class Resource<T> {
private Resource() {}
public static final class Loading<T> extends Resource<T> {
private Loading() {}
}
public static final class Success<T> extends Resource<T> {
private final T data;
public Success(T data) {
this.data = data;
}
public T getData() {
return data;
}
}
public static final class Failure<T> extends Resource<T> {
private final Exception exception;
public Failure(Exception exception) {
this.exception = exception;
}
public Exception getException() {
return exception;
}
}
}
英文:
I'm wondering if there is an equivalent in Java code for this kotlin sealed class
sealed class Resource<out T> {
class Loading<out T> : Resource<T>()
data class Success<out T>(val data: T) : Resource<T>()
data class Failure<out T>(val exception: Exception) : Resource<T>()
}
答案1
得分: 2
以下是我在我的Java项目中使用的内容:
public class State<T> {
// 状态,如果可能的话,使用枚举
public static final int INITIAL = 0;
public static final int LOADING = 1;
public static final int SUCCESS = 2;
public static final int ERROR = 3;
private final int type;
private final T data;
private final Throwable error;
private State(@NonNull int type, @Nullable T d, @Nullable Throwable e) {
this.type = type;
this.data = d;
this.error = e;
}
public static <T> State<T> initial() {
return new State<>(INITIAL, null, null);
}
public static <T> State<T> loading() {
return new State<>(LOADING, null, null);
}
public static <T> State<T> success(T d) {
return new State<>(SUCCESS, d, null);
}
public static <T> State<T> error(Throwable e) {
return new State<>(ERROR, null, e);
}
public int getType() {
return type;
}
public T getData() {
return data;
}
public Throwable getError() {
return error;
}
}
英文:
This is what I use in my Java project:
public class State<T> {
// States, use enums if possible
public static final int INITIAL = 0;
public static final int LOADING = 1;
public static final int SUCCESS = 2;
public static final int ERROR = 3;
private final int type;
private final T data;
private final Throwable error;
private State(@NonNull int type, @Nullable T d, @Nullable Throwable e) {
this.type = type;
this.data = d;
this.error = e;
}
public static <T> State<T> initial() {
return new State<>(INITIAL, null, null);
}
public static <T> State<T> loading() {
return new State<>(LOADING, null, null);
}
public static <T> State<T> success(T d) {
return new State<>(SUCCESS, d, null);
}
public static <T> State<T> error(Throwable e) {
return new State<>(ERROR, null, e);
}
public int getType() {
return type;
}
public T getData() {
return data;
}
public Throwable getError() {
return error;
}
}
答案2
得分: 1
就像这样(不幸的是,太啰嗦):
class Resource<T> {
class Loading extends Resource<T> { }
class Success extends Resource<T> {
@NonNull
T data;
public Success(T data) {
this.data = data;
}
}
class Failure extends Resource<T> {
@NonNull
Exception exception;
public Failure(Exception exception) {
this.exception = exception
}
}
}
英文:
Just like that (unfortunately, too verbose):
class Resource<T> {
class Loading extends Resource<T> { }
class Success extends Resource<T> {
@NonNull
T data;
public Success(T data) {
this.data = data;
}
}
class Failure extends Resource<T> {
@NonNull
Exception exception;
public Failure(Exception exception) {
this.exception = exception
}
}
}
答案3
得分: 1
谢谢,Augusto,我也使用过这个
public class StateData<T> {
@NonNull
private DataStatus status;
@Nullable
private T data;
@Nullable
private Throwable error;
public StateData() {
this.status = DataStatus.CREATED;
this.data = null;
this.error = null;
}
public StateData<T> loading() {
this.status = DataStatus.LOADING;
this.data = null;
this.error = null;
return this;
}
public StateData<T> success(@NonNull T data) {
this.status = DataStatus.SUCCESS;
this.data = data;
this.error = null;
return this;
}
public StateData<T> error(@NonNull Throwable error) {
this.status = DataStatus.ERROR;
this.data = null;
this.error = error;
return this;
}
public StateData<T> complete() {
this.status = DataStatus.COMPLETE;
return this;
}
@NonNull
public DataStatus getStatus() {
return status;
}
@Nullable
public T getData() {
return data;
}
@Nullable
public Throwable getError() {
return error;
}
public enum DataStatus {
CREATED,
SUCCESS,
ERROR,
LOADING,
COMPLETE
}
}
英文:
Thanks Augusto, I have also used this one
public class StateData<T> {
@NonNull
private DataStatus status;
@Nullable
private T data;
@Nullable
private Throwable error;
public StateData() {
this.status = DataStatus.CREATED;
this.data = null;
this.error = null;
}
public StateData<T> loading() {
this.status = DataStatus.LOADING;
this.data = null;
this.error = null;
return this;
}
public StateData<T> success(@NonNull T data) {
this.status = DataStatus.SUCCESS;
this.data = data;
this.error = null;
return this;
}
public StateData<T> error(@NonNull Throwable error) {
this.status = DataStatus.ERROR;
this.data = null;
this.error = error;
return this;
}
public StateData<T> complete() {
this.status = DataStatus.COMPLETE;
return this;
}
@NonNull
public DataStatus getStatus() {
return status;
}
@Nullable
public T getData() {
return data;
}
@Nullable
public Throwable getError() {
return error;
}
public enum DataStatus {
CREATED,
SUCCESS,
ERROR,
LOADING,
COMPLETE
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论