BAEL-4583-Generic-arrays-streams

This commit is contained in:
Ashley Frieze 2021-03-21 23:22:54 +00:00
parent ce9f112ed6
commit 99bb67a413
3 changed files with 83 additions and 1 deletions

View File

@ -24,6 +24,12 @@
<artifactId>jmh-generator-annprocess</artifactId> <artifactId>jmh-generator-annprocess</artifactId>
<version>${jmh.version}</version> <version>${jmh.version}</version>
</dependency> </dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.19.0</version>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
<properties> <properties>

View File

@ -2,7 +2,6 @@ package com.baeldung.genericarrays;
import org.junit.Test; import org.junit.Test;
import java.util.ArrayList;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;

View File

@ -0,0 +1,77 @@
package com.baeldung.genericarrays;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.IntFunction;
import java.util.stream.Stream;
import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
public class StreamToArrayUnitTest {
@Test
public void givenAStream_thenCanGetArrayOfObject() {
Object[] strings = Stream.of("A", "AAA", "B", "AAB", "C")
.filter(string -> string.startsWith("A"))
.toArray();
assertThat(strings).containsExactly("A", "AAA", "AAB");
assertThat(strings).isNotInstanceOf(String[].class);
}
@Test
public void givenAStream_thenCanGetArrayOfString() {
String[] strings = Stream.of("A", "AAA", "B", "AAB", "C")
.filter(string -> string.startsWith("A"))
.toArray(String[]::new);
assertThat(strings).containsExactly("A", "AAA", "AAB");
assertThat(strings).isInstanceOf(String[].class);
}
@SuppressWarnings("unchecked")
@Test
public void givenAStream_whenConvertToOptional_thenCanGetArrayOfOptional() {
Stream<Optional<String>> stream = Stream.of("A", "AAA", "B", "AAB", "C")
.filter(string -> string.startsWith("A"))
.map(Optional::of);
Optional<String>[] strings = stream
.toArray(Optional[]::new);
assertThat(strings).containsExactly(Optional.of("A"),
Optional.of("AAA"),
Optional.of("AAB"));
assertThat(strings).isInstanceOf(Optional[].class);
}
@Test
public void givenAStream_whenConvertToOptional_thenCanGetArrayOfOptionalWithHelper() {
Optional<String>[] strings = Stream.of("A", "AAA", "B", "AAB", "C")
.filter(string -> string.startsWith("A"))
.map(Optional::of)
.toArray(genericArray(Optional[]::new));
assertThat(strings).containsExactly(Optional.of("A"),
Optional.of("AAA"),
Optional.of("AAB"));
assertThat(strings).isInstanceOf(Optional[].class);
}
@Test
public void whenInvalidUseOfGenericArray_thenIllegalCast() {
assertThatThrownBy(() -> {
ArrayList<String>[] lists = Stream.of(singletonList("A"))
.toArray(genericArray(List[]::new));
}).isInstanceOf(ClassCastException.class);
}
@SuppressWarnings("unchecked")
private static <T, R extends T> IntFunction<R[]> genericArray(IntFunction<T[]> arrayCreator) {
return size -> (R[])arrayCreator.apply(size);
}
}