Skip to content

Instantly share code, notes, and snippets.

Feeding my inner nerd

Sławomir Czerwiński sczerwinski

Feeding my inner nerd
Block or report user

Report or block sczerwinski

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
sczerwinski / AndroidString.kt
Created Jul 23, 2019
Representation of Android String, which is either a string or a string resource
View AndroidString.kt
import android.content.Context
import androidx.annotation.StringRes
import it.czerwinski.kotlin.util.Either
import it.czerwinski.kotlin.util.Left
import it.czerwinski.kotlin.util.Right
import it.czerwinski.kotlin.util.merge
typealias AndroidString = Either<Int, String>
fun AndroidString(string: String): AndroidString = Right(string)
sczerwinski / NotNullAssertionOperator.kt
Created Mar 10, 2019
Comparison of `!!` operator, `requireNotNull()`, `checkNotNull()` and null-safe operators: `?.`, `?:`
View NotNullAssertionOperator.kt
import org.junit.Test
class NotNullAssertionOperator {
fun exclamation() {
fun square(x: Double?): Double = x!!.times(x)
sczerwinski / WeakReferenceProperty.kt
Created Sep 14, 2017
Weak reference property delegate in Kotlin
View WeakReferenceProperty.kt
class WeakReferenceProperty<T>(private val creator: () -> T) {
private var value: WeakReference<T> =
operator fun getValue(thisRef: Any?, property: KProperty<*>): T =
value.get() ?: creator().also { value = WeakReference(it) }
private fun <T> weak(creator: () -> T) =
sczerwinski / AndroidMatchers.kt
Created Jun 15, 2017
Kotlin functions creating custom Hamcrest matchers for Android
View AndroidMatchers.kt
import android.view.View
import org.hamcrest.Description
import org.hamcrest.Matcher
import org.hamcrest.TypeSafeMatcher
inline fun <reified T : View> withType(): Matcher<View> = object : TypeSafeMatcher<View>() {
override fun describeTo(description: Description?) {
description?.appendText("with type: ${}")
sczerwinski / AndroidTestRules.kt
Last active Jun 12, 2017
Kotlin functions providing ActivityTestRule and IntentsTestRule with a simplified syntax.
View AndroidTestRules.kt
inline fun <reified T : Activity> activityTestRule(initialTouchMode: Boolean = false, launchActivity: Boolean = true) =
ActivityTestRule(, initialTouchMode, launchActivity)
inline fun <reified T : Activity> intentsTestRule(initialTouchMode: Boolean = false, launchActivity: Boolean = true) =
IntentsTestRule(, initialTouchMode, launchActivity)
sczerwinski / SSL.kt
Last active Jan 25, 2017
Kotlin port of the solution proposed by @naderghanabri:
View SSL.kt
object SSL {
fun trustAll() {
val sslContext = SSLContext.getInstance("SSL")
sslContext.init(null, arrayOf(TrustAll), SecureRandom())
sczerwinski / MaterialColor.kt
Created Jul 22, 2016
Material Design colors palette in Kotlin
View MaterialColor.kt
enum class MaterialColor(val tones: Map<Int, Int> = emptyMap(), val accentTones: Map<Int, Int> = emptyMap(), val singleColor: Int? = null) {
tones = mapOf(
50 to 0xFFEBEE,
100 to 0xFFCDD2,
200 to 0xEF9A9A,
300 to 0xE57373,
400 to 0xEF5350,
sczerwinski / SquareMatrix.kt
Last active Jul 4, 2017
Functional square matrix
View SquareMatrix.kt
class SquareMatrix(val size: Int, private val elements: (Int, Int) -> Float) {
operator fun get(row: Int, col: Int): Float {
require(row in 0..size - 1) { "Row ${row} out of bounds: 0..${size - 1}" }
require(col in 0..size - 1) { "Column ${col} out of bounds: 0..${size - 1}" }
return elements(row, col)
You can’t perform that action at this time.