org.springframework.boot.context.properties.bind.AggregateBinder Maven / Gradle / Ivy
/*
* Copyright 2012-2019 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.context.properties.bind;
import java.util.function.Supplier;
import org.springframework.boot.context.properties.bind.Binder.Context;
import org.springframework.boot.context.properties.source.ConfigurationPropertyName;
import org.springframework.boot.context.properties.source.ConfigurationPropertySource;
/**
* Internal strategy used by {@link Binder} to bind aggregates (Maps, Lists, Arrays).
*
* @param the type being bound
* @author Phillip Webb
* @author Madhura Bhave
*/
abstract class AggregateBinder {
private final Context context;
AggregateBinder(Context context) {
this.context = context;
}
/**
* Determine if recursive binding is supported.
* @param source the configuration property source or {@code null} for all sources.
* @return if recursive binding is supported
*/
protected abstract boolean isAllowRecursiveBinding(ConfigurationPropertySource source);
/**
* Perform binding for the aggregate.
* @param name the configuration property name to bind
* @param target the target to bind
* @param elementBinder an element binder
* @return the bound aggregate or null
*/
@SuppressWarnings("unchecked")
public final Object bind(ConfigurationPropertyName name, Bindable target, AggregateElementBinder elementBinder) {
Object result = bindAggregate(name, target, elementBinder);
Supplier value = target.getValue();
if (result == null || value == null) {
return result;
}
return merge((Supplier) value, (T) result);
}
/**
* Perform the actual aggregate binding.
* @param name the configuration property name to bind
* @param target the target to bind
* @param elementBinder an element binder
* @return the bound result
*/
protected abstract Object bindAggregate(ConfigurationPropertyName name, Bindable target,
AggregateElementBinder elementBinder);
/**
* Merge any additional elements into the existing aggregate.
* @param existing the supplier for the existing value
* @param additional the additional elements to merge
* @return the merged result
*/
protected abstract T merge(Supplier existing, T additional);
/**
* Return the context being used by this binder.
* @return the context
*/
protected final Context getContext() {
return this.context;
}
/**
* Internal class used to supply the aggregate and cache the value.
*
* @param the aggregate type
*/
protected static class AggregateSupplier {
private final Supplier supplier;
private T supplied;
public AggregateSupplier(Supplier supplier) {
this.supplier = supplier;
}
public T get() {
if (this.supplied == null) {
this.supplied = this.supplier.get();
}
return this.supplied;
}
public boolean wasSupplied() {
return this.supplied != null;
}
}
}