dev.alangomes.springspigot.configuration.Instance Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of spigot-spring-boot-starter Show documentation
Show all versions of spigot-spring-boot-starter Show documentation
Spring support for spigot plugins
package dev.alangomes.springspigot.configuration;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.experimental.FieldDefaults;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.env.Environment;
import java.util.function.Supplier;
/**
* Class that represents a dynamic property
*/
@AllArgsConstructor(access = AccessLevel.PACKAGE)
@FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE)
public class Instance {
Environment environment;
String expression;
ConversionService conversionService;
Class type;
/**
* Evaluate and retrieve the value of the property
*
* @return The value of the property
*/
public T get() {
try {
return conversionService.convert(environment.resolveRequiredPlaceholders(expression), type);
} catch (IllegalArgumentException exception) {
return null;
}
}
/*
* Evaluate and retrieve the value of the property if not {@code null}, otherwise return {@param defaultValue}
*
* @param defaultValue the default value to be returned if the property is {@code null}
* @return The value of the property if not {@code null}, {@param defaultValue} otherwise
*/
public T orElse(T defaultValue) {
T value = get();
return value != null ? value : defaultValue;
}
/*
* Evaluate and retrieve the value of the property if not {@code null}, otherwise return the value supplied from {@param valueSupplier}
*
* @param valueSupplier a supplier for the default value to be returned if the property is {@code null}
* @return The value of the property if not {@code null}, otherwise the value supplied from {@param valueSupplier}
*/
public T orElseGet(Supplier extends T> valueSupplier) {
T value = get();
return value != null ? value : valueSupplier.get();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy