File tree Expand file tree Collapse file tree
core-kotlin/src/main/kotlin/com/baeldung/operators Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ enum class Currency {
2+ DOLLARS , EURO
3+ }
4+
5+ class Money (val amount : BigDecimal , val currency : Currency ) : Comparable<Money> {
6+
7+ override fun compareTo (other : Money ): Int =
8+ convert(Currency .DOLLARS ).compareTo(other.convert(Currency .DOLLARS ))
9+
10+ fun convert (currency : Currency ): BigDecimal = TODO ()
11+
12+ override fun equals (other : Any? ): Boolean {
13+ if (this == = other) return true
14+ if (other !is Money ) return false
15+
16+ if (amount != other.amount) return false
17+ if (currency != other.currency) return false
18+
19+ return true
20+ }
21+
22+ override fun hashCode (): Int {
23+ var result = amount.hashCode()
24+ result = 31 * result + currency.hashCode()
25+ return result
26+ }
27+ }
Original file line number Diff line number Diff line change 1+ interface Page <T > {
2+ fun pageNumber (): Int
3+ fun pageSize (): Int
4+ fun elements (): MutableList <T >
5+ }
6+
7+ operator fun <T > Page<T>.get (index : Int ): T = elements()[index]
8+ operator fun <T > Page<T>.get (start : Int , endExclusive : Int ): List <T > = elements().subList(start, endExclusive)
9+ operator fun <T > Page<T>.set (index : Int , value : T ) {
10+ elements()[index] = value
11+ }
12+
13+ operator fun <T > Page<T>.contains (element : T ): Boolean = element in elements()
14+ operator fun <T > Page<T>.iterator () = elements().iterator()
Original file line number Diff line number Diff line change 1+ data class Point (val x : Int , val y : Int )
2+
3+ operator fun Point.unaryMinus () = Point (- x, - y)
4+ operator fun Point.not () = Point (y, x)
5+ operator fun Point.inc () = Point (x + 1 , y + 1 )
6+ operator fun Point.dec () = Point (x - 1 , y - 1 )
7+
8+ operator fun Point.plus (other : Point ): Point = Point (x + other.x, y + other.y)
9+ operator fun Point.minus (other : Point ): Point = Point (x - other.x, y - other.y)
10+ operator fun Point.times (other : Point ): Point = Point (x * other.x, y * other.y)
11+ operator fun Point.div (other : Point ): Point = Point (x / other.x, y / other.y)
12+ operator fun Point.rem (other : Point ): Point = Point (x % other.x, y % other.y)
13+ operator fun Point.times (factor : Int ): Point = Point (x * factor, y * factor)
14+ operator fun Int.times (point : Point ): Point = Point (point.x * this , point.y * this )
Original file line number Diff line number Diff line change 1+ interface View
2+ class TextView (val value : String ) : View
3+ class ImageView (val url : String ) : View
4+
5+ class UI {
6+ private val views = mutableListOf<View >()
7+
8+ operator fun String.unaryPlus () {
9+ views.add(TextView (this ))
10+ }
11+
12+ operator fun View.unaryPlus () {
13+ views.add(this )
14+ }
15+ }
16+
17+ fun ui (init : UI .() -> Unit ): UI {
18+ val ui = UI ()
19+ ui.init ()
20+
21+ return ui
22+ }
23+
24+ fun main (args : Array <String >) {
25+ val header = ui {
26+ + ImageView (" http://logo.com" )
27+ + " Brand name"
28+ + " Brand description"
29+ }
30+ }
Original file line number Diff line number Diff line change 1+ operator fun <T > MutableCollection<T>.plusAssign (element : T ) {
2+ add(element)
3+ }
4+ operator fun BigInteger.plus (other : Int ): BigInteger = add(BigInteger (" $other " ))
You can’t perform that action at this time.
0 commit comments