Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add distinctOrEmpty to KiwiLists; internal refactoring #756

Merged
merged 2 commits into from
Jul 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions src/main/java/org/kiwiproject/collect/KiwiLists.java
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ public static <T> T nth(List<T> items, int number) {
*/
public static <T> List<T> distinct(Collection<T> collection) {
checkArgument(nonNull(collection), "collection can not be null");
return distinctOrNull(collection);
return distinctListFrom(collection);
}

/**
Expand All @@ -248,7 +248,23 @@ public static <T> List<T> distinct(Collection<T> collection) {
* @return a new list with only unique elements or null.
*/
public static <T> List<T> distinctOrNull(Collection<T> collection) {
return nonNull(collection) ? collection.stream().distinct().collect(toList()) : null;
return nonNull(collection) ? distinctListFrom(collection) : null;
}

/**
* Returns a list of the collection elements with duplicates stripped out or an empty list if the input collection
* is null (or empty).
*
* @param collection the collection of values
* @param <T> the type of items in the collection
* @return a new list with only unique elements or an empty list
*/
public static <T> List<T> distinctOrEmpty(Collection<T> collection) {
return nonNull(collection) ? distinctListFrom(collection) : new ArrayList<>();
}

private static <T> List<T> distinctListFrom(Collection<T> collection) {
return collection.stream().distinct().collect(toList());
}

/**
Expand Down
30 changes: 30 additions & 0 deletions src/test/java/org/kiwiproject/collect/KiwiListsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.NullAndEmptySource;
import org.junit.jupiter.params.provider.ValueSource;
import org.kiwiproject.junit.jupiter.WhiteBoxTest;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
import java.util.stream.IntStream;
import java.util.stream.Stream;

Expand Down Expand Up @@ -342,6 +344,7 @@ class Distinct {

@Test
void shouldReturnDistinct(SoftAssertions softly) {
softly.assertThat(KiwiLists.distinct(newArrayList())).isEmpty();
softly.assertThat(KiwiLists.distinct(newArrayList(1, 2, 3))).hasSize(3);
softly.assertThat(KiwiLists.distinct(newArrayList(1, 1, 1))).hasSize(1);
softly.assertThat(KiwiLists.distinct(newArrayList("a", "b", "b"))).hasSize(2);
Expand All @@ -362,6 +365,33 @@ class DistinctOrNull {
void shouldAllowNull() {
assertThat(KiwiLists.distinctOrNull(null)).isNull();
}

@Test
void shouldReturnEmptyList_GivenEmptyList() {
assertThat(KiwiLists.distinctOrNull(List.of())).isEmpty();
}

@Test
void shouldReturnDistinctList() {
assertThat(KiwiLists.distinctOrNull(List.of(42, 42, 42, 84, 126, 126, 336, 336)))
.containsExactly(42, 84, 126, 336);
}
}

@Nested
class DistinctOrEmpty {

@ParameterizedTest
@NullAndEmptySource
void shouldReturnEmptyList_GivenNullOrEmptyCollection(Set<String> strings) {
assertThat(KiwiLists.distinctOrEmpty(strings)).isEmpty();
}

@Test
void shouldReturnDistinctList() {
assertThat(KiwiLists.distinctOrEmpty(List.of(42, 42, 84, 84, 126, 336, 336)))
.containsExactly(42, 84, 126, 336);
}
}

@Nested
Expand Down