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

net.isger.brick.plugin.persist.Persists Maven / Gradle / Ivy

The newest version!
package net.isger.brick.plugin.persist;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import net.isger.util.Helpers;
import net.isger.util.Strings;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public final class Persists {

    private static final Logger LOG;

    private Map persists;

    static {
        LOG = LoggerFactory.getLogger(Persists.class);
    }

    public Persists() {
        this(null);
    }

    @SuppressWarnings("unchecked")
    public Persists(List persists) {
        this.persists = new HashMap();
        if (persists != null) {
            for (Object instance : persists) {
                if (instance instanceof Persist) {
                    add((Persist) instance);
                } else if (instance instanceof Map) {
                    for (Entry entry : ((Map) instance)
                            .entrySet()) {
                        instance = entry.getValue();
                        if (instance instanceof Persist) {
                            put(entry.getKey(), (Persist) instance);
                        }
                    }
                }
            }
        }
    }

    public void add(Persist persist) {
        put("", persist);
    }

    public void put(String name, Persist persist) {
        int index = name.lastIndexOf(".");
        String key;
        if (index++ > 0) {
            key = name.substring(0, index);
            name = name.substring(index);
        } else {
            key = "";
        }
        key += getName(persist.getClass(), name);
        if (LOG.isDebugEnabled()) {
            LOG.info("Binding [{}] persist [{}]", key, persist);
        }
        persist = persists.put(key, persist);
        if (persist != null) {
            LOG.warn("(!) Discard [{}] service [{}]", key, persist);
        }
    }

    public Persist get(String name) {
        return persists.get(name);
    }

    public Collection values() {
        return persists.values();
    }

    public Set> entrySet() {
        return persists.entrySet();
    }

    public static final String getName(Class clazz) {
        return getName(clazz, "");
    }

    public static final String getName(Class clazz,
            String name) {
        return Helpers.getAliasName(clazz, "Persist$", Strings.toLower(name));
    }

}