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

net.e6tech.elements.common.resources.Configurable Maven / Gradle / Ivy

There is a newer version: 2.7.9
Show newest version
/*
 * Copyright 2017 Futeh Kao
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.e6tech.elements.common.resources;

import net.e6tech.elements.common.reflection.Annotator;

import java.lang.annotation.Annotation;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.Function;

/**
 * Created by futeh.
 */
public interface Configurable {

    Configurator configurator();

    U configurable();

    default U annotation(Class key) {
        configurator().annotation(key);
        return configurable();
    }

    default  T get(String key) {
        return configurator().get(key);
    }

    default  T get(String key, T defval) {
         return configurator().get(key, defval);
    }

    default   T get(Class key) {
        return configurator().get(key);
    }

    default   T get(Class key, T defval) {
        return configurator().get(key);
    }

    default  T computeIfAbsent(String key, Function mappingFunction) {
        return configurator().computeIfAbsent(key, mappingFunction);
    }

    default  T computeIfAbsent(Class key, Function, T> mappingFunction) {
        return configurator().computeIfAbsent(key, mappingFunction);
    }

    default  U annotate(Class cls, BiConsumer consumer) {
        configurator().annotate(cls, consumer);
        return configurable();
    }

    default  U annotate(Class cls, Function func, V value) {
        return annotate(cls, ((annotationValue, annotation) -> annotationValue.set(() -> func.apply(annotation), value)));
    }

    default  ConfigurableAnnotator annotate(Class cls) {
        return new ConfigurableAnnotator<>(cls, this);
    }

    default  U put(Class cls, T instance) {
        configurator().put(cls, instance);
        return configurable();
    }

    default U put(String key, Object value) {
        configurator().put(key, value);
        return configurable();
    }

    default U putAll(Configurator configurator) {
        configurator().putAll(configurator);
        return configurable();
    }

    default U putAll(Map map) {
        configurator().putAll(map);
        return configurable();
    }

    class ConfigurableAnnotator implements Configurable {
        Class annotationClass;
        Configurable configurable;

        ConfigurableAnnotator(Class annotationClass, Configurable configurable) {
            this.annotationClass = annotationClass;
            this.configurable = configurable;
        }

        public Configurator configurator() {
            return configurable.configurator();
        }

        public U configurable() {
            return configurable.configurable();
        }

        public  ConfigurableAnnotator annotate(Function func, V value) {
            this.annotate(annotationClass, func, value);
            return this;
        }
    }
}