Skip to content

Commit

Permalink
runtime-core: Rename combination method
Browse files Browse the repository at this point in the history
  • Loading branch information
minborg committed Apr 17, 2017
1 parent b2b4dcd commit 33476c6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
Expand Up @@ -31,7 +31,7 @@ public final class Combination {
/** /**
* Creates and returns all possible combinations of the given elements. * Creates and returns all possible combinations of the given elements.
* *
* The order of the combinations is unspecified. * The order of the combinations in the stream is unspecified.
* *
* @param <T> element type * @param <T> element type
* @param items to combine * @param items to combine
Expand All @@ -57,23 +57,24 @@ public static <T> Stream<List<T>> of(final T... items) {


/** /**
* Creates and returns all possible combinations of the given elements * Creates and returns all possible combinations of the given elements
* whereby each element only occur one time. Elements are checked for * whereby each element only occurs at most once in any given List. Elements
* occurrence by its identity, not equality. * are checked for occurrence by its {@code equals() } method.
* *
* The order of the combinations is unspecified. * The order of the combinations in the stream is unspecified.
* *
* @param <T> element type * @param <T> element type
* @param items to combine * @param items to combine
* @return all possible combinations of the given elements * @return all possible combinations of the given elements whereby each
* element only occurs at most once in any given List
*/ */
@SafeVarargs @SafeVarargs
@SuppressWarnings("varargs") // Creating a List from an array is safe @SuppressWarnings("varargs") // Creating a List from an array is safe
public static <T> Stream<List<T>> ofUniqueIdentity(final T... items) { public static <T> Stream<List<T>> ofDistinct(final T... items) {
return of(items).filter(Combination::isUniqueIdentityElements); return of(items).filter(Combination::isDistinctElements);
} }


private static <T> boolean isUniqueIdentityElements(List<T> list) { private static <T> boolean isDistinctElements(List<T> list) {
final Set<T> discovered = Collections.newSetFromMap(new IdentityHashMap<>()); final Set<T> discovered = Collections.newSetFromMap(new HashMap<>());
return list.stream().allMatch(discovered::add); return list.stream().allMatch(discovered::add);
} }


Expand Down
Expand Up @@ -118,7 +118,7 @@ public void testOfUniqueEmpty() {


@Test @Test
public void testOfUniqueSingleton() { public void testOfUniqueSingleton() {
final List<List<String>> actual = Combination.ofUniqueIdentity("a") final List<List<String>> actual = Combination.ofDistinct("a")
.collect(toList()); .collect(toList());


assertEquals( assertEquals(
Expand All @@ -131,7 +131,7 @@ public void testOfUniqueSingleton() {


@Test @Test
public void testUniqueOfTwo() { public void testUniqueOfTwo() {
final Set<List<String>> actual = Combination.ofUniqueIdentity("a", "b") final Set<List<String>> actual = Combination.ofDistinct("a", "b")
.collect(toSet()); .collect(toSet());


final Set<List<String>> expected = Arrays.asList( final Set<List<String>> expected = Arrays.asList(
Expand All @@ -147,7 +147,7 @@ public void testUniqueOfTwo() {


@Test @Test
public void testOfUniqueThree() { public void testOfUniqueThree() {
Set<List<String>> actual = Combination.ofUniqueIdentity("a", "b", "c") Set<List<String>> actual = Combination.ofDistinct("a", "b", "c")
.collect(toSet()); .collect(toSet());


final Set<List<String>> expected = Arrays.asList( final Set<List<String>> expected = Arrays.asList(
Expand Down

0 comments on commit 33476c6

Please sign in to comment.