Polish IndexedElementsBinder
This commit is contained in:
parent
69234f8c08
commit
60d525e732
|
@ -38,12 +38,12 @@ class ArrayBinder extends IndexedElementsBinder<Object> {
|
||||||
@Override
|
@Override
|
||||||
protected Object bindAggregate(ConfigurationPropertyName name, Bindable<?> target,
|
protected Object bindAggregate(ConfigurationPropertyName name, Bindable<?> target,
|
||||||
AggregateElementBinder elementBinder) {
|
AggregateElementBinder elementBinder) {
|
||||||
IndexedCollectionSupplier collection = new IndexedCollectionSupplier(
|
IndexedCollectionSupplier result = new IndexedCollectionSupplier(ArrayList::new);
|
||||||
ArrayList::new);
|
ResolvableType aggregateType = target.getType();
|
||||||
ResolvableType elementType = target.getType().getComponentType();
|
ResolvableType elementType = target.getType().getComponentType();
|
||||||
bindIndexed(name, elementBinder, collection, target.getType(), elementType);
|
bindIndexed(name, elementBinder, aggregateType, elementType, result);
|
||||||
if (collection.wasSupplied()) {
|
if (result.wasSupplied()) {
|
||||||
List<Object> list = (List<Object>) collection.get();
|
List<Object> list = (List<Object>) result.get();
|
||||||
Object array = Array.newInstance(elementType.resolve(), list.size());
|
Object array = Array.newInstance(elementType.resolve(), list.size());
|
||||||
for (int i = 0; i < list.size(); i++) {
|
for (int i = 0; i < list.size(); i++) {
|
||||||
Array.set(array, i, list.get(i));
|
Array.set(array, i, list.get(i));
|
||||||
|
|
|
@ -40,14 +40,14 @@ class CollectionBinder extends IndexedElementsBinder<Collection<Object>> {
|
||||||
AggregateElementBinder elementBinder) {
|
AggregateElementBinder elementBinder) {
|
||||||
Class<?> collectionType = (target.getValue() == null ? target.getType().resolve()
|
Class<?> collectionType = (target.getValue() == null ? target.getType().resolve()
|
||||||
: List.class);
|
: List.class);
|
||||||
IndexedCollectionSupplier collection = new IndexedCollectionSupplier(
|
|
||||||
() -> CollectionFactory.createCollection(collectionType, 0));
|
|
||||||
ResolvableType elementType = target.getType().asCollection().getGeneric();
|
|
||||||
ResolvableType aggregateType = ResolvableType.forClassWithGenerics(List.class,
|
ResolvableType aggregateType = ResolvableType.forClassWithGenerics(List.class,
|
||||||
target.getType().asCollection().getGenerics());
|
target.getType().asCollection().getGenerics());
|
||||||
bindIndexed(name, elementBinder, collection, aggregateType, elementType);
|
ResolvableType elementType = target.getType().asCollection().getGeneric();
|
||||||
if (collection.wasSupplied()) {
|
IndexedCollectionSupplier result = new IndexedCollectionSupplier(
|
||||||
return collection.get();
|
() -> CollectionFactory.createCollection(collectionType, 0));
|
||||||
|
bindIndexed(name, elementBinder, aggregateType, elementType, result);
|
||||||
|
if (result.wasSupplied()) {
|
||||||
|
return result.get();
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
|
@ -53,13 +53,20 @@ abstract class IndexedElementsBinder<T> extends AggregateBinder<T> {
|
||||||
return source == null || source instanceof IterableConfigurationPropertySource;
|
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,
|
protected final void bindIndexed(ConfigurationPropertyName name,
|
||||||
AggregateElementBinder elementBinder, IndexedCollectionSupplier collection,
|
AggregateElementBinder elementBinder, ResolvableType aggregateType,
|
||||||
ResolvableType aggregateType, ResolvableType elementType) {
|
ResolvableType elementType, IndexedCollectionSupplier result) {
|
||||||
for (ConfigurationPropertySource source : getContext().getSources()) {
|
for (ConfigurationPropertySource source : getContext().getSources()) {
|
||||||
bindIndexed(source, name, elementBinder, collection, aggregateType,
|
bindIndexed(source, name, elementBinder, result, aggregateType, elementType);
|
||||||
elementType);
|
if (result.wasSupplied() && result.get() != null) {
|
||||||
if (collection.wasSupplied() && collection.get() != null) {
|
|
||||||
return;
|
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.
|
* collection.
|
||||||
*/
|
*/
|
||||||
protected static class IndexedCollectionSupplier
|
protected static class IndexedCollectionSupplier
|
||||||
|
|
Loading…
Reference in New Issue