diff --git a/spring-core/src/main/java/org/springframework/core/env/PropertySources.java b/spring-core/src/main/java/org/springframework/core/env/PropertySources.java index b4b0d7432e82..83c32960125f 100644 --- a/spring-core/src/main/java/org/springframework/core/env/PropertySources.java +++ b/spring-core/src/main/java/org/springframework/core/env/PropertySources.java @@ -16,12 +16,16 @@ package org.springframework.core.env; +import java.util.stream.Stream; +import java.util.stream.StreamSupport; + import org.springframework.lang.Nullable; /** * Holder containing one or more {@link PropertySource} objects. * * @author Chris Beams + * @author Christoph Dreis * @since 3.1 */ public interface PropertySources extends Iterable> { @@ -39,4 +43,12 @@ public interface PropertySources extends Iterable> { @Nullable PropertySource get(String name); + /** + * Returns a sequential {@code Stream} for the {@link PropertySource objects} + * contained in this instance. + */ + default Stream> stream() { + return StreamSupport.stream(spliterator(), false); + } + } diff --git a/spring-core/src/test/java/org/springframework/core/env/MutablePropertySourcesTests.java b/spring-core/src/test/java/org/springframework/core/env/MutablePropertySourcesTests.java index 7ec6d966d908..663c7362d0b0 100644 --- a/spring-core/src/test/java/org/springframework/core/env/MutablePropertySourcesTests.java +++ b/spring-core/src/test/java/org/springframework/core/env/MutablePropertySourcesTests.java @@ -26,6 +26,7 @@ /** * @author Chris Beams * @author Juergen Hoeller + * @author Christoph Dreis */ public class MutablePropertySourcesTests { @@ -153,4 +154,23 @@ public void getNonExistentPropertySourceReturnsNull() { assertThat(sources.get("bogus"), nullValue()); } + @Test + public void streamContainsPropertySource() { + MutablePropertySources sources = new MutablePropertySources(); + sources.addLast(new MockPropertySource("test")); + assertThat(sources.stream(), notNullValue()); + assertThat(sources.stream().count(), is(1L)); + assertThat(sources.stream().anyMatch(source -> + source.getName().equals("test")), is(true)); + assertThat(sources.stream().anyMatch(source -> + source.getName().equals("bogus")), is(false)); + } + + @Test + public void streamIsNotNullForEmptySources() { + MutablePropertySources sources = new MutablePropertySources(); + assertThat(sources.stream(), notNullValue()); + assertThat(sources.stream().count(), is(0L)); + } + }