Merge pull request #13724 from dreis2211:property-sources-stream

* pr/13724:
  Use PropertySources.stream() where possible
pull/13545/merge
Stephane Nicoll 6 years ago
commit 6fdc5d0eee

@ -22,7 +22,6 @@ import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.databind.SerializationFeature;
@ -156,8 +155,7 @@ public class EnvironmentEndpointDocumentationTests
@Override @Override
protected void customizePropertySources( protected void customizePropertySources(
MutablePropertySources propertySources) { MutablePropertySources propertySources) {
StreamSupport environment.getPropertySources().stream()
.stream(environment.getPropertySources().spliterator(), false)
.filter(this::includedPropertySource) .filter(this::includedPropertySource)
.forEach(propertySources::addLast); .forEach(propertySources::addLast);
} }

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2017 the original author or authors. * Copyright 2012-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -20,7 +20,6 @@ import java.util.Arrays;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
import org.springframework.core.env.PropertySource; import org.springframework.core.env.PropertySource;
import org.springframework.core.env.PropertySources; import org.springframework.core.env.PropertySources;
@ -42,8 +41,7 @@ final class CompositePropertySources implements PropertySources {
@Override @Override
public Iterator<PropertySource<?>> iterator() { public Iterator<PropertySource<?>> iterator() {
return this.propertySources.stream() return this.propertySources.stream().flatMap(PropertySources::stream)
.flatMap((sources) -> StreamSupport.stream(sources.spliterator(), false))
.collect(Collectors.toList()).iterator(); .collect(Collectors.toList()).iterator();
} }

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2017 the original author or authors. * Copyright 2012-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -20,7 +20,6 @@ import java.util.Arrays;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
import java.util.Set; import java.util.Set;
import java.util.stream.StreamSupport;
import org.springframework.core.env.PropertySource; import org.springframework.core.env.PropertySource;
import org.springframework.core.env.PropertySources; import org.springframework.core.env.PropertySources;
@ -59,8 +58,7 @@ final class FilteredPropertySources implements PropertySources {
@Override @Override
public Iterator<PropertySource<?>> iterator() { public Iterator<PropertySource<?>> iterator() {
return StreamSupport.stream(this.delegate.spliterator(), false) return this.delegate.stream().filter(this::included).iterator();
.filter(this::included).iterator();
} }
private boolean included(PropertySource<?> propertySource) { private boolean included(PropertySource<?> propertySource) {

@ -18,13 +18,13 @@ package org.springframework.boot.context.properties.source;
import java.util.Collections; import java.util.Collections;
import java.util.stream.Stream; import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
import org.springframework.core.env.MutablePropertySources; import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource; import org.springframework.core.env.PropertySource;
import org.springframework.core.env.PropertySource.StubPropertySource; import org.springframework.core.env.PropertySource.StubPropertySource;
import org.springframework.core.env.PropertySources;
import org.springframework.core.env.PropertySourcesPropertyResolver; import org.springframework.core.env.PropertySourcesPropertyResolver;
import org.springframework.util.Assert; import org.springframework.util.Assert;
@ -136,9 +136,8 @@ public final class ConfigurationPropertySources {
} }
private static Stream<PropertySource<?>> streamPropertySources( private static Stream<PropertySource<?>> streamPropertySources(
Iterable<PropertySource<?>> sources) { PropertySources sources) {
return StreamSupport.stream(sources.spliterator(), false) return sources.stream().flatMap(ConfigurationPropertySources::flatten)
.flatMap(ConfigurationPropertySources::flatten)
.filter(ConfigurationPropertySources::isIncluded); .filter(ConfigurationPropertySources::isIncluded);
} }

@ -16,11 +16,9 @@
package org.springframework.boot.diagnostics.analyzer; package org.springframework.boot.diagnostics.analyzer;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import org.springframework.boot.context.properties.source.ConfigurationPropertySources; import org.springframework.boot.context.properties.source.ConfigurationPropertySources;
import org.springframework.boot.context.properties.source.InvalidConfigurationPropertyValueException; import org.springframework.boot.context.properties.source.InvalidConfigurationPropertyValueException;
@ -74,9 +72,10 @@ class InvalidConfigurationPropertyValueFailureAnalyzer
} }
private Stream<PropertySource<?>> getPropertySources() { private Stream<PropertySource<?>> getPropertySources() {
Iterable<PropertySource<?>> sources = (this.environment != null if (this.environment == null) {
? this.environment.getPropertySources() : Collections.emptyList()); return Stream.empty();
return StreamSupport.stream(sources.spliterator(), false) }
return this.environment.getPropertySources().stream()
.filter((source) -> !ConfigurationPropertySources .filter((source) -> !ConfigurationPropertySources
.isAttachedConfigurationPropertySource(source)); .isAttachedConfigurationPropertySource(source));
} }

@ -20,7 +20,6 @@ import java.util.Collection;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.stream.StreamSupport;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
@ -92,9 +91,8 @@ public class SpringApplicationJsonEnvironmentPostProcessor
public void postProcessEnvironment(ConfigurableEnvironment environment, public void postProcessEnvironment(ConfigurableEnvironment environment,
SpringApplication application) { SpringApplication application) {
MutablePropertySources propertySources = environment.getPropertySources(); MutablePropertySources propertySources = environment.getPropertySources();
StreamSupport.stream(propertySources.spliterator(), false) propertySources.stream().map(JsonPropertyValue::get).filter(Objects::nonNull)
.map(JsonPropertyValue::get).filter(Objects::nonNull).findFirst() .findFirst().ifPresent((v) -> processJson(environment, v));
.ifPresent((v) -> processJson(environment, v));
} }
private void processJson(ConfigurableEnvironment environment, private void processJson(ConfigurableEnvironment environment,

Loading…
Cancel
Save