com.laamella.parameter_source.ParameterSource Maven / Gradle / Ivy
package com.laamella.parameter_source;
import java.net.URI;
import java.net.URL;
import java.time.Duration;
import java.util.List;
import java.util.Optional;
import static com.laamella.parameter_source.ParameterSourceException.missingKeyException;
import static com.laamella.parameter_source.TypeConverter.*;
import static java.util.Objects.requireNonNull;
/**
* Abstract interface to a parameter (simple key-value) store, adds checking of required parameters, a tight interface,
* logging, and easy testing.
*/
public interface ParameterSource {
/**
* Retrieves a string from this source by key.
*/
default Optional getOptionalString(String key) {
requireNonNull(key);
return getOptionalObject(key).map(o -> objectToString(key, o));
}
/**
* Retrieves an optional Integer from this source by key.
*/
default Optional getOptionalInteger(String key) {
requireNonNull(key);
return getOptionalObject(key).map(o -> objectToInteger(key, o));
}
/**
* Retrieves a list of Strings from this source by key.
*/
default Optional> getOptionalStringList(String key) {
requireNonNull(key);
return getOptionalObject(key).map(o -> objectToList(key, o, String.class));
}
/**
* Retrieves an optional Long from this source by key.
*/
default Optional getOptionalLong(String key) {
requireNonNull(key);
return getOptionalObject(key).map(o -> objectToLong(key, o));
}
/**
* Retrieves an optional URL from this source by key.
*/
default Optional getOptionalUrl(String key) {
requireNonNull(key);
return getOptionalObject(key).map(o -> objectToUrl(key, o));
}
/**
* Retrieves an optional Class from this source by key.
*/
default Optional> getOptionalClass(String key) {
requireNonNull(key);
return getOptionalObject(key).map(o -> objectToClass(key, o));
}
/**
* Retrieves an optional enum from this source by key.
*/
default > Optional getOptionalEnum(String key, Class enumType) {
requireNonNull(key);
return getOptionalObject(key).map(o -> objectToEnum(key, o, enumType));
}
/**
* Retrieves an optional URI from this source by key.
*/
default Optional getOptionalUri(String key) {
requireNonNull(key);
return getOptionalObject(key).map(o -> objectToUri(key, o));
}
/**
* Retrieves an optional Float from this source by key.
*/
default Optional getOptionalFloat(String key) {
requireNonNull(key);
return getOptionalObject(key).map(o -> objectToFloat(key, o));
}
/**
* Retrieves an optional Double from this source by key.
*/
default Optional getOptionalDouble(String key) {
requireNonNull(key);
return getOptionalObject(key).map(o -> objectToDouble(key, o));
}
/**
* Retrieves an optional boolean from this source by key.
*/
default Optional getOptionalBoolean(String key) {
requireNonNull(key);
return getOptionalObject(key).map(o -> objectToBoolean(key, o));
}
/**
* Retrieves an obfuscated string from this source by key.
*/
default Optional getOptionalObfuscatedString(String key) {
requireNonNull(key);
return getOptionalString(key).map(s -> stringToUnobfuscatedString(key, s));
}
/**
* Retrieves an optional Object from this source by key.
*/
default Optional