Skip to content

Commit

Permalink
extended CollectionHelper by isEmpty test
Browse files Browse the repository at this point in the history
  • Loading branch information
rouven-walter committed Jan 23, 2020
1 parent a3249d2 commit 7e3f4ff
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
10 changes: 10 additions & 0 deletions src/main/java/org/logicng/util/CollectionHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ private CollectionHelper() {
// Intentionally left empty
}

/**
* Null safe test whether the given collection is empty, i.e. tests if the collection is {@code null} or empty.
* @param collection the collection, may be {@code null}
* @param <T> the type of the elements in the collection, may be arbitrary
* @return {@code true} if the collection is {@code null} or empty, otherwise {@code false}
*/
public static <T> boolean isEmpty(final Collection<T> collection) {
return collection == null || collection.isEmpty();
}

/**
* Null safe wrapper for collections. Returns an (unmodifiable!) empty list if the given collection is {@code
* null}, otherwise returns the given list unchanged.
Expand Down
15 changes: 12 additions & 3 deletions src/test/java/org/logicng/util/CollectionHelperTest.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package org.logicng.util;

import static org.assertj.core.api.Assertions.assertThat;
import static org.logicng.util.CollectionHelper.nullSafe;

import org.junit.jupiter.api.Test;

import java.util.ArrayList;
Expand All @@ -12,13 +9,25 @@
import java.util.SortedSet;
import java.util.TreeSet;

import static org.assertj.core.api.Assertions.assertThat;
import static org.logicng.util.CollectionHelper.isEmpty;
import static org.logicng.util.CollectionHelper.nullSafe;

/**
* Unit tests for {@link CollectionHelper}
* @version 2.0.0
* @since 2.0.0
*/
public class CollectionHelperTest {

@Test
public void testIsEmpty() {
assertThat(isEmpty(null)).isTrue();
assertThat(isEmpty(Collections.emptySet())).isTrue();
assertThat(isEmpty(Collections.emptyMap().entrySet())).isTrue();
assertThat(isEmpty(Collections.singletonList(2))).isFalse();
}

@Test
public void testNullSafe() {
assertThat(nullSafe(null)).isEmpty();
Expand Down

0 comments on commit 7e3f4ff

Please sign in to comment.