Merge pull request #5800 from alimate/BAEL-2375
BAEL-2375: Operator Overloading in Kotlin
This commit is contained in:
commit
ea6aa58fdf
|
@ -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<Money> {
|
||||
|
||||
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
|
||||
}
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package com.baeldung.operators
|
||||
|
||||
interface Page<T> {
|
||||
fun pageNumber(): Int
|
||||
fun pageSize(): Int
|
||||
fun elements(): MutableList<T>
|
||||
}
|
||||
|
||||
operator fun <T> Page<T>.get(index: Int): T = elements()[index]
|
||||
operator fun <T> Page<T>.get(start: Int, endExclusive: Int): List<T> = elements().subList(start, endExclusive)
|
||||
operator fun <T> Page<T>.set(index: Int, value: T) {
|
||||
elements()[index] = value
|
||||
}
|
||||
|
||||
operator fun <T> Page<T>.contains(element: T): Boolean = element in elements()
|
||||
operator fun <T> Page<T>.iterator() = elements().iterator()
|
|
@ -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<Point>()
|
||||
|
||||
operator fun Point.unaryPlus() {
|
||||
points.add(this)
|
||||
}
|
||||
}
|
||||
|
||||
fun shape(init: Shape.() -> Unit): Shape {
|
||||
val shape = Shape()
|
||||
shape.init()
|
||||
|
||||
return shape
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package com.baeldung.operators
|
||||
|
||||
import java.math.BigInteger
|
||||
|
||||
operator fun <T> MutableCollection<T>.plusAssign(element: T) {
|
||||
add(element)
|
||||
}
|
||||
operator fun BigInteger.plus(other: Int): BigInteger = add(BigInteger("$other"))
|
|
@ -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<T>(val page: Int, val size: Int, vararg val elements: T) : Page<T> {
|
||||
override fun pageNumber(): Int = page
|
||||
override fun pageSize(): Int = size
|
||||
override fun elements(): MutableList<T> = mutableListOf(*elements)
|
||||
}
|
|
@ -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)
|
||||
}
|
||||
}
|
|
@ -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)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue