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

net.e6tech.elements.common.resources.Configurator 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.*;
import java.util.function.BiConsumer;
import java.util.function.Function;

/**
 * Created by futeh.
 */
@SuppressWarnings("unchecked")
public class Configurator {
    Map configuration = new HashMap();

    public static  Configurator create(Class cls, BiConsumer consumer) {
        Configurator configurator = new Configurator();
        return configurator.annotate(cls, consumer);
    }

    public  Configurator annotate(Class cls, BiConsumer consumer) {
        T existing = (T) configuration.get(cls);
        configuration.put(cls, Annotator.create(cls, existing, consumer));
        return this;
    }

    public  R annotatedValue(Class key, Function function) {
        T t = (T) configuration.get(key);
        if (t == null)
            t = Annotator.create((Class)key, null);
        return function.apply(t);
    }

    public  Optional annotation(Class key) {
        T t = (T) configuration.get(key);
        return Optional.ofNullable(t);
    }

    public  T get(String key) {
        return (T) configuration.get(key);
    }

    public  T get(String key, T defval) {
        T t = (T) configuration.get(key);
        return (t == null) ? defval : t;
    }

    public  T get(Class key) {
        return (T) configuration.get(key);
    }

    public  T get(Class key, T defval) {
        T t = (T) configuration.get(key);
        return (t == null) ? defval : t;
    }

    public  Map map(Class key) {
        return (Map) configuration.get(key);
    }

    public  List list(Class key) {
        return (List) configuration.get(key);
    }

    public  T computeIfAbsent(String key, Function mappingFunction) {
        return (T) configuration.computeIfAbsent(key, mappingFunction);
    }

    public  T computeIfAbsent(Class key, Function, T> mappingFunction) {
        return (T) configuration.computeIfAbsent(key, mappingFunction);
    }

    public  Map computeMapIfAbsent(Class key) {
        return (Map) configuration.computeIfAbsent(key, k -> new LinkedHashMap<>());
    }

    public  List computeListIfAbsent(Class key) {
        return (List) configuration.computeIfAbsent(key, k -> new LinkedList<>());
    }

    public  Configurator put(Class cls, T instance) {
        configuration.put(cls, instance);
        return this;
    }

    public Configurator put(String key, Object value) {
        configuration.put(key, value);
        return this;
    }

    public Configurator putAll(Configurator configurator) {
        if (configurator != null)
            configuration.putAll(configurator.configuration);
        return this;
    }

    public Configurator putAll(Map map) {
        if (map != null)
            configuration.putAll(map);
        return this;
    }

    public Configurator clear() {
        configuration.clear();
        return this;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy