Skip to content

Commit

Permalink
fix/Add list extension (#148)
Browse files Browse the repository at this point in the history
* Add list extension

* Fix test function description

* Update java version

* Revert jdk change.
  • Loading branch information
bruno-garces authored Feb 1, 2021
1 parent 95779e7 commit e67d4bf
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 6 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
language: android
dist: xenial


before_cache:
- rm -f $HOME/.gradle/caches/modules-2/modules-2.lock
- rm -fr $HOME/.gradle/caches/*/plugin-resolution/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
package com.mindera.skeletoid.kt.extensions.utils

fun <T: Any> Collection<T>.nullIfEmpty(): Collection<T>? {
return if (isEmpty()) null else this
}
fun <T> Collection<T>.nullIfEmpty() = if (isEmpty()) null else this
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.mindera.skeletoid.kt.extensions.utils

fun <T> List<T>.nullIfEmpty() = if (isEmpty()) null else this
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ class CollectionTest {

@Test
fun `test nullIfEmpty with non empty collection`() {
val collection = listOf("One", "Two", "Three")
val collection = setOf("One", "Two", "Three")
Assert.assertNotNull(collection.nullIfEmpty())
}

@Test
fun `test nullIfEmpty with empty collection`() {
val collection = listOf<String>()
val collection = setOf<String>()
Assert.assertNull(collection.nullIfEmpty())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.mindera.skeletoid.kt.extensions.utils

import org.junit.Assert
import org.junit.Test

class ListExtTest {

@Test
fun `test nullIfEmpty with non empty list`() {
val list = listOf("One", "Two", "Three")
Assert.assertNotNull(list.nullIfEmpty())
}

@Test
fun `test nullIfEmpty with empty list`() {
val list = listOf<String>()
Assert.assertNull(list.nullIfEmpty())
}
}

0 comments on commit e67d4bf

Please sign in to comment.