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

poussecafe.injector.DependencyInjector Maven / Gradle / Ivy

There is a newer version: 0.29.0
Show newest version
package poussecafe.injector;

import java.util.Map;
import java.util.Objects;

public abstract class DependencyInjector {

    private Map, Object> injectableServices;

    protected DependencyInjector(Map, Object> injectableServices) {
        setInjectableServices(injectableServices);
    }

    private void setInjectableServices(Map, Object> injectableServices) {
        Objects.requireNonNull(injectableServices, "Injectable services cannot be null");
        this.injectableServices = injectableServices;
    }

    public void trySettingDependency() {
        if (isValidTarget()) {
            setDependencyIfResolved();
        }
    }

    protected abstract boolean isValidTarget();

    private void setDependencyIfResolved() {
        Class dependencyType = getTargetType();
        Object dependency = injectableServices.get(dependencyType);
        if (dependency != null) {
            setResolvedDependency(dependency);
        }
    }

    protected abstract Class getTargetType();

    protected abstract void setResolvedDependency(Object dependency);
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy