如何将一个封闭的Kotlin类转换为Java类

huangapple go评论72阅读模式
英文:

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&lt;out T&gt; {
    class Loading&lt;out T&gt; : Resource&lt;T&gt;()
    data class Success&lt;out T&gt;(val data: T) : Resource&lt;T&gt;()
    data class Failure&lt;out T&gt;(val exception: Exception) : Resource&lt;T&gt;()
}

答案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&lt;T&gt; {
// 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 &lt;T&gt; State&lt;T&gt; initial() {
return new State&lt;&gt;(INITIAL, null, null);
}
public static &lt;T&gt; State&lt;T&gt; loading() {
return new State&lt;&gt;(LOADING, null, null);
}
public static &lt;T&gt; State&lt;T&gt; success(T d) {
return new State&lt;&gt;(SUCCESS, d, null);
}
public static &lt;T&gt; State&lt;T&gt; error(Throwable e) {
return new State&lt;&gt;(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&lt;T&gt; {
class Loading extends Resource&lt;T&gt; { }
class Success extends Resource&lt;T&gt; {
@NonNull
T data;
public Success(T data) {
this.data = data;
}
}
class Failure extends Resource&lt;T&gt; {
@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&lt;T&gt; {
@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&lt;T&gt; loading() {
this.status = DataStatus.LOADING;
this.data = null;
this.error = null;
return this;
}
public StateData&lt;T&gt; success(@NonNull T data) {
this.status = DataStatus.SUCCESS;
this.data = data;
this.error = null;
return this;
}
public StateData&lt;T&gt; error(@NonNull Throwable error) {
this.status = DataStatus.ERROR;
this.data = null;
this.error = error;
return this;
}
public StateData&lt;T&gt; 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
}
}

huangapple
  • 本文由 发表于 2020年8月15日 23:20:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/63427640.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定