Created
September 30, 2021 13:31
-
-
Save igor-brishkoski/4df636445358f85ca40d22e02b4a9b2d to your computer and use it in GitHub Desktop.
Kotlin Result to Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
////////// Kotlin ////////////////////// | |
class MainActivity : AppCompatActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
val t = JavaTest() | |
t.test() | |
} | |
} | |
object MySingleton { | |
fun getResult(c: (Result<String>) -> Unit) { | |
c(Result.success("Hello")) | |
} | |
fun <T> toJavaResult(r: Result<T>): JavaResult<T> { | |
Log.d("mytag-singleton", r.toString()) | |
// D/mytag-singleton: Success(Success(Hello)) | |
// Why double wrap? Success(Success(Hello)) | |
return r.toJavaResult() | |
} | |
} | |
// extension helper | |
fun <T> Result<T>.toJavaResult(): JavaResult<T> { | |
Log.d("mytag-extensions", toString()) | |
//Here too D/mytag-extensions: Success(Success(Hello)) | |
fold( | |
onSuccess = { data: T -> return JavaResult.success(data) }, // returns JavaResult(Success("Hello")) | |
onFailure = { throwable: Throwable -> return JavaResult.error(throwable) }, | |
) | |
// This works as intended | |
// result.onSuccess { // AS says it is a T, but it's Success(T) | |
// (it as Result<T>).onSuccess { d -> | |
// return JavaResult.success(d) // returns JavaResult("Hello") | |
// } | |
// (it as Result<T>).onFailure { tr -> | |
// return JavaResult.error(tr) | |
// } | |
// } | |
} | |
/////////////// Java ////////////////////// | |
public class JavaResult<T> { | |
public static final int SUCCESS = 0; | |
public static final int ERROR = 1; | |
private final int type; | |
private final T data; | |
private final Throwable error; | |
private JavaResult(int type, @Nullable T d, @Nullable Throwable e) { | |
this.type = type; | |
this.data = d; | |
this.error = e; | |
} | |
public static <T> JavaResult<T> success(T d) { | |
return new JavaResult<>(SUCCESS, d, null); | |
} | |
public static <T> JavaResult<T> error(Throwable e) { | |
return new JavaResult<>(ERROR, null, e); | |
} | |
... | |
} | |
public class JavaTest { | |
public void test() { | |
MySingleton.INSTANCE.getResult(kotlinResult -> { | |
Log.d("mytag-javatest", kotlinResult.toString()); // D/mytag: Success(Hello) | |
JavaResult<String> javaResult = MySingleton.INSTANCE.toJavaResult(kotlinResult); | |
switch (javaResult.getType()) { | |
case JavaResult.SUCCESS: { | |
// Caused by: java.lang.ClassCastException: kotlin.Result cannot be cast to java.lang.String | |
// Still Success("Hello") | |
Log.d("mytag", javaResult.getData().toString()); | |
} | |
case JavaResult.ERROR: { | |
Log.e("mytag", javaResult.getError().getMessage()); | |
} | |
} | |
return Unit.INSTANCE; | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment