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..93eb78c5b6 --- /dev/null +++ b/core-kotlin/src/main/kotlin/com/baeldung/operators/Money.kt @@ -0,0 +1,31 @@ +package com.baeldung.operators + +import java.math.BigDecimal + +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..1077eb94f9 --- /dev/null +++ b/core-kotlin/src/main/kotlin/com/baeldung/operators/Page.kt @@ -0,0 +1,16 @@ +package com.baeldung.operators + +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..e3282e64cc --- /dev/null +++ b/core-kotlin/src/main/kotlin/com/baeldung/operators/Point.kt @@ -0,0 +1,31 @@ +package com.baeldung.operators + +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) + +class Shape { + 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/Utils.kt b/core-kotlin/src/main/kotlin/com/baeldung/operators/Utils.kt new file mode 100644 index 0000000000..0f16544f38 --- /dev/null +++ b/core-kotlin/src/main/kotlin/com/baeldung/operators/Utils.kt @@ -0,0 +1,8 @@ +package com.baeldung.operators + +import java.math.BigInteger + +operator fun MutableCollection.plusAssign(element: T) { + add(element) +} +operator fun BigInteger.plus(other: Int): BigInteger = add(BigInteger("$other")) \ No newline at end of file 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 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