Created
May 21, 2018 07:30
-
-
Save nesterchung/6e6edb5b8c5eb67d18e89e0340d23f20 to your computer and use it in GitHub Desktop.
kotlin hepler function for RemoteCallbackList
This file contains hidden or 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
inline fun <E : IInterface?> RemoteCallbackList<E>.forEach(f: (E) -> Unit) { | |
var i = beginBroadcast() | |
while (i > 0) { | |
i-- | |
try { | |
f(getBroadcastItem(i)) | |
} catch (e: RemoteException) { | |
// The RemoteCallbackList will take care of removing | |
// the dead object for us. | |
} | |
} | |
finishBroadcast() | |
} | |
inline fun <E : IInterface?, R> RemoteCallbackList<E>.map(f: (E) -> R): List<R> { | |
val ret = mutableListOf<R>() | |
mapTo(ret) { f(it) } | |
return ret | |
} | |
inline fun <E : IInterface?, R, C : MutableCollection<R>> RemoteCallbackList<E>.mapTo(c: C, f: (E) -> R) { | |
forEach { | |
c.add(f(it)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment