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

io.micronaut.context.DefaultConditionContext Maven / Gradle / Ivy

There is a newer version: 4.7.5
Show newest version
/*
 * Copyright 2017-2020 original authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.micronaut.context;

import io.micronaut.core.annotation.Nullable;
import io.micronaut.context.condition.ConditionContext;
import io.micronaut.context.condition.Failure;
import io.micronaut.core.annotation.AnnotationMetadataProvider;
import io.micronaut.core.annotation.Internal;

import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.convert.ArgumentConversionContext;
import io.micronaut.core.type.Argument;
import io.micronaut.core.value.PropertyResolver;
import io.micronaut.inject.BeanDefinition;

import java.util.*;
import java.util.stream.Stream;

/**
 * A Default context implementation.
 *
 * @param  The condition context type
 * @author Graeme Rocher
 * @since 1.0
 */
@Internal
class DefaultConditionContext implements ConditionContext {

    private final DefaultBeanContext beanContext;
    private final B component;
    private final List failures = new ArrayList<>(2);
    private final BeanResolutionContext resolutionContext;

    /**
     * @param beanContext The bean context
     * @param component   The component type
     * @param resolutionContext The resolution context
     */
    DefaultConditionContext(DefaultBeanContext beanContext, B component, BeanResolutionContext resolutionContext) {
        this.beanContext = beanContext;
        this.component = component;
        this.resolutionContext = resolutionContext;
    }

    @Override
    public B getComponent() {
        return component;
    }

    @Override
    public BeanContext getBeanContext() {
        return beanContext;
    }

    @Override
    public BeanResolutionContext getBeanResolutionContext() {
        return resolutionContext;
    }

    @Override
    public ConditionContext fail(@NonNull Failure failure) {
        failures.add(failure);
        return this;
    }

    @Override
    public String toString() {
        return component.toString();
    }

    @Override
    public List getFailures() {
        return Collections.unmodifiableList(failures);
    }

    @NonNull
    @Override
    public  T getBean(@NonNull BeanDefinition definition) {
        return beanContext.getBean(definition);
    }

    @NonNull
    @Override
    public  T getBean(@NonNull Class beanType, @Nullable Qualifier qualifier) {
        return beanContext.getBean(resolutionContext, beanType, qualifier);
    }

    @NonNull
    @Override
    public  T getBean(@NonNull Argument beanType, @Nullable Qualifier qualifier) {
        return beanContext.getBean(resolutionContext, beanType, qualifier);
    }

    @NonNull
    @Override
    public  Optional findBean(@NonNull Class beanType, @Nullable Qualifier qualifier) {
        return beanContext.findBean(resolutionContext, beanType, qualifier);
    }

    @NonNull
    @Override
    public  Collection getBeansOfType(@NonNull Class beanType) {
        return getBeansOfType(beanType, null);
    }

    @NonNull
    @Override
    public  Collection getBeansOfType(@NonNull Class beanType, @Nullable Qualifier qualifier) {
        return beanContext.getBeansOfType(resolutionContext, Argument.of(beanType), qualifier);
    }

    @NonNull
    @Override
    public  Collection getBeansOfType(@NonNull Argument beanType, @Nullable Qualifier qualifier) {
        return beanContext.getBeansOfType(resolutionContext, beanType, qualifier);
    }

    @NonNull
    @Override
    public  Stream streamOfType(@NonNull Class beanType, @Nullable Qualifier qualifier) {
        return beanContext.streamOfType(resolutionContext, beanType, qualifier);
    }

    @NonNull
    @Override
    public  Stream streamOfType(@NonNull Argument beanType, @Nullable Qualifier qualifier) {
        return beanContext.streamOfType(resolutionContext, beanType, qualifier);
    }

    @NonNull
    @Override
    public  T getProxyTargetBean(@NonNull Class beanType, @Nullable Qualifier qualifier) {
        return beanContext.getProxyTargetBean(beanType, qualifier);
    }

    @Override
    public boolean containsProperty(@NonNull String name) {
        if (beanContext instanceof PropertyResolver resolver) {
            return resolver.containsProperty(name);
        }
        return false;
    }

    @Override
    public boolean containsProperties(@NonNull String name) {
        if (beanContext instanceof PropertyResolver resolver) {
            return resolver.containsProperties(name);
        }
        return false;
    }

    @NonNull
    @Override
    public  Optional getProperty(@NonNull String name, @NonNull ArgumentConversionContext conversionContext) {
        if (beanContext instanceof PropertyResolver resolver) {
            return resolver.getProperty(name, conversionContext);
        }
        return Optional.empty();
    }

    @Override
    public Collection> getPropertyPathMatches(String pathPattern) {
        if (beanContext instanceof PropertyResolver resolver) {
            return resolver.getPropertyPathMatches(pathPattern);
        }
        return Collections.emptyList();
    }

    @Override
    public  Optional findBean(Argument beanType, Qualifier qualifier) {
        return beanContext.findBean(beanType, qualifier);
    }
}