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

com.github.ltsopensource.autoconfigure.resolver.AbstractResolver Maven / Gradle / Ivy

package com.github.ltsopensource.autoconfigure.resolver;

import com.github.ltsopensource.autoconfigure.AutoConfigContext;
import com.github.ltsopensource.autoconfigure.PropertiesConfigurationResolveException;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.Set;

/**
 * @author Robert HG ([email protected]) on 4/20/16.
 */
public abstract class AbstractResolver implements Resolver {

    protected void doFilter(AutoConfigContext context, PropertyDescriptor descriptor, Filter filter) {

        Set names = context.getNamesByDescriptor(descriptor);

        for (Map.Entry entry : context.getPropMap().entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();
            for (String name : names) {
                if (filter.onCondition(name, key, value)) {
                    filter.call(name, key, value);
                }
            }
        }
    }

    protected void writeProperty(AutoConfigContext context, PropertyDescriptor descriptor, Object value) {
        try {
            Method writeMethod = descriptor.getWriteMethod();
            writeMethod.invoke(context.getTargetObject(), value);
        } catch (Exception e) {
            throw new PropertiesConfigurationResolveException("Inject Property " + descriptor.getName() + " Error", e);
        }
    }

    protected interface Filter {

        boolean onCondition(String name, String key, String value);

        /**
         * @return is go on
         */
        boolean call(String name, String key, String value);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy