Last active
October 27, 2018 16:03
-
-
Save loddar/ebb95222c0be6d96a896eb7405c72bcd to your computer and use it in GitHub Desktop.
How to handle state with java stream?
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
package org.failearly.fp.learn; | |
import org.failearly.xquest.Question; | |
import org.junit.jupiter.api.Test; | |
import java.util.List; | |
import java.util.Map; | |
import static org.hamcrest.MatcherAssert.assertThat; | |
import static org.hamcrest.Matchers.is; | |
@Question("How to handle state with java stream?") | |
class HandleStateInStreamTest { | |
@Test | |
void typicalWithTwoTokens() { | |
// given | |
var values = List.of("<a>", "val0", "<b>", "val2", "val3"); | |
// when | |
final Map<String, List<String>> result = groupBy(values); | |
// then | |
assertThat("Grouped by <a> and <b>?", result, is( | |
Map.of( | |
"<a>", List.of("val0"), | |
"<b>", List.of("val2", "val3")) | |
) | |
); | |
} | |
@Question("What if the value list end with a token?") | |
@Test | |
void endsWithEmptyToken() { | |
// given | |
var values = List.of("<a>", "val0", "<b>", "val2", "val3","<c>"); | |
// when | |
final Map<String, List<String>> result = groupBy(values); | |
// then | |
assertThat("Grouped by <a>, <b> and empty <c>?", result, is( | |
Map.of( | |
"<a>", List.of("val0"), | |
"<b>", List.of("val2", "val3"), | |
"<c>", List.of() | |
) | |
) | |
); | |
} | |
@Question("What if the list does not start with an token?") | |
@Test | |
void theFirstNoneTokenValuesWillBeIgnored() { | |
// given | |
var values = List.of("value w/o token","<a>", "val0", "<b>", "val2", "val3"); | |
// when | |
final Map<String, List<String>> result = groupBy(values); | |
// then | |
assertThat("Values will be ignored?", result, is( | |
Map.of( | |
"<a>", List.of("val0"), | |
"<b>", List.of("val2", "val3")) | |
) | |
); | |
} | |
@Question("What if there are no values (thus empty list)?") | |
@Test | |
void emptyCase() { | |
// given | |
var values = List.<String>of(); | |
// when | |
final Map<String, List<String>> result = groupBy(values); | |
// then | |
assertThat("Also empty map?", result, is(Map.of())); | |
} | |
@Question("What if there just one token?") | |
@Test | |
void mapWithOneEntry() { | |
// given | |
var values = List.of("<a>"); | |
// when | |
final Map<String, List<String>> result = groupBy(values); | |
// then | |
assertThat("Map is not empty, but only one entry with empty list?", result, is(Map.of("<a>", List.of()))); | |
} | |
@Question("What if there just two tokens?") | |
@Test | |
void mapWithTwoEntries() { | |
// given | |
var values = List.of("<a>","<b>"); | |
// when | |
final Map<String, List<String>> result = groupBy(values); | |
// then | |
assertThat("Does the map has two entries?", result, is( | |
Map.of( | |
"<a>", List.of(), | |
"<b>", List.of()) | |
) | |
); | |
} | |
private Map<String, List<String>> groupBy(final List<String> values) { | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment