Consistent Iterator/Stream support in PropertySources and PropertyValues

Issue: SPR-16894
This commit is contained in:
Juergen Hoeller
2018-07-05 23:59:56 +02:00
parent bb3061d112
commit d8c7270c00
7 changed files with 194 additions and 18 deletions

View File

@@ -18,16 +18,21 @@ package org.springframework.beans;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.stream.Stream;
import org.springframework.lang.Nullable;
import org.springframework.util.StringUtils;
/**
* Default implementation of the {@link PropertyValues} interface.
* The default implementation of the {@link PropertyValues} interface.
* Allows simple manipulation of properties, and provides constructors
* to support deep copy and construction from a Map.
*
@@ -244,6 +249,21 @@ public class MutablePropertyValues implements PropertyValues, Serializable {
}
@Override
public Iterator<PropertyValue> iterator() {
return Collections.unmodifiableList(this.propertyValueList).iterator();
}
@Override
public Spliterator<PropertyValue> spliterator() {
return Spliterators.spliterator(this.propertyValueList, 0);
}
@Override
public Stream<PropertyValue> stream() {
return this.propertyValueList.stream();
}
@Override
public PropertyValue[] getPropertyValues() {
return this.propertyValueList.toArray(new PropertyValue[0]);

View File

@@ -16,6 +16,13 @@
package org.springframework.beans;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import org.springframework.lang.Nullable;
/**
@@ -27,7 +34,33 @@ import org.springframework.lang.Nullable;
* @since 13 May 2001
* @see PropertyValue
*/
public interface PropertyValues {
public interface PropertyValues extends Iterable<PropertyValue> {
/**
* Return an {@link Iterator} over the property values.
* @since 5.1
*/
@Override
default Iterator<PropertyValue> iterator() {
return Arrays.asList(getPropertyValues()).iterator();
}
/**
* Return a {@link Spliterator} over the property values.
* @since 5.1
*/
@Override
default Spliterator<PropertyValue> spliterator() {
return Spliterators.spliterator(getPropertyValues(), 0);
}
/**
* Return a sequential {@link Stream} containing the property values.
* @since 5.1
*/
default Stream<PropertyValue> stream() {
return StreamSupport.stream(spliterator(), false);
}
/**
* Return an array of the PropertyValue objects held in this object.