From 9b39f51635636783a7b4e6f09f5b16d32a547beb Mon Sep 17 00:00:00 2001 From: Oliver Drotbohm Date: Tue, 4 Dec 2018 12:23:17 +0100 Subject: [PATCH] =?UTF-8?q?DATACMNS-1432=20-=20Added=20Streamable.toList()?= =?UTF-8?q?=20and=20=E2=80=A6.toSet()=20for=20convenience.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../springframework/data/util/Streamable.java | 20 +++++++++ .../data/util/StreamableUnitTests.java | 41 +++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 src/test/java/org/springframework/data/util/StreamableUnitTests.java diff --git a/src/main/java/org/springframework/data/util/Streamable.java b/src/main/java/org/springframework/data/util/Streamable.java index d489083b6..a64e33c23 100644 --- a/src/main/java/org/springframework/data/util/Streamable.java +++ b/src/main/java/org/springframework/data/util/Streamable.java @@ -146,6 +146,26 @@ public interface Streamable extends Iterable, Supplier> { return Streamable.of(() -> Stream.concat(this.stream(), stream.get())); } + /** + * Creates a new, unmodifiable {@link List}. + * + * @return will never be {@literal null}. + * @since 2.2 + */ + default List toList() { + return stream().collect(StreamUtils.toUnmodifiableList()); + } + + /** + * Creates a new, unmodifiable {@link Set}. + * + * @return will never be {@literal null}. + * @since 2.2 + */ + default Set toSet() { + return stream().collect(StreamUtils.toUnmodifiableSet()); + } + /* * (non-Javadoc) * @see java.util.function.Supplier#get() diff --git a/src/test/java/org/springframework/data/util/StreamableUnitTests.java b/src/test/java/org/springframework/data/util/StreamableUnitTests.java new file mode 100644 index 000000000..b6715a158 --- /dev/null +++ b/src/test/java/org/springframework/data/util/StreamableUnitTests.java @@ -0,0 +1,41 @@ +/* + * Copyright 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.util; + +import static org.assertj.core.api.Assertions.*; + +import java.util.Arrays; +import java.util.stream.Stream; + +import org.junit.Test; + +/** + * Unit tests for {@link Streamable} + * + * @author Oliver Gierke + * @soundtrack The Intersphere - Antitype (The Grand Delusion) + */ +public class StreamableUnitTests { + + @Test // DATACMNS-1432 + public void collectsToCollections() { + + Streamable streamable = Streamable.of(() -> Stream.of(1, 2, 1)); + + assertThat(streamable.toList()).containsExactly(1, 2, 1); + assertThat(streamable.toSet()).containsExactlyInAnyOrder(1, 2); + } +}