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

io.micronaut.inject.qualifiers.CompositeQualifier 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.inject.qualifiers;

import io.micronaut.context.Qualifier;
import io.micronaut.core.annotation.Internal;
import io.micronaut.inject.BeanType;

import java.util.Arrays;
import java.util.Collection;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * A {@link Qualifier} composed of other qualifiers.
 *
 * @param  The type
 * @author Graeme Rocher
 * @since 1.0
 */
@Internal
final class CompositeQualifier implements Qualifier {

    private final Qualifier[] qualifiers;

    /**
     * @param qualifiers The qualifiers
     */
    CompositeQualifier(Qualifier[] qualifiers) {
        this.qualifiers = qualifiers;
    }

    @Override
    public > Stream reduce(Class beanType, Stream candidates) {
        Stream reduced = candidates;
        for (Qualifier qualifier : qualifiers) {
            reduced = qualifier.reduce(beanType, reduced);
        }
        return reduced;
    }

    @Override
    public > Collection filter(Class beanType, Collection candidates) {
        for (Qualifier qualifier : qualifiers) {
            candidates = qualifier.filter(beanType, candidates);
        }
        return candidates;
    }

    public Qualifier[] getQualifiers() {
        return qualifiers;
    }

    @Override
    public boolean contains(Qualifier qualifier) {
        if (qualifier instanceof CompositeQualifier compositeQualifier) {
            for (Qualifier q : compositeQualifier.qualifiers) {
                if (!contains(q)) {
                    return false;
                }
            }
            return true;
        }
        if (qualifier instanceof FilteringCompositeQualifier filteringCompositeQualifier) {
            for (Qualifier q : filteringCompositeQualifier.getQualifiers()) {
                if (!contains(q)) {
                    return false;
                }
            }
            return true;
        }
        for (Qualifier q : qualifiers) {
            if (q.contains(qualifier)) {
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        CompositeQualifier that = (CompositeQualifier) o;
        return Arrays.equals(qualifiers, that.qualifiers);
    }

    @Override
    public int hashCode() {
        return Arrays.hashCode(qualifiers);
    }

    @Override
    public String toString() {
        return Arrays.stream(qualifiers).map(Object::toString).collect(Collectors.joining(" and "));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy