Skip to content

Commit ea6aa58

Browse files
authored
Merge pull request eugenp#5800 from alimate/BAEL-2375
BAEL-2375: Operator Overloading in Kotlin
2 parents 97fe04a + ef74a25 commit ea6aa58

7 files changed

Lines changed: 175 additions & 0 deletions

File tree

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.baeldung.operators
2+
3+
import java.math.BigDecimal
4+
5+
enum class Currency {
6+
DOLLARS, EURO
7+
}
8+
9+
class Money(val amount: BigDecimal, val currency: Currency) : Comparable<Money> {
10+
11+
override fun compareTo(other: Money): Int =
12+
convert(Currency.DOLLARS).compareTo(other.convert(Currency.DOLLARS))
13+
14+
fun convert(currency: Currency): BigDecimal = TODO()
15+
16+
override fun equals(other: Any?): Boolean {
17+
if (this === other) return true
18+
if (other !is Money) return false
19+
20+
if (amount != other.amount) return false
21+
if (currency != other.currency) return false
22+
23+
return true
24+
}
25+
26+
override fun hashCode(): Int {
27+
var result = amount.hashCode()
28+
result = 31 * result + currency.hashCode()
29+
return result
30+
}
31+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.baeldung.operators
2+
3+
interface Page<T> {
4+
fun pageNumber(): Int
5+
fun pageSize(): Int
6+
fun elements(): MutableList<T>
7+
}
8+
9+
operator fun <T> Page<T>.get(index: Int): T = elements()[index]
10+
operator fun <T> Page<T>.get(start: Int, endExclusive: Int): List<T> = elements().subList(start, endExclusive)
11+
operator fun <T> Page<T>.set(index: Int, value: T) {
12+
elements()[index] = value
13+
}
14+
15+
operator fun <T> Page<T>.contains(element: T): Boolean = element in elements()
16+
operator fun <T> Page<T>.iterator() = elements().iterator()
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.baeldung.operators
2+
3+
data class Point(val x: Int, val y: Int)
4+
5+
operator fun Point.unaryMinus() = Point(-x, -y)
6+
operator fun Point.not() = Point(y, x)
7+
operator fun Point.inc() = Point(x + 1, y + 1)
8+
operator fun Point.dec() = Point(x - 1, y - 1)
9+
10+
operator fun Point.plus(other: Point): Point = Point(x + other.x, y + other.y)
11+
operator fun Point.minus(other: Point): Point = Point(x - other.x, y - other.y)
12+
operator fun Point.times(other: Point): Point = Point(x * other.x, y * other.y)
13+
operator fun Point.div(other: Point): Point = Point(x / other.x, y / other.y)
14+
operator fun Point.rem(other: Point): Point = Point(x % other.x, y % other.y)
15+
operator fun Point.times(factor: Int): Point = Point(x * factor, y * factor)
16+
operator fun Int.times(point: Point): Point = Point(point.x * this, point.y * this)
17+
18+
class Shape {
19+
val points = mutableListOf<Point>()
20+
21+
operator fun Point.unaryPlus() {
22+
points.add(this)
23+
}
24+
}
25+
26+
fun shape(init: Shape.() -> Unit): Shape {
27+
val shape = Shape()
28+
shape.init()
29+
30+
return shape
31+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.baeldung.operators
2+
3+
import java.math.BigInteger
4+
5+
operator fun <T> MutableCollection<T>.plusAssign(element: T) {
6+
add(element)
7+
}
8+
operator fun BigInteger.plus(other: Int): BigInteger = add(BigInteger("$other"))
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.baeldung.operators
2+
3+
import org.junit.Test
4+
import kotlin.test.assertEquals
5+
import kotlin.test.assertTrue
6+
7+
class PageTest {
8+
9+
private val page = PageImpl(1, 10, "Java", "Kotlin", "Scala")
10+
11+
@Test
12+
fun `Get convention should work as expected`() {
13+
assertEquals(page[1], "Kotlin")
14+
assertEquals(page[1, 3], listOf("Kotlin", "Scala"))
15+
}
16+
17+
@Test
18+
fun `In convention should work on a page as expected`() {
19+
assertTrue("Kotlin" in page)
20+
}
21+
22+
}
23+
24+
private class PageImpl<T>(val page: Int, val size: Int, vararg val elements: T) : Page<T> {
25+
override fun pageNumber(): Int = page
26+
override fun pageSize(): Int = size
27+
override fun elements(): MutableList<T> = mutableListOf(*elements)
28+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.baeldung.operators
2+
3+
import org.junit.Test
4+
import kotlin.test.assertEquals
5+
import kotlin.test.assertTrue
6+
7+
class PointTest {
8+
9+
private val p1 = Point(1, 2)
10+
private val p2 = Point(2, 3)
11+
12+
@Test
13+
fun `We should be able to add two points together using +`() {
14+
assertEquals(p1 + p2, Point(3, 5))
15+
}
16+
17+
@Test
18+
fun `We shoud be able to subtract one point from another using -`() {
19+
assertEquals(p1 - p2, Point(-1, -1))
20+
}
21+
22+
@Test
23+
fun `We should be able to multiply two points together with *`() {
24+
assertEquals(p1 * p2, Point(2, 6))
25+
}
26+
27+
@Test
28+
fun `We should be able to divide one point by another`() {
29+
assertEquals(p1 / p2, Point(0, 0))
30+
}
31+
32+
@Test
33+
fun `We should be able to scale a point by an integral factor`() {
34+
assertEquals(p1 * 2, Point(2, 4))
35+
assertEquals(2 * p1, Point(2, 4))
36+
}
37+
38+
@Test
39+
fun `We should be able to add points to an empty shape`() {
40+
val line = shape {
41+
+Point(0, 0)
42+
+Point(1, 3)
43+
}
44+
45+
assertTrue(Point(0, 0) in line.points)
46+
assertTrue(Point(1, 3) in line.points)
47+
}
48+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung.operators
2+
3+
import java.math.BigInteger
4+
import org.junit.Test
5+
import kotlin.test.assertEquals
6+
7+
class UtilsTest {
8+
9+
@Test
10+
fun `We should be able to add an int value to an existing BigInteger using +`() {
11+
assertEquals(BigInteger.ZERO + 1, BigInteger.ONE)
12+
}
13+
}

0 commit comments

Comments
 (0)