From 2ee5e03b887585e5a4f1b4657b5d47583f2984fb Mon Sep 17 00:00:00 2001 From: Ali Dehghani Date: Thu, 22 Nov 2018 23:07:29 +0330 Subject: [PATCH 1/5] 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 From a8d9eb084a9f48d14dd9af46e2f870b12df74c33 Mon Sep 17 00:00:00 2001 From: Ali Dehghani Date: Thu, 22 Nov 2018 23:47:49 +0330 Subject: [PATCH 2/5] Added two missing import statements --- core-kotlin/src/main/kotlin/com/baeldung/operators/Money.kt | 2 ++ core-kotlin/src/main/kotlin/com/baeldung/operators/Utils.kt | 2 ++ 2 files changed, 4 insertions(+) diff --git a/core-kotlin/src/main/kotlin/com/baeldung/operators/Money.kt b/core-kotlin/src/main/kotlin/com/baeldung/operators/Money.kt index ffaea71e1e..1bb015ac27 100644 --- a/core-kotlin/src/main/kotlin/com/baeldung/operators/Money.kt +++ b/core-kotlin/src/main/kotlin/com/baeldung/operators/Money.kt @@ -1,3 +1,5 @@ +import java.math.BigDecimal + enum class Currency { DOLLARS, EURO } diff --git a/core-kotlin/src/main/kotlin/com/baeldung/operators/Utils.kt b/core-kotlin/src/main/kotlin/com/baeldung/operators/Utils.kt index a3c671369c..ba33d3e7a2 100644 --- a/core-kotlin/src/main/kotlin/com/baeldung/operators/Utils.kt +++ b/core-kotlin/src/main/kotlin/com/baeldung/operators/Utils.kt @@ -1,3 +1,5 @@ +import java.math.BigInteger + operator fun MutableCollection.plusAssign(element: T) { add(element) } From 1250605e19850ea102e4275fcc8a882220c82d97 Mon Sep 17 00:00:00 2001 From: Ali Dehghani Date: Sun, 25 Nov 2018 00:51:27 +0330 Subject: [PATCH 3/5] Added a new shape class instead of UI. --- .../kotlin/com/baeldung/operators/Point.kt | 17 ++++++++++- .../main/kotlin/com/baeldung/operators/UI.kt | 30 ------------------- 2 files changed, 16 insertions(+), 31 deletions(-) delete mode 100644 core-kotlin/src/main/kotlin/com/baeldung/operators/UI.kt diff --git a/core-kotlin/src/main/kotlin/com/baeldung/operators/Point.kt b/core-kotlin/src/main/kotlin/com/baeldung/operators/Point.kt index 7af2fc1cad..22617a4ee6 100644 --- a/core-kotlin/src/main/kotlin/com/baeldung/operators/Point.kt +++ b/core-kotlin/src/main/kotlin/com/baeldung/operators/Point.kt @@ -11,4 +11,19 @@ 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 +operator fun Int.times(point: Point): Point = Point(point.x * this, point.y * this) + +class Shape { + private val points = mutableListOf() + + operator fun Point.unaryPlus() { + points.add(this) + } +} + +fun shape(init: Shape.() -> Unit): Shape { + val shape = Shape() + shape.init() + + return shape +} \ 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 deleted file mode 100644 index abb4a4a00c..0000000000 --- a/core-kotlin/src/main/kotlin/com/baeldung/operators/UI.kt +++ /dev/null @@ -1,30 +0,0 @@ -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 From ad8ae556a8a8e0673d8810490447366e5a544584 Mon Sep 17 00:00:00 2001 From: Ali Dehghani Date: Thu, 29 Nov 2018 13:30:52 +0330 Subject: [PATCH 4/5] Added a few unit tests for kotlin operator examples. --- .../kotlin/com/baeldung/operators/Money.kt | 2 + .../kotlin/com/baeldung/operators/Page.kt | 2 + .../kotlin/com/baeldung/operators/Point.kt | 4 +- .../kotlin/com/baeldung/operators/Utils.kt | 2 + .../kotlin/com/baeldung/operators/PageTest.kt | 28 +++++++++++ .../com/baeldung/operators/PointTest.kt | 48 +++++++++++++++++++ 6 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 core-kotlin/src/test/kotlin/com/baeldung/operators/PageTest.kt create mode 100644 core-kotlin/src/test/kotlin/com/baeldung/operators/PointTest.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 index 1bb015ac27..93eb78c5b6 100644 --- a/core-kotlin/src/main/kotlin/com/baeldung/operators/Money.kt +++ b/core-kotlin/src/main/kotlin/com/baeldung/operators/Money.kt @@ -1,3 +1,5 @@ +package com.baeldung.operators + import java.math.BigDecimal enum class Currency { diff --git a/core-kotlin/src/main/kotlin/com/baeldung/operators/Page.kt b/core-kotlin/src/main/kotlin/com/baeldung/operators/Page.kt index db03779b35..1077eb94f9 100644 --- a/core-kotlin/src/main/kotlin/com/baeldung/operators/Page.kt +++ b/core-kotlin/src/main/kotlin/com/baeldung/operators/Page.kt @@ -1,3 +1,5 @@ +package com.baeldung.operators + interface Page { fun pageNumber(): Int fun pageSize(): Int diff --git a/core-kotlin/src/main/kotlin/com/baeldung/operators/Point.kt b/core-kotlin/src/main/kotlin/com/baeldung/operators/Point.kt index 22617a4ee6..e3282e64cc 100644 --- a/core-kotlin/src/main/kotlin/com/baeldung/operators/Point.kt +++ b/core-kotlin/src/main/kotlin/com/baeldung/operators/Point.kt @@ -1,3 +1,5 @@ +package com.baeldung.operators + data class Point(val x: Int, val y: Int) operator fun Point.unaryMinus() = Point(-x, -y) @@ -14,7 +16,7 @@ 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) class Shape { - private val points = mutableListOf() + val points = mutableListOf() operator fun Point.unaryPlus() { points.add(this) diff --git a/core-kotlin/src/main/kotlin/com/baeldung/operators/Utils.kt b/core-kotlin/src/main/kotlin/com/baeldung/operators/Utils.kt index ba33d3e7a2..0f16544f38 100644 --- a/core-kotlin/src/main/kotlin/com/baeldung/operators/Utils.kt +++ b/core-kotlin/src/main/kotlin/com/baeldung/operators/Utils.kt @@ -1,3 +1,5 @@ +package com.baeldung.operators + import java.math.BigInteger operator fun MutableCollection.plusAssign(element: T) { diff --git a/core-kotlin/src/test/kotlin/com/baeldung/operators/PageTest.kt b/core-kotlin/src/test/kotlin/com/baeldung/operators/PageTest.kt new file mode 100644 index 0000000000..4217fc0c08 --- /dev/null +++ b/core-kotlin/src/test/kotlin/com/baeldung/operators/PageTest.kt @@ -0,0 +1,28 @@ +package com.baeldung.operators + +import org.junit.Test +import kotlin.test.assertEquals +import kotlin.test.assertTrue + +class PageTest { + + private val page = PageImpl(1, 10, "Java", "Kotlin", "Scala") + + @Test + fun `Get convention should work as expected`() { + assertEquals(page[1], "Kotlin") + assertEquals(page[1, 3], listOf("Kotlin", "Scala")) + } + + @Test + fun `In convention should work on a page as expected`() { + assertTrue("Kotlin" in page) + } + +} + +private class PageImpl(val page: Int, val size: Int, vararg val elements: T) : Page { + override fun pageNumber(): Int = page + override fun pageSize(): Int = size + override fun elements(): MutableList = mutableListOf(*elements) +} \ No newline at end of file diff --git a/core-kotlin/src/test/kotlin/com/baeldung/operators/PointTest.kt b/core-kotlin/src/test/kotlin/com/baeldung/operators/PointTest.kt new file mode 100644 index 0000000000..168ab6431d --- /dev/null +++ b/core-kotlin/src/test/kotlin/com/baeldung/operators/PointTest.kt @@ -0,0 +1,48 @@ +package com.baeldung.operators + +import org.junit.Test +import kotlin.test.assertEquals +import kotlin.test.assertTrue + +class PointTest { + + private val p1 = Point(1, 2) + private val p2 = Point(2, 3) + + @Test + fun `We should be able to add two points together using +`() { + assertEquals(p1 + p2, Point(3, 5)) + } + + @Test + fun `We shoud be able to subtract one point from another using -`() { + assertEquals(p1 - p2, Point(-1, -1)) + } + + @Test + fun `We should be able to multiply two points together with *`() { + assertEquals(p1 * p2, Point(2, 6)) + } + + @Test + fun `We should be able to divide one point by another`() { + assertEquals(p1 / p2, Point(0, 0)) + } + + @Test + fun `We should be able to scale a point by an integral factor`() { + assertEquals(p1 * 2, Point(2, 4)) + assertEquals(2 * p1, Point(2, 4)) + } + + @Test + fun `We should be able to add points to an empty shape`() { + val line = shape { + +Point(0, 0) + +Point(1, 3) + } + + assertTrue(Point(0, 0) in line.points) + assertTrue(Point(1, 3) in line.points) + } +} \ No newline at end of file From ef74a2538e126eec8bec6e5b81d6ecd570454c12 Mon Sep 17 00:00:00 2001 From: Ali Dehghani Date: Thu, 29 Nov 2018 15:57:06 +0330 Subject: [PATCH 5/5] Added a few unit tests for kotlin operator examples. --- .../test/kotlin/com/baeldung/operators/UtilsTest.kt | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 core-kotlin/src/test/kotlin/com/baeldung/operators/UtilsTest.kt diff --git a/core-kotlin/src/test/kotlin/com/baeldung/operators/UtilsTest.kt b/core-kotlin/src/test/kotlin/com/baeldung/operators/UtilsTest.kt new file mode 100644 index 0000000000..4abe962cb5 --- /dev/null +++ b/core-kotlin/src/test/kotlin/com/baeldung/operators/UtilsTest.kt @@ -0,0 +1,13 @@ +package com.baeldung.operators + +import java.math.BigInteger +import org.junit.Test +import kotlin.test.assertEquals + +class UtilsTest { + + @Test + fun `We should be able to add an int value to an existing BigInteger using +`() { + assertEquals(BigInteger.ZERO + 1, BigInteger.ONE) + } +} \ No newline at end of file