Skip to content

Commit

Permalink
Add isNotEmpty method in CollectionUtils (and tests)
Browse files Browse the repository at this point in the history
  • Loading branch information
flyrev authored and tipsy committed Apr 4, 2018
1 parent 030e9d0 commit 9c5cab7
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 4 deletions.
11 changes: 11 additions & 0 deletions src/main/java/spark/utils/CollectionUtils.java
Expand Up @@ -42,4 +42,15 @@ public static boolean isEmpty(Collection<?> collection) {
return (collection == null || collection.isEmpty());
}

/**
* Return {@code true} if the supplied Collection is not {@code null} and not empty.
* Otherwise, return {@code false}.
*
* @param collection the Collection to check
* @return whether the given Collection is not empty
*/
public static boolean isNotEmpty(Collection<?> collection) {
return !isEmpty(collection);
}

}
47 changes: 43 additions & 4 deletions src/test/java/spark/utils/CollectionUtilsTest.java
Expand Up @@ -10,21 +10,60 @@
public class CollectionUtilsTest {

@Test
public void testIsEmpty_whenCollectionIsEmpty_thenReturnTrue() throws Exception {
public void testIsEmpty_whenCollectionIsEmpty_thenReturnTrue() {

Collection<Object> testCollection = new ArrayList<>();

assertTrue("Should return true because collection is empty", CollectionUtils.isEmpty(testCollection));

}

@Test
public void testIsEmpty_whenCollectionIsNotEmpty_thenReturnFalse() throws Exception {
public void testIsEmpty_whenCollectionIsNotEmpty_thenReturnFalse() {

Collection<Integer> testCollection = new ArrayList<>();
testCollection.add(1);
testCollection.add(2);

assertFalse("Should return false because collection is empty", CollectionUtils.isEmpty(testCollection));
assertFalse("Should return false because collection is not empty", CollectionUtils.isEmpty(testCollection));

}

@Test
public void testIsEmpty_whenCollectionIsNull_thenReturnTrue() {

Collection<Integer> testCollection = null;

assertTrue("Should return true because collection is null", CollectionUtils.isEmpty(testCollection));

}

@Test
public void testIsNotEmpty_whenCollectionIsEmpty_thenReturnFalse() {

Collection<Object> testCollection = new ArrayList<>();

assertFalse("Should return false because collection is empty", CollectionUtils.isNotEmpty(testCollection));

}

@Test
public void testIsNotEmpty_whenCollectionIsNotEmpty_thenReturnTrue() {

Collection<Integer> testCollection = new ArrayList<>();
testCollection.add(1);
testCollection.add(2);

assertTrue("Should return true because collection is not empty", CollectionUtils.isNotEmpty(testCollection));

}

@Test
public void testIsNotEmpty_whenCollectionIsNull_thenReturnFalse() {

Collection<Object> testCollection = null;

assertFalse("Should return false because collection is null", CollectionUtils.isNotEmpty(testCollection));

}
}
}

0 comments on commit 9c5cab7

Please sign in to comment.