Always Class — spring-boot Architecture
Architecture documentation for the Always class in PropertyMapper.java from the spring-boot codebase.
Entity Profile
Relationship Graph
Source Code
core/spring-boot/src/main/java/org/springframework/boot/context/properties/PropertyMapper.java lines 396–556
public static class Always<T> {
private final Supplier<@Nullable T> supplier;
private final Predicate<T> predicate;
Always(Supplier<@Nullable T> supplier, Predicate<T> predicate) {
this.supplier = supplier;
this.predicate = predicate;
}
/**
* Return an adapted version of the source changed through the given adapter
* function.
* @param <R> the resulting type
* @param adapter the adapter to apply
* @return a new adapted source instance
*/
public <R> Always<R> as(Adapter<? super T, ? extends R> adapter) {
Assert.notNull(adapter, "'adapter' must not be null");
Supplier<@Nullable R> supplier = () -> {
T value = getValue();
return (value == null || test(value)) ? adapter.adapt(value) : null;
};
Predicate<R> predicate = (adaptedValue) -> {
T value = getValue();
return value == null || test(value);
};
return new Always<>(supplier, predicate);
}
/**
* Complete the mapping by passing any non-filtered value to the specified
* consumer. The method is designed to be used with mutable objects.
* @param consumer the consumer that should accept the value if it's not been
* filtered
*/
public void to(Consumer<@Nullable ? super T> consumer) {
Assert.notNull(consumer, "'consumer' must not be null");
T value = getValue();
if (value == null || test(value)) {
consumer.accept(value);
}
}
/**
* Complete the mapping for any non-filtered value by applying the given
* function to an existing instance and returning a new one. For filtered
* values, the {@code instance} parameter is returned unchanged. The method is
* designed to be used with immutable objects.
* @param <R> the result type
* @param instance the current instance
* @param mapper the mapping function
* @return a new mapped instance or the original instance
*/
public <R> R to(R instance, Mapper<R, ? super T> mapper) {
Assert.notNull(instance, "'instance' must not be null");
Assert.notNull(mapper, "'mapper' must not be null");
T value = getValue();
if (value == null || test(value)) {
return mapper.map(instance, value);
}
return instance;
}
/**
* Complete the mapping by creating a new instance from the non-filtered
* value.
* @param <R> the resulting type
* @param factory the factory used to create the instance
* @return the instance
* @throws NoSuchElementException if the value has been filtered
*/
public <R> R toInstance(Factory<? super T, ? extends R> factory) {
Assert.notNull(factory, "'factory' must not be null");
T value = getValue();
if (value == null || test(value)) {
return factory.create(value);
}
throw new NoSuchElementException("No value present");
}
/**
* Complete the mapping by calling the specified method when the value has not
* been filtered.
* @param runnable the method to call if the value has not been filtered
*/
public void toCall(Runnable runnable) {
Assert.notNull(runnable, "'runnable' must not be null");
T value = getValue();
if (value == null || test(value)) {
runnable.run();
}
}
private @Nullable T getValue() {
return this.supplier.get();
}
private boolean test(T value) {
Assert.state(value != null, "'value' must not be null");
return this.predicate.test(value);
}
/**
* Adapter that support nullable values.
*
* @param <T> the source type
* @param <R> the result type
*/
@FunctionalInterface
public interface Adapter<T, R> {
/**
* Adapt the given value.
* @param value the value to adapt
* @return an adapted value or {@code null}
*/
@Nullable R adapt(@Nullable T value);
}
/**
* Factory that supports nullable values.
*
* @param <T> the source type
* @param <R> the result type
*/
@FunctionalInterface
public interface Factory<T, R extends @Nullable Object> {
/**
* Create a new instance for the given nullable value.
* @param value the value used to create the instance (may be
* {@code null})
* @return the resulting instance
*/
R create(@Nullable T value);
}
/**
* Mapper that supports nullable values.
*
* @param <T> the source type
* @param <R> the result type
*/
@FunctionalInterface
public interface Mapper<R extends @Nullable Object, T> {
/**
* Map a existing instance for the given nullable value.
* @param instance the existing instance
* @param value the value to map (may be {@code null})
* @return the resulting mapped instance
*/
R map(R instance, @Nullable T value);
}
}
Domain
Source
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free