-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Null extensions + int/char/long extensions (#14)
- Loading branch information
1 parent
c7c4f49
commit db4bbcb
Showing
17 changed files
with
628 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ plugins { | |
} | ||
|
||
group = "xyz.lbres" | ||
version = "0.2.1" | ||
version = "0.3.0" | ||
|
||
repositories { | ||
mavenCentral() | ||
|
19 changes: 19 additions & 0 deletions
19
kotlin-utils/src/main/kotlin/xyz/lbres/kotlinutils/char/ext/CharExt.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package xyz.lbres.kotlinutils.char.ext | ||
|
||
import xyz.lbres.kotlinutils.general.ternaryIf | ||
import xyz.lbres.kotlinutils.int.ext.isZero | ||
|
||
/** | ||
* Returns this number if not zero, or the result of calling [getDefaultValue] if it is. | ||
* | ||
* @param getDefaultValue () -> [Char] | ||
* @return [Char] the current value, or the default | ||
*/ | ||
fun Char.ifZero(getDefaultValue: () -> Char): Char = ternaryIf(isZero(), getDefaultValue(), this) | ||
|
||
/** | ||
* Unary check to determine if value is zero | ||
* | ||
* @return [Boolean]: true if value is zero, false otherwise | ||
*/ | ||
fun Char.isZero(): Boolean = code.isZero() |
30 changes: 30 additions & 0 deletions
30
kotlin-utils/src/main/kotlin/xyz/lbres/kotlinutils/collection/char/ext/CharCollectionExt.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package xyz.lbres.kotlinutils.collection.char.ext | ||
|
||
import xyz.lbres.kotlinutils.char.ext.isZero | ||
|
||
/** | ||
* Filter a char collection to contain only elements that do not equal zero. | ||
* | ||
* @return [Collection]<[Char]>: collection containing the same values as [this], except any elements with value 0. | ||
*/ | ||
fun Collection<Char>.filterNotZero(): Collection<Char> = filterNot { it.isZero() } | ||
|
||
/** | ||
* Add all values in collection. | ||
* | ||
* @return [Char]: sum of numbers in collection | ||
*/ | ||
fun Collection<Char>.sum(): Char = fold(Char(0)) { acc, char -> acc + char.code } | ||
|
||
/** | ||
* Multiply all values in collection | ||
* | ||
* @return [Char]: product of numbers in collection, or 0 if collection is empty | ||
*/ | ||
fun Collection<Char>.product(): Char { | ||
if (isEmpty()) { | ||
return Char(0) | ||
} | ||
|
||
return fold(1) { acc, char -> acc * char.code }.toChar() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
kotlin-utils/src/main/kotlin/xyz/lbres/kotlinutils/collection/int/ext/IntCollectionExt.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package xyz.lbres.kotlinutils.collection.int.ext | ||
|
||
import xyz.lbres.kotlinutils.general.ternaryIf | ||
import xyz.lbres.kotlinutils.int.ext.isZero | ||
|
||
/** | ||
* Filter an integer collection to contain only elements that do not equal zero. | ||
* | ||
* @return [Collection]<[Int]>: collection containing the same values as [this], except any elements with value 0. | ||
*/ | ||
fun Collection<Int>.filterNotZero(): Collection<Int> = filterNot { it.isZero() } | ||
|
||
/** | ||
* Add all values in collection. | ||
* | ||
* @return [Int]: sum of numbers in collection | ||
*/ | ||
fun Collection<Int>.sum(): Int = fold(0, Int::plus) | ||
|
||
/** | ||
* Multiply all values in collection | ||
* | ||
* @return [Int]: product of numbers in collection, or 0 if collection is empty | ||
*/ | ||
fun Collection<Int>.product(): Int = ternaryIf(isEmpty(), 0, fold(1, Int::times)) |
25 changes: 25 additions & 0 deletions
25
kotlin-utils/src/main/kotlin/xyz/lbres/kotlinutils/collection/long/ext/LongCollectionExt.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package xyz.lbres.kotlinutils.collection.long.ext | ||
|
||
import xyz.lbres.kotlinutils.general.ternaryIf | ||
import xyz.lbres.kotlinutils.long.ext.isZero | ||
|
||
/** | ||
* Filter a long collection to contain only elements that do not equal zero. | ||
* | ||
* @return [Collection]<[Long]>: collection containing the same values as [this], except any elements with value 0. | ||
*/ | ||
fun Collection<Long>.filterNotZero(): Collection<Long> = filterNot { it.isZero() } | ||
|
||
/** | ||
* Add all values in collection. | ||
* | ||
* @return [Long]: sum of numbers in collection | ||
*/ | ||
fun Collection<Long>.sum(): Long = fold(0, Long::plus) | ||
|
||
/** | ||
* Multiply all values in collection | ||
* | ||
* @return [Long]: product of numbers in collection, or 0 if collection is empty | ||
*/ | ||
fun Collection<Long>.product(): Long = ternaryIf(isEmpty(), 0, fold(1, Long::times)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 4 additions & 8 deletions
12
kotlin-utils/src/main/kotlin/xyz/lbres/kotlinutils/int/ext/IntExt.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
kotlin-utils/src/main/kotlin/xyz/lbres/kotlinutils/long/ext/LongExt.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package xyz.lbres.kotlinutils.long.ext | ||
|
||
import xyz.lbres.kotlinutils.general.ternaryIf | ||
|
||
/** | ||
* Returns this number if not zero, or the result of calling [getDefaultValue] if it is. | ||
* | ||
* @param getDefaultValue () -> [Long] | ||
* @return [Long] the current value, or the default | ||
*/ | ||
fun Long.ifZero(getDefaultValue: () -> Long): Long = ternaryIf(isZero(), getDefaultValue(), this) | ||
|
||
/** | ||
* Unary check to determine if value is zero | ||
* | ||
* @return [Boolean]: true if value is zero, false otherwise | ||
*/ | ||
fun Long.isZero(): Boolean = equals(0L) | ||
|
||
/** | ||
* Unary check to determine if value is negative | ||
* | ||
* @return [Boolean]: true if value is less than zero, false otherwise | ||
*/ | ||
fun Long.isNegative(): Boolean = this < 0L |
37 changes: 37 additions & 0 deletions
37
kotlin-utils/src/test/kotlin/xyz/lbres/kotlinutils/char/ext/CharExtTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package xyz.lbres.kotlinutils.char.ext | ||
|
||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertFalse | ||
import kotlin.test.assertTrue | ||
|
||
internal class CharExtTest { | ||
@Test | ||
internal fun testIfZero() { | ||
val getValue = { Char(2) } | ||
|
||
var char = Char(0) | ||
var expected = Char(2) | ||
assertEquals(expected, char.ifZero(getValue)) | ||
|
||
char = Char(2) | ||
expected = Char(2) | ||
assertEquals(expected, char.ifZero(getValue)) | ||
|
||
char = Char(15) | ||
expected = Char(15) | ||
assertEquals(expected, char.ifZero(getValue)) | ||
} | ||
|
||
@Test | ||
internal fun testIsZero() { | ||
var char = Char(0) | ||
assertTrue(char.isZero()) | ||
|
||
char = Char(1) | ||
assertFalse(char.isZero()) | ||
|
||
char = Char(100) | ||
assertFalse(char.isZero()) | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
...-utils/src/test/kotlin/xyz/lbres/kotlinutils/collection/char/ext/CharCollectionExtTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package xyz.lbres.kotlinutils.collection.char.ext | ||
|
||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
internal class CharCollectionExtTest { | ||
private val zero = Char(0) | ||
private val one = Char(1) | ||
private val four = Char(4) | ||
private val five = Char(5) | ||
|
||
@Test | ||
internal fun testFilterNotZero() { | ||
var list: List<Char> = listOf() | ||
var expected: List<Char> = listOf() | ||
assertEquals(expected, list.filterNotZero()) | ||
|
||
list = listOf(zero, zero, zero) | ||
expected = listOf() | ||
assertEquals(expected, list.filterNotZero()) | ||
|
||
list = listOf(one, Char(2), zero, four, zero, zero, five) | ||
expected = listOf(one, Char(2), four, five) | ||
assertEquals(expected, list.filterNotZero()) | ||
|
||
list = listOf(one, four, Char(1000), Char(19), five) | ||
expected = listOf(one, four, Char(1000), Char(19), five) | ||
assertEquals(expected, list.filterNotZero()) | ||
} | ||
|
||
@Test | ||
internal fun testSum() { | ||
var list: List<Char> = emptyList() | ||
var expected = zero | ||
assertEquals(expected, list.sum()) | ||
|
||
list = listOf(Char(33)) | ||
expected = Char(33) | ||
assertEquals(expected, list.sum()) | ||
|
||
list = listOf(Char(100), Char(45), Char(10), Char(67), Char(99)) | ||
expected = Char(321) | ||
assertEquals(expected, list.sum()) | ||
} | ||
|
||
@Test | ||
internal fun testProduct() { | ||
var list: List<Char> = emptyList() | ||
var expected = zero | ||
assertEquals(expected, list.product()) | ||
|
||
list = listOf(zero) | ||
expected = zero | ||
assertEquals(expected, list.product()) | ||
|
||
list = listOf(one) | ||
expected = one | ||
assertEquals(expected, list.product()) | ||
|
||
list = listOf(four, four, zero) | ||
expected = zero | ||
assertEquals(expected, list.product()) | ||
|
||
list = listOf(Char(15), Char(23), Char(4), Char(4)) | ||
expected = Char(5520) | ||
assertEquals(expected, list.product()) | ||
} | ||
} |
Oops, something went wrong.