Consistent Iterator/Stream support in PropertySources and PropertyValues
Issue: SPR-16894
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
* Copyright 2002-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.
|
||||
@@ -18,7 +18,10 @@ package org.springframework.core.env;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Spliterator;
|
||||
import java.util.Spliterators;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
@@ -26,7 +29,7 @@ import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.lang.Nullable;
|
||||
|
||||
/**
|
||||
* Default implementation of the {@link PropertySources} interface.
|
||||
* The default implementation of the {@link PropertySources} interface.
|
||||
* Allows manipulation of contained property sources and provides a constructor
|
||||
* for copying an existing {@code PropertySources} instance.
|
||||
*
|
||||
@@ -73,6 +76,21 @@ public class MutablePropertySources implements PropertySources {
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Iterator<PropertySource<?>> iterator() {
|
||||
return this.propertySourceList.iterator();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Spliterator<PropertySource<?>> spliterator() {
|
||||
return Spliterators.spliterator(this.propertySourceList, 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Stream<PropertySource<?>> stream() {
|
||||
return this.propertySourceList.stream();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean contains(String name) {
|
||||
return this.propertySourceList.contains(PropertySource.named(name));
|
||||
@@ -85,10 +103,6 @@ public class MutablePropertySources implements PropertySources {
|
||||
return (index != -1 ? this.propertySourceList.get(index) : null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<PropertySource<?>> iterator() {
|
||||
return this.propertySourceList.iterator();
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the given property source object with highest precedence.
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2011 the original author or authors.
|
||||
* Copyright 2002-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.
|
||||
@@ -16,16 +16,29 @@
|
||||
|
||||
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 Juergen Hoeller
|
||||
* @since 3.1
|
||||
* @see PropertySource
|
||||
*/
|
||||
public interface PropertySources extends Iterable<PropertySource<?>> {
|
||||
|
||||
/**
|
||||
* Return a sequential {@link Stream} containing the property sources.
|
||||
* @since 5.1
|
||||
*/
|
||||
default Stream<PropertySource<?>> stream() {
|
||||
return StreamSupport.stream(spliterator(), false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return whether a property source with the given name is contained.
|
||||
* @param name the {@linkplain PropertySource#getName() name of the property source} to find
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
* Copyright 2002-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.
|
||||
@@ -16,6 +16,8 @@
|
||||
|
||||
package org.springframework.core.env;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.mock.env.MockPropertySource;
|
||||
@@ -46,9 +48,9 @@ public class MutablePropertySourcesTests {
|
||||
assertThat(sources.contains("g"), is(false));
|
||||
|
||||
assertThat(sources.get("b"), not(nullValue()));
|
||||
assertThat(sources.get("b").getProperty("p1"), equalTo((Object)"bValue"));
|
||||
assertThat(sources.get("b").getProperty("p1"), equalTo("bValue"));
|
||||
assertThat(sources.get("d"), not(nullValue()));
|
||||
assertThat(sources.get("d").getProperty("p1"), equalTo((Object)"dValue"));
|
||||
assertThat(sources.get("d").getProperty("p1"), equalTo("dValue"));
|
||||
|
||||
sources.addBefore("b", new MockPropertySource("a"));
|
||||
sources.addAfter("b", new MockPropertySource("c"));
|
||||
@@ -153,4 +155,48 @@ public class MutablePropertySourcesTests {
|
||||
assertThat(sources.get("bogus"), nullValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void iteratorContainsPropertySource() {
|
||||
MutablePropertySources sources = new MutablePropertySources();
|
||||
sources.addLast(new MockPropertySource("test"));
|
||||
|
||||
Iterator<PropertySource<?>> it = sources.iterator();
|
||||
assertTrue(it.hasNext());
|
||||
assertEquals("test", it.next().getName());
|
||||
|
||||
try {
|
||||
it.remove();
|
||||
fail("Should have thrown UnsupportedOperationException");
|
||||
}
|
||||
catch (UnsupportedOperationException ex) {
|
||||
// expected
|
||||
}
|
||||
assertFalse(it.hasNext());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void iteratorIsEmptyForEmptySources() {
|
||||
MutablePropertySources sources = new MutablePropertySources();
|
||||
Iterator<PropertySource<?>> it = sources.iterator();
|
||||
assertFalse(it.hasNext());
|
||||
}
|
||||
|
||||
@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 -> "test".equals(source.getName())), is(true));
|
||||
assertThat(sources.stream().anyMatch(source -> "bogus".equals(source.getName())), is(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void streamIsEmptyForEmptySources() {
|
||||
MutablePropertySources sources = new MutablePropertySources();
|
||||
assertThat(sources.stream(), notNullValue());
|
||||
assertThat(sources.stream().count(), is(0L));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user