Polish IndexedElementsBinder

This commit is contained in:
Phillip Webb 2018-02-02 13:12:26 -08:00
parent 69234f8c08
commit 60d525e732
3 changed files with 24 additions and 17 deletions

View File

@ -38,12 +38,12 @@ class ArrayBinder extends IndexedElementsBinder<Object> {
@Override
protected Object bindAggregate(ConfigurationPropertyName name, Bindable<?> target,
AggregateElementBinder elementBinder) {
IndexedCollectionSupplier collection = new IndexedCollectionSupplier(
ArrayList::new);
IndexedCollectionSupplier result = new IndexedCollectionSupplier(ArrayList::new);
ResolvableType aggregateType = target.getType();
ResolvableType elementType = target.getType().getComponentType();
bindIndexed(name, elementBinder, collection, target.getType(), elementType);
if (collection.wasSupplied()) {
List<Object> list = (List<Object>) collection.get();
bindIndexed(name, elementBinder, aggregateType, elementType, result);
if (result.wasSupplied()) {
List<Object> list = (List<Object>) result.get();
Object array = Array.newInstance(elementType.resolve(), list.size());
for (int i = 0; i < list.size(); i++) {
Array.set(array, i, list.get(i));

View File

@ -40,14 +40,14 @@ class CollectionBinder extends IndexedElementsBinder<Collection<Object>> {
AggregateElementBinder elementBinder) {
Class<?> collectionType = (target.getValue() == null ? target.getType().resolve()
: List.class);
IndexedCollectionSupplier collection = new IndexedCollectionSupplier(
() -> CollectionFactory.createCollection(collectionType, 0));
ResolvableType elementType = target.getType().asCollection().getGeneric();
ResolvableType aggregateType = ResolvableType.forClassWithGenerics(List.class,
target.getType().asCollection().getGenerics());
bindIndexed(name, elementBinder, collection, aggregateType, elementType);
if (collection.wasSupplied()) {
return collection.get();
ResolvableType elementType = target.getType().asCollection().getGeneric();
IndexedCollectionSupplier result = new IndexedCollectionSupplier(
() -> CollectionFactory.createCollection(collectionType, 0));
bindIndexed(name, elementBinder, aggregateType, elementType, result);
if (result.wasSupplied()) {
return result.get();
}
return null;
}

View File

@ -53,13 +53,20 @@ abstract class IndexedElementsBinder<T> extends AggregateBinder<T> {
return source == null || source instanceof IterableConfigurationPropertySource;
}
/**
* Bind indexed elements to the supplied collection.
* @param name The name of the property to bind
* @param elementBinder the binder to use for elements
* @param aggregateType the aggregate type, may be a collection or an array
* @param elementType the element type
* @param result the destination for results
*/
protected final void bindIndexed(ConfigurationPropertyName name,
AggregateElementBinder elementBinder, IndexedCollectionSupplier collection,
ResolvableType aggregateType, ResolvableType elementType) {
AggregateElementBinder elementBinder, ResolvableType aggregateType,
ResolvableType elementType, IndexedCollectionSupplier result) {
for (ConfigurationPropertySource source : getContext().getSources()) {
bindIndexed(source, name, elementBinder, collection, aggregateType,
elementType);
if (collection.wasSupplied() && collection.get() != null) {
bindIndexed(source, name, elementBinder, result, aggregateType, elementType);
if (result.wasSupplied() && result.get() != null) {
return;
}
}
@ -134,7 +141,7 @@ abstract class IndexedElementsBinder<T> extends AggregateBinder<T> {
}
/**
* {@link AggregateBinder.AggregateSupplier AggregateSupplier} for an index
* {@link AggregateBinder.AggregateSupplier AggregateSupplier} for an indexed
* collection.
*/
protected static class IndexedCollectionSupplier