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

org.jboss.weld.bootstrap.events.builder.BeanAttributesBuilderImpl Maven / Gradle / Ivy

There is a newer version: 6.0.2.Final
Show newest version
/*
 * JBoss, Home of Professional Open Source
 * Copyright 2016, Red Hat, Inc., and individual contributors
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * 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
 * http://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 org.jboss.weld.bootstrap.events.builder;

import java.lang.annotation.Annotation;
import java.util.HashSet;
import java.util.Set;

import javax.enterprise.context.Dependent;
import javax.enterprise.inject.Any;
import javax.enterprise.inject.Default;
import javax.enterprise.inject.spi.BeanAttributes;
import javax.enterprise.inject.spi.builder.BeanAttributesConfigurator;
import javax.inject.Named;

import org.jboss.weld.bean.attributes.ImmutableBeanAttributes;
import org.jboss.weld.util.collections.ImmutableSet;

/**
 *
 * @author Martin Kouba
 *
 * @param 
 */
public class BeanAttributesBuilderImpl {
    // implements BeanAttributesBuilder {

    static final Set DEFAULT_QUALIFIERS = ImmutableSet.of(Any.Literal.INSTANCE, Default.Literal.INSTANCE);

    private final BeanAttributesConfiguratorImpl configurator;

    /**
     *
     * @param configurator
     */
    public BeanAttributesBuilderImpl(BeanAttributesConfiguratorImpl configurator) {
        this.configurator = configurator;
    }

    // @Override
    public BeanAttributesConfigurator configure() {
        return configurator;
    }

    // @Override
    public BeanAttributes build() {
        return new ImmutableBeanAttributes(ImmutableSet.copyOf(configurator.getStereotypes()), configurator.isAlternative(), configurator.getName(),
                initQualifiers(configurator.getQualifiers()), ImmutableSet.copyOf(configurator.getTypes()), initScope(configurator));
    }

    private Class initScope(BeanAttributesConfiguratorImpl configurator) {
        return configurator.getScope() != null ? configurator.getScope() : Dependent.class;
    }

    private Set initQualifiers(Set qualifiers) {
        if (qualifiers.isEmpty()) {
            return DEFAULT_QUALIFIERS;
        }
        Set normalized = new HashSet(qualifiers);
        normalized.remove(Any.Literal.INSTANCE);
        normalized.remove(Default.Literal.INSTANCE);
        if (normalized.isEmpty()) {
            normalized = DEFAULT_QUALIFIERS;
        } else {
            ImmutableSet.Builder builder = ImmutableSet.builder();
            if (normalized.size() == 1) {
                if (qualifiers.iterator().next().annotationType().equals(Named.class)) {
                    builder.add(Default.Literal.INSTANCE);
                }
            }
            builder.add(Any.Literal.INSTANCE);
            builder.addAll(qualifiers);
            normalized = builder.build();
        }
        return normalized;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy