com.pandich.dropwizard.curator.PropertySources.java Source code

Java tutorial

Introduction

Here is the source code for com.pandich.dropwizard.curator.PropertySources.java

Source

/*
 * This file is part of dropwizard-curator.
 *
 * dropwizard-curator is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * dropwizard-curator is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with dropwizard-curator.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.pandich.dropwizard.curator;

import com.google.common.collect.Maps;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentMap;

import static com.pandich.dropwizard.curator.PropertySources.PropertySource.ABSENT;
import static org.apache.commons.lang3.StringUtils.isBlank;
import static org.apache.commons.lang3.builder.ToStringStyle.SIMPLE_STYLE;

public final class PropertySources
        implements Iterable<Map.Entry<Class, ConcurrentMap<String, PropertySources.PropertySource>>> {

    public static enum PropertySource {

        ZOOKEEPER("ZooKeeper"), DROPWIZARD("DropWizard"), ABSENT("Absent");

        private final String source;

        PropertySource(final String source) {
            this.source = source;
        }

        public String getSource() {

            return source;
        }

        @Override
        public String toString() {

            return getSource();
        }

    }

    public static final ToStringStyle TO_STRING_STYLE = SIMPLE_STYLE;
    private final ConcurrentMap<Class, ConcurrentMap<String, PropertySource>> classPropertySources = Maps
            .newConcurrentMap();

    public void register(final Class clazz, final Field field, final PropertySource source) {

        if (field == null) {
            return;
        }
        register(clazz, field.getName(), source);
    }

    public void register(final Class clazz, final Method method, final PropertySource source) {

        if (method == null) {
            return;
        }
        register(clazz, method.getName(), source);
    }

    public void register(final Class clazz, final String name, final PropertySource source) {

        if (clazz == null || isBlank(name) || source == null) {
            return;
        }

        this.classPropertySources.putIfAbsent(clazz, Maps.<String, PropertySource>newConcurrentMap());
        this.classPropertySources.get(clazz).put(name, source);
    }

    public PropertySource lookup(final Class clazz, final Field field) {

        if (field == null) {
            return ABSENT;
        }
        return lookup(clazz, field.getName());
    }

    public PropertySource lookup(final Class clazz, final Method method) {

        if (method == null) {
            return ABSENT;
        }
        return lookup(clazz, method.getName());
    }

    public PropertySource lookup(final Class clazz, final String name) {

        final ConcurrentMap<String, PropertySource> propertySources = this.classPropertySources.get(clazz);
        if (propertySources == null) {
            return ABSENT;
        }

        final PropertySource propertySource = propertySources.get(name);
        if (propertySource == null) {
            return ABSENT;
        }

        return propertySource;
    }

    @Override
    public Iterator<Map.Entry<Class, ConcurrentMap<String, PropertySource>>> iterator() {

        return this.classPropertySources.entrySet().iterator();
    }

    @Override
    public String toString() {

        final ToStringBuilder builder = new ToStringBuilder(this, TO_STRING_STYLE);

        for (final Map.Entry<Class, ConcurrentMap<String, PropertySource>> classEntry : this.classPropertySources
                .entrySet()) {

            final ToStringBuilder classBuilder = new ToStringBuilder(classEntry.getClass(), TO_STRING_STYLE);
            for (final Map.Entry<String, PropertySource> propertyEntry : classEntry.getValue().entrySet()) {
                classBuilder.append(propertyEntry.getKey(), propertyEntry.getValue());
            }

            builder.append(classBuilder.toString());

        }

        return builder.build();
    }
}