All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.laamella.parameter_source.ObjectParameterSource Maven / Gradle / Ivy

There is a newer version: 1.0
Show newest version
package com.laamella.parameter_source;

import java.util.Optional;

/**
 * A parameter source that stores objects.
 * If a retrieve value is not of the requested type,
 * conversion will be attempted.
 */
public interface ObjectParameterSource extends ParameterSource {
    default Optional getOptionalString(String key) {
        return getOptionalObject(key).map(Object::toString);
    }

    default Optional getOptionalInteger(String key) {
        Optional optionalObject = getOptionalObject(key);
        if (optionalObject.isPresent()) {
            Object o = optionalObject.get();
            if (o instanceof Integer) {
                return Optional.of((Integer) o);
            }
            throw new ParameterSourceException("%s does not contain an integer value.", key);
        } else {
            return Optional.empty();
        }
    }
}