From 2ee5e03b887585e5a4f1b4657b5d47583f2984fb Mon Sep 17 00:00:00 2001 From: Ali Dehghani Date: Thu, 22 Nov 2018 23:07:29 +0330 Subject: [PATCH] Added the codes used for operator overloading article --- .../kotlin/com/baeldung/operators/Money.kt | 27 +++++++++++++++++ .../kotlin/com/baeldung/operators/Page.kt | 14 +++++++++ .../kotlin/com/baeldung/operators/Point.kt | 14 +++++++++ .../main/kotlin/com/baeldung/operators/UI.kt | 30 +++++++++++++++++++ .../kotlin/com/baeldung/operators/Utils.kt | 4 +++ 5 files changed, 89 insertions(+) create mode 100644 core-kotlin/src/main/kotlin/com/baeldung/operators/Money.kt create mode 100644 core-kotlin/src/main/kotlin/com/baeldung/operators/Page.kt create mode 100644 core-kotlin/src/main/kotlin/com/baeldung/operators/Point.kt create mode 100644 core-kotlin/src/main/kotlin/com/baeldung/operators/UI.kt create mode 100644 core-kotlin/src/main/kotlin/com/baeldung/operators/Utils.kt diff --git a/core-kotlin/src/main/kotlin/com/baeldung/operators/Money.kt b/core-kotlin/src/main/kotlin/com/baeldung/operators/Money.kt new file mode 100644 index 0000000000..ffaea71e1e --- /dev/null +++ b/core-kotlin/src/main/kotlin/com/baeldung/operators/Money.kt @@ -0,0 +1,27 @@ +enum class Currency { + DOLLARS, EURO +} + +class Money(val amount: BigDecimal, val currency: Currency) : Comparable { + + override fun compareTo(other: Money): Int = + convert(Currency.DOLLARS).compareTo(other.convert(Currency.DOLLARS)) + + fun convert(currency: Currency): BigDecimal = TODO() + + override fun equals(other: Any?): Boolean { + if (this === other) return true + if (other !is Money) return false + + if (amount != other.amount) return false + if (currency != other.currency) return false + + return true + } + + override fun hashCode(): Int { + var result = amount.hashCode() + result = 31 * result + currency.hashCode() + return result + } +} \ No newline at end of file diff --git a/core-kotlin/src/main/kotlin/com/baeldung/operators/Page.kt b/core-kotlin/src/main/kotlin/com/baeldung/operators/Page.kt new file mode 100644 index 0000000000..db03779b35 --- /dev/null +++ b/core-kotlin/src/main/kotlin/com/baeldung/operators/Page.kt @@ -0,0 +1,14 @@ +interface Page { + fun pageNumber(): Int + fun pageSize(): Int + fun elements(): MutableList +} + +operator fun Page.get(index: Int): T = elements()[index] +operator fun Page.get(start: Int, endExclusive: Int): List = elements().subList(start, endExclusive) +operator fun Page.set(index: Int, value: T) { + elements()[index] = value +} + +operator fun Page.contains(element: T): Boolean = element in elements() +operator fun Page.iterator() = elements().iterator() \ No newline at end of file diff --git a/core-kotlin/src/main/kotlin/com/baeldung/operators/Point.kt b/core-kotlin/src/main/kotlin/com/baeldung/operators/Point.kt new file mode 100644 index 0000000000..7af2fc1cad --- /dev/null +++ b/core-kotlin/src/main/kotlin/com/baeldung/operators/Point.kt @@ -0,0 +1,14 @@ +data class Point(val x: Int, val y: Int) + +operator fun Point.unaryMinus() = Point(-x, -y) +operator fun Point.not() = Point(y, x) +operator fun Point.inc() = Point(x + 1, y + 1) +operator fun Point.dec() = Point(x - 1, y - 1) + +operator fun Point.plus(other: Point): Point = Point(x + other.x, y + other.y) +operator fun Point.minus(other: Point): Point = Point(x - other.x, y - other.y) +operator fun Point.times(other: Point): Point = Point(x * other.x, y * other.y) +operator fun Point.div(other: Point): Point = Point(x / other.x, y / other.y) +operator fun Point.rem(other: Point): Point = Point(x % other.x, y % other.y) +operator fun Point.times(factor: Int): Point = Point(x * factor, y * factor) +operator fun Int.times(point: Point): Point = Point(point.x * this, point.y * this) \ No newline at end of file diff --git a/core-kotlin/src/main/kotlin/com/baeldung/operators/UI.kt b/core-kotlin/src/main/kotlin/com/baeldung/operators/UI.kt new file mode 100644 index 0000000000..abb4a4a00c --- /dev/null +++ b/core-kotlin/src/main/kotlin/com/baeldung/operators/UI.kt @@ -0,0 +1,30 @@ +interface View +class TextView(val value: String) : View +class ImageView(val url: String) : View + +class UI { + private val views = mutableListOf() + + operator fun String.unaryPlus() { + views.add(TextView(this)) + } + + operator fun View.unaryPlus() { + views.add(this) + } +} + +fun ui(init: UI.() -> Unit): UI { + val ui = UI() + ui.init() + + return ui +} + +fun main(args: Array) { + val header = ui { + +ImageView("http://logo.com") + +"Brand name" + +"Brand description" + } +} \ No newline at end of file diff --git a/core-kotlin/src/main/kotlin/com/baeldung/operators/Utils.kt b/core-kotlin/src/main/kotlin/com/baeldung/operators/Utils.kt new file mode 100644 index 0000000000..a3c671369c --- /dev/null +++ b/core-kotlin/src/main/kotlin/com/baeldung/operators/Utils.kt @@ -0,0 +1,4 @@ +operator fun MutableCollection.plusAssign(element: T) { + add(element) +} +operator fun BigInteger.plus(other: Int): BigInteger = add(BigInteger("$other")) \ No newline at end of file