Skip to content

Instantly share code, notes, and snippets.

@eungju
eungju / RxTest.kt
Last active November 26, 2017 14:13
Catch UndeliverableException
@Test
fun simple() {
val observable = Observable.create<Int> { emitter ->
emitter.onNext(1)
emitter.onError(IOException("Timeout"))
}
observable
.take(1)
.subscribe({ assertEquals(1, it) }, { assertTrue(it is UndeliverableException) })
}
@eungju
eungju / Correct.kt
Last active August 29, 2017 02:00
Rx에서 reactive base type 만들 때 자주 하는 실수
override fun connectDevice(userDeviceRef: UserDeviceRef, deviceAttributes: DeviceAttributes, nodeRef: PilsnerRef): Completable
= Completable.fromRunnable {
rwLock.write {
devicePresences[userDeviceRef.deviceId] = Presence(userDeviceRef, deviceAttributes, nodeRef)
userDevices.put(userDeviceRef.userId, userDeviceRef.deviceId)
}
}
override fun getPresencesOfUser(userId: UserId): Single<List<Presence>>
= Single.fromCallable {
@eungju
eungju / Headers.kt
Last active July 21, 2017 04:55
HTTP 헤더 이름은 왜 타입을 안만드나?
class Http {
data class HeaderName(val name: String) : Comparable<HeaderName> {
override fun compareTo(other: HeaderName): Int = name.compareTo(other.name, true)
fun get(headers: MultiMap): String? = headers.get(name)
fun set(headers: MultiMap, value: String): Unit { headers.set(name, value) }
fun copyIfExist(from: MultiMap, to: MultiMap) {
get(from)?.let { set(to, it) }
}
@eungju
eungju / README.md
Last active July 5, 2022 21:18
Kotlin let run apply also

let

inline fun <T, R> T.let(block: (T) -> R): R = block(this)

run

inline fun <T, R> T.run(block: T.() -> R): R = block()

apply

@eungju
eungju / RxJavaTest.java
Created June 15, 2017 01:26
RxJava 2.1.0+withLatestFrom+cache+firstOrError causes NoSuchElementException
@Test
fun withLatestFromCausesNoSuchElementException() {
val state = Observable.just(42).cacheWithInitialCapacity(1)
var error = 0
val threads = (0..4 - 1)
.map {
Thread {
Observable.just(Unit)
.withLatestFrom(state, BiFunction { _: Unit, state: Int -> state })
.firstOrError()
@eungju
eungju / FlowLayout.java
Last active September 1, 2016 12:15
안드로이드 커스텀 레이아웃 단위 테스트
/**
* https://github.com/ultimate-deej/FlowLayout-for-Android/blob/master/src/org/deejdev/android/FlowLayout.java
*/
public class FlowLayout extends ViewGroup {
public static final int LEFT_TO_RIGHT = 0;
public static final int TOP_DOWN = 1;
public static final int RIGHT_TO_LEFT = 2;
public static final int BOTTOM_UP = 3;
private int mGravity;
@eungju
eungju / RxPermission.kt
Last active August 8, 2016 04:05
Android Permissions with RxJava
import rx.Observable
interface RxPermission {
val isGranted: Boolean
fun request(): Observable<Boolean>
}
@eungju
eungju / PingPongTest.kt
Last active July 8, 2016 02:17
Ping Pong
class PingPongTest {
fun containsDigit(n: Int, k: Int): Boolean = if (n == 0) false else if (n % 10 == k) true else containsDigit(n / 10, k)
fun pingPong(n: Int) = Observable.range(1, n - 1)
.map { it % 7 == 0 || containsDigit(it, 7) }
.scan(1, { direction, turn -> direction * if (turn) -1 else 1 })
.scan { acc, direction -> acc + direction }
.last()
.toBlocking()
.single()
@eungju
eungju / TypeTest.kt
Last active May 18, 2016 04:13
123 > "123"
import org.junit.Test
import kotlin.test.assertTrue
class TypeTest {
operator fun Int.compareTo(other: String): Int {
return 1
}
@Test
fun gt() {
@eungju
eungju / after.kt
Last active May 10, 2016 16:44
MVP+RxJava
class LocationAgreementAgree(scheduling: RxScheduling,
api: Api,
session: Session) : LocationAgreementSubmit(scheduling, api, session, true) {
private val disuseChecked = BehaviorRelay.create<Boolean>(false)
private val termsOfUseChecked = BehaviorRelay.create<Boolean>(false)
private val ageChecked = BehaviorRelay.create<Boolean>(false)
override val positiveEnabled = Observable.combineLatest(disuseChecked, termsOfUseChecked, ageChecked, { a, b, c -> a && b && c })
fun disuseChecked(): Observable<Boolean> = disuseChecked