Skip to content

Instantly share code, notes, and snippets.

View fo2rist's full-sized avatar

Dmitry Sitnikov fo2rist

View GitHub Profile
@fo2rist
fo2rist / Step_1_InitialCode.java
Last active March 23, 2020 22:22
Java To Kotlin Conversion Example
public class NonLazyDemoAdapter extends FragmentStatePagerAdapter {
static class FragmentBundle implements Serializable {
int id;
String name;
FragmentBundle(int id, String name) {
this.id = id;
this.name = name;
}
@fo2rist
fo2rist / RxSchedulersTrampolineRule.kt
Created November 23, 2019 03:46
RxExceptionsHandler Handler Rule
class RxSchedulersTrampolineRule : ExternalResource() {
private var defaultExceptionHandler: Thread.UncaughtExceptionHandler? = null
override fun before() {
// Remember current default exception handler and replace it with one that rethrows the exception
// Rethrowing allows unit test fail in the same place the application would fail.
defaultExceptionHandler = Thread.getDefaultUncaughtExceptionHandler()
Thread.setDefaultUncaughtExceptionHandler { _, exc -> throw exc }
// Turn on Trampoline rule to execute RX call synchronously
@fo2rist
fo2rist / RxExceptionSampleTest.kt
Last active November 23, 2019 04:46
RxExceptionsHandler Sample Test
private fun waitAndFail(): Single<Nothing> {
return Single.fromCallable {
Thread.sleep(500)
throw IllegalStateException()
}
}
@Test
fun `subscription on failing function and do not expect the exception`() {
waitAndFail().subscribe()
@fo2rist
fo2rist / TwoDataClasses.java
Last active August 28, 2019 15:25
Gson Examples. Two data classes corresponding Java code
public final class WithoutDefaultConstructor {
private final int intValue;
@NotNull
private final String strValue;
public WithoutDefaultConstructor(int intValue, @NotNull String strValue) {
super();
this.intValue = intValue;
this.strValue = strValue;
}
@fo2rist
fo2rist / TwoDataClasses.kt
Created August 27, 2019 04:32
Gson Examples. Two data classes
data class WithoutDefaultConstructor(
val intValue: Int,
val strValue: String = "default"
)
data class WithDefaultConstructor(
val intValue: Int = 1,
val strValue: String = "default"
)
@fo2rist
fo2rist / SomeData.kt
Created August 27, 2019 04:24
Gson Examples. Class with default constructor
data class SomeData(
val intValue: Int = 0,
val strValue: String = "default value"
)
@fo2rist
fo2rist / SomeData.kt
Created August 27, 2019 04:13
Gson Examples. Class without default constructor
data class SomeData(
val intValue: Int,
val strValue: String = "default value"
)
@fo2rist
fo2rist / SomeStrictActivity.kt
Created July 9, 2019 18:50
ActivityThatMayCrash.kt
class SomeStrictActivity : Activity() {
companion object {
@JvmStatic
fun createIntent(context: Context, importantParameter: Parameter): Intent {
val intent = Intent(context, SomeStrictActivity::class.java)
intent.putExtra(EXTRA_PARAMETER, importantParameter)
return intent
}
}
// try-ignore approach
try {
doAction()
} catch (exc: SomePreconditionViolated) {
//ignore
} finally {
finalize()
}
/* ^ COMPARE v */
@fo2rist
fo2rist / TryOrElseExample.kt
Last active July 6, 2019 00:05
Initialization example
// try-fail-catch approach
try {
doMainAction()
} catch (exc: SomePreconditionViolated) {
doFallbackAction()
}
/* ^ COMPARE v */
// check-and-execute approach