@Test public void shouldConvertToJavaStream() { final Value<Integer> value = of(1, 2, 3); final java.util.stream.Stream<Integer> s1 = value.toJavaStream(); if (value.isSingleValued()) { final java.util.stream.Stream<Integer> s2 = java.util.stream.Stream.of(1); assertThat(List.ofAll(s1::iterator)).isEqualTo(List.ofAll(s2::iterator)); } else { final java.util.stream.Stream<Integer> s2 = java.util.stream.Stream.of(1, 2, 3); assertThat(List.ofAll(s1::iterator)).isEqualTo(List.ofAll(s2::iterator)); } }
@Test public void shouldConvertNonEmptyValueToMatchValue() { final Value<Integer> value = of(1); final String actual = value.match().when((Value<Integer> v) -> v.getOrElse(-1) == 1).then("ok").getOrElse("nok"); assertThat(actual).isEqualTo("ok"); }
@Test public void shouldPerformsActionOnEachElement() { final int[] consumer = new int[1]; final Value<Integer> value = of(1, 2, 3); value.forEach(i -> consumer[0] += i); assertThat(consumer[0]).isEqualTo(value.isSingleValued() ? 1 : 6); }
@Test public void shouldBeAwareOfExistingElement() { final Value<Integer> value = of(1, 2); if (value.isSingleValued()) { assertThat(value.exists(i -> i == 1)).isTrue(); } else { assertThat(value.exists(i -> i == 2)).isTrue(); } }
@Test public void shouldConvertToJavaMap() { final Value<Integer> value = of(1, 2, 3); final java.util.Map<Integer, Integer> map = value.toJavaMap(v -> Tuple.of(v, v)); if (value.isSingleValued()) { assertThat(map).isEqualTo(JavaCollections.javaMap(1, 1)); } else { assertThat(map).isEqualTo(JavaCollections.javaMap(1, 1, 2, 2, 3, 3)); } }
@Test public void shouldConvertToSet() { final Value<Integer> value = of(1, 2, 3); final Set<Integer> set = value.toSet(); if (value.isSingleValued()) { assertThat(set).isEqualTo(HashSet.of(1)); } else { assertThat(set).isEqualTo(HashSet.of(1, 2, 3)); } }
@Test public void shouldConvertToJavaSet() { final Value<Integer> value = of(1, 2, 3); final java.util.Set<Integer> set = value.toJavaSet(); if (value.isSingleValued()) { assertThat(set).isEqualTo(JavaCollections.javaSet(1)); } else { assertThat(set).isEqualTo(JavaCollections.javaSet(1, 2, 3)); } }
@Test public void shouldConvertToJavaArray() { final Value<Integer> value = of(1, 2, 3); final Integer[] ints = value.toJavaArray(Integer.class); if (value.isSingleValued()) { assertThat(ints).isEqualTo(new int[] {1}); } else { assertThat(ints).isEqualTo(new int[] {1, 2, 3}); } }
@Test public void shouldConvertToJavaList() { final Value<Integer> value = of(1, 2, 3); final java.util.List<Integer> list = value.toJavaList(); if (value.isSingleValued()) { assertThat(list).isEqualTo(Arrays.asList(1)); } else { assertThat(list).isEqualTo(Arrays.asList(1, 2, 3)); } }
@Test public void shouldConvertToStream() { final Value<Integer> value = of(1, 2, 3); final Stream<Integer> stream = value.toStream(); if (value.isSingleValued()) { assertThat(stream).isEqualTo(Stream.of(1)); } else { assertThat(stream).isEqualTo(Stream.of(1, 2, 3)); } }
@Test public void shouldConvertToVector() { final Value<Integer> value = of(1, 2, 3); final Vector<Integer> vector = value.toVector(); if (value.isSingleValued()) { assertThat(vector).isEqualTo(Vector.of(1)); } else { assertThat(vector).isEqualTo(Vector.of(1, 2, 3)); } }
@Test public void shouldConvertToCharSeq() { Value<Character> v = of('a', 'b', 'c'); assertThat( Match.of(v) .whenTypeIn(Iterator.class) .then(Iterator.of("ignore").toString()) .getOrElse(v.toString())) .isEqualTo(v.toCharSeq().toString()); }
@Test public void shouldConvertToQueue() { final Value<Integer> value = of(1, 2, 3); final Queue<Integer> queue = value.toQueue(); if (value.isSingleValued()) { assertThat(queue).isEqualTo(Queue.of(1)); } else { assertThat(queue).isEqualTo(Queue.of(1, 2, 3)); } }
@Test public void shouldConvertToMap() { final Value<Integer> value = of(1, 2, 3); final Map<Integer, Integer> map = value.toMap(v -> Tuple.of(v, v)); if (value.isSingleValued()) { assertThat(map).isEqualTo(HashMap.of(1, 1)); } else { assertThat(map).isEqualTo(HashMap.empty().put(1, 1).put(2, 2).put(3, 3)); } }
@Test public void shouldConvertToList() { final Value<Integer> value = of(1, 2, 3); final List<Integer> list = value.toList(); if (value.isSingleValued()) { assertThat(list).isEqualTo(List.of(1)); } else { assertThat(list).isEqualTo(List.of(1, 2, 3)); } }
@Test public void shouldConvertToArray() { final Value<Integer> value = of(1, 2, 3); final Array<Integer> array = value.toArray(); if (value.isSingleValued()) { assertThat(array).isEqualTo(Array.of(1)); } else { assertThat(array).isEqualTo(Array.of(1, 2, 3)); } }
@Test public void shouldConvertEmptyValueToMatchValue() { final Value<Integer> value = empty(); final String actual = value.match().when(Value<Integer>::isEmpty).then("ok").getOrElse("nok"); assertThat(actual).isEqualTo("ok"); }