Skip to content

Commit

Permalink
Null extensions + int/char/long extensions (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
lbressler13 authored Sep 27, 2022
1 parent c7c4f49 commit db4bbcb
Show file tree
Hide file tree
Showing 17 changed files with 628 additions and 34 deletions.
2 changes: 1 addition & 1 deletion kotlin-utils/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

group = "xyz.lbres"
version = "0.2.1"
version = "0.3.0"

repositories {
mavenCentral()
Expand Down
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()
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()
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import xyz.lbres.kotlinutils.classes.multiset.MultiSet
import xyz.lbres.kotlinutils.classes.multiset.MultiSetImpl
import xyz.lbres.kotlinutils.classes.multiset.MutableMultiSet
import xyz.lbres.kotlinutils.classes.multiset.MutableMultiSetImpl
import xyz.lbres.kotlinutils.int.ext.isZero
import xyz.lbres.kotlinutils.generic.ext.isNotNull
import xyz.lbres.kotlinutils.generic.ext.isNull

/**
* Create a MultiSet with the elements in the current collection.
Expand All @@ -21,8 +22,23 @@ fun <E> Collection<E>.toMultiSet(): MultiSet<E> = MultiSetImpl(this)
fun <E> Collection<E>.toMutableMultiSet(): MutableMultiSet<E> = MutableMultiSetImpl(this)

/**
* Filter an integer collection to contain only elements that do not equal zero.
* Count number of elements in the collection that are null.
*
* @return [Collection<Int>]: collection containing the same values as [this], except any elements with value 0.
* @return [Int]
*/
fun Collection<Int>.filterNotZero() = filterNot { it.isZero() }
fun <E> Collection<E?>.countNull(): Int = count { it.isNull() }

/**
* Count number of elements in the collection that are not null.
*
* @return [Int]
*/
fun <E> Collection<E?>.countNotNull(): Int = count { it.isNotNull() }

/**
* Check if a collection is not null and is not empty.
* Combines separate calls for checking if the collection is null and if it is empty.
*
* @return [Boolean]: true if the collection is not null and has size > 0, false otherwise
*/
fun <E> Collection<E>?.isNotNullOrEmpty() = !this.isNullOrEmpty()
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))
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))
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,17 @@ package xyz.lbres.kotlinutils.generic.ext
* @return [T] the current value, or the default
*/
fun <T> T?.ifNull(getDefaultValue: () -> T): T = this ?: getDefaultValue()

/**
* Returns true if a value is null, or false otherwise
*
* @return [Boolean]
*/
fun <T> T?.isNull(): Boolean = this == null

/**
* Returns true if a value is not null, or false otherwise
*
* @return [Boolean]
*/
fun <T> T?.isNotNull(): Boolean = this != null
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
package xyz.lbres.kotlinutils.int.ext

import xyz.lbres.kotlinutils.general.ternaryIf

/**
* Returns this number if not zero, or the result of calling [getDefaultValue] if it is.
*
* @param getDefaultValue [() -> Int]
* @param getDefaultValue () -> [Int]
* @return [Int] the current value, or the default
*/
fun Int.ifZero(getDefaultValue: () -> Int): Int {
return if (equals(0)) {
getDefaultValue()
} else {
this
}
}
fun Int.ifZero(getDefaultValue: () -> Int): Int = ternaryIf(isZero(), getDefaultValue(), this)

/**
* Unary check to determine if value is zero
Expand Down
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
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())
}
}
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())
}
}
Loading

0 comments on commit db4bbcb

Please sign in to comment.