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

net.n2oapp.framework.config.metadata.compile.ComponentScope Maven / Gradle / Ivy

There is a newer version: 7.28.2
Show newest version
package net.n2oapp.framework.config.metadata.compile;

import java.util.function.Function;

/**
 * Компонент-обёртка.
 * Применяется для передачи родительского компонента в сборку к дочернему
 */
public class ComponentScope {
    private Object component;
    private ComponentScope parentScope;

    public ComponentScope(Object component) {
        this.component = component;
    }

    public ComponentScope(Object component, ComponentScope parentScope) {
        this.component = component;
        this.parentScope = parentScope;
    }

    public  T unwrap(Class clazz) {
        if (clazz.isAssignableFrom(component.getClass()))
            return (T) component;
        else
            return null;
    }

    public  R getFirstNotNull(Class clazz, Function function) {
        return getFirstNotNull(this, clazz, function);
    }

    public static  R getFirstNotNull(ComponentScope componentScope, Class clazz, Function function) {
        while (componentScope != null) {
            T unwrapped = componentScope.unwrap(clazz);
            if (unwrapped != null && function.apply(unwrapped) != null) {
                return function.apply(unwrapped);
            }
            componentScope = componentScope.getParentScope();
        }

        return null;
    }

    public ComponentScope getParentScope() {
        return parentScope;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy