-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add SetUtils for creating ordered sets
- Loading branch information
1 parent
67f1314
commit a74fbb8
Showing
3 changed files
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package de.cronn.commons.lang; | ||
|
||
import java.util.Arrays; | ||
import java.util.LinkedHashSet; | ||
import java.util.Set; | ||
|
||
public final class SetUtils { | ||
|
||
private SetUtils() { | ||
} | ||
|
||
@SafeVarargs | ||
@SuppressWarnings("varargs") | ||
public static <E> Set<E> orderedSet(E... elements) { | ||
return new LinkedHashSet<>(Arrays.asList(elements)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package de.cronn.commons.lang; | ||
|
||
import static org.assertj.core.api.Assertions.*; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
class SetUtilsTest { | ||
|
||
@Test | ||
void testOrderedSet() { | ||
assertThat(SetUtils.orderedSet("abc")).containsExactly("abc"); | ||
assertThat(SetUtils.orderedSet("abc", "def", "ghi")).containsExactly("abc", "def", "ghi"); | ||
assertThat(SetUtils.orderedSet(2, 1, 3)).containsExactly(2, 1, 3); | ||
assertThat(SetUtils.orderedSet(2, 1, 3, 1)).containsExactly(2, 1, 3); | ||
assertThat(SetUtils.orderedSet(null, "abc")).containsExactly(null, "abc"); | ||
assertThat(SetUtils.orderedSet((Object) null)).singleElement().isNull(); | ||
} | ||
|
||
} |