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

org.kiwiproject.consul.util.bookend.ConsulBookendContext Maven / Gradle / Ivy

package org.kiwiproject.consul.util.bookend;

import static com.google.common.base.Preconditions.checkState;
import static java.util.Objects.isNull;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

public class ConsulBookendContext {

    private Map data;

    ConsulBookendContext() {
        // package-private constructor
    }

    public void put(String key, Object value) {
        if (isNull(data)) {
            data = new HashMap<>();
        }

        data.put(key, value);
    }

    public  Optional get(String key, Class klazz) {
        if (isNull(data) || !data.containsKey(key)) {
            return Optional.empty();
        }

        var object = data.get(key);
        if (isNull(object)) {
            return Optional.empty();
        }

        checkState(klazz.isAssignableFrom(object.getClass()),
                "Data for key '%s' is not of type: %s", key, klazz.getName());

        T castObject = klazz.cast(object);
        return Optional.of(castObject);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy