Last active
December 2, 2018 20:06
-
-
Save PaulWoitaschek/7f3c4d5310a66ed4984785ee2d6f70ed to your computer and use it in GitHub Desktop.
Comparable selector
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
import java.util.* | |
import kotlin.comparisons.compareValuesBy | |
/** | |
* Demonstration of how the compare selectors could be moved to the companion object to prevent object allocations. | |
* Else each call to compareTo would create n new objects | |
* | |
* @author Paul Woitaschek | |
*/ | |
class Foo(val a: String, val b: Int, val c: Date) : Comparable<Foo> { | |
override fun compareTo(other: Foo) = compareValuesBy(this, other, *selectors) | |
companion object { | |
val selectors: Array<(Foo) -> Comparable<*>?> = arrayOf(Foo::a, Foo::b, Foo::c) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment