Skip to content

Commit db81fd9

Browse files
authored
[KTLN-763] Add samples (#1079)
* [KTLN-763] Add samples * [KTLN-763] Add samples
1 parent 704b04b commit db81fd9

File tree

4 files changed

+88
-0
lines changed

4 files changed

+88
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<artifactId>core-kotlin-lang-5</artifactId>
7+
<name>core-kotlin-lang-5</name>
8+
<packaging>jar</packaging>
9+
10+
<parent>
11+
<groupId>com.baeldung</groupId>
12+
<artifactId>core-kotlin-modules</artifactId>
13+
<version>1.0.0-SNAPSHOT</version>
14+
</parent>
15+
16+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung.nullable.comparable
2+
3+
fun compareTwoInts(a: Int?, b: Int?): Int? {
4+
return a?.compareTo(b ?: return null)
5+
}
6+
7+
fun Int?.isGreaterThan(other: Int?): Boolean? {
8+
return this?.let { it > (other ?: return null) }
9+
}
10+
11+
fun <T : Comparable<T>> T?.isGreaterThan(other: T?): Boolean? {
12+
return this?.let { it > (other ?: return null) }
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.baeldung.nullable.comparable
2+
3+
import org.junit.jupiter.api.Assertions.assertEquals
4+
import org.junit.jupiter.api.Test
5+
6+
class NullableComparableUnitTest {
7+
@Test
8+
fun `compare nullable integers using compareTo`() {
9+
val a: Int? = 5
10+
val b: Int? = 3
11+
val c: Int? = null
12+
assertEquals(0, compareTwoInts(a, a))
13+
assertEquals(1, compareTwoInts(a, b))
14+
assertEquals(-1, compareTwoInts(b, a))
15+
assertEquals(null, compareTwoInts(a, c))
16+
assertEquals(null, compareTwoInts(c, b))
17+
}
18+
19+
@Test fun `compare nullable integers using extension function`() {
20+
val a: Int? = 5
21+
val b: Int? = 3
22+
val c: Int? = null
23+
assertEquals(true, a.isGreaterThan(b))
24+
assertEquals(false, b.isGreaterThan(a))
25+
assertEquals(null, c.isGreaterThan(a))
26+
assertEquals(null, c.isGreaterThan(c))
27+
}
28+
29+
@Test fun `compare generic Int comparable types using extension function`() {
30+
val a: Int? = 5
31+
val b: Int? = 3
32+
val c: Int? = null
33+
assertEquals(true, a.isGreaterThan(b))
34+
assertEquals(false, b.isGreaterThan(a))
35+
assertEquals(false, b.isGreaterThan(b))
36+
assertEquals(null, c.isGreaterThan(b))
37+
}
38+
39+
@Test fun `compare generic String comparable types using extension function`() {
40+
val a: String? = "ABC"
41+
val b: String? = "ZXY"
42+
val c: String? = null
43+
assertEquals(true, b.isGreaterThan(a))
44+
assertEquals(false, a.isGreaterThan(b))
45+
assertEquals(false, b.isGreaterThan(b))
46+
assertEquals(null, c.isGreaterThan(b))
47+
}
48+
49+
@Test fun `compare nullable integers using compareValues function`() {
50+
val a: Int? = 5
51+
val b: Int? = 3
52+
val c: Int? = null
53+
assertEquals(1, compareValues(a, b))
54+
assertEquals(-1, compareValues(b, a))
55+
assertEquals(1, compareValues(a, c))
56+
assertEquals(-1, compareValues(c, a))
57+
assertEquals(0, compareValues(c, c)) }
58+
}

core-kotlin-modules/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
<module>core-kotlin-lang-2</module>
5656
<module>core-kotlin-lang-3</module>
5757
<module>core-kotlin-lang-4</module>
58+
<module>core-kotlin-lang-5</module>
5859
<module>core-kotlin-lang-scope</module>
5960
<module>core-kotlin-lang-oop</module>
6061
<module>core-kotlin-lang-oop-2</module>

0 commit comments

Comments
 (0)