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

org.jboss.weld.bootstrap.enablement.ModuleEnablement Maven / Gradle / Ivy

There is a newer version: 3.0.0.Alpha1
Show newest version
/*
 * JBoss, Home of Professional Open Source
 * Copyright 2012, 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.enablement;

import static com.google.common.collect.Sets.union;

import java.io.Serializable;
import java.lang.annotation.Annotation;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.Decorator;
import javax.enterprise.inject.spi.Interceptor;

import com.google.common.collect.ImmutableMap;

/**
 * Holds information about interceptors, decorators and alternatives that are enabled in this module.
 *
 * @author Jozef Hartinger
 *
 */
public class ModuleEnablement {

    public static final ModuleEnablement EMPTY_ENABLEMENT = new ModuleEnablement(Collections.>emptyList(), Collections.>emptyList(), Collections., Integer>emptyMap(), Collections.>emptySet(), Collections.>emptySet());

    private final List> interceptors;
    private final List> decorators;

    private final Map, Integer> interceptorMap;
    private final Map, Integer> decoratorMap;
    private final Map, Integer> globalAlternatives;

    private final Set> localAlternativeClasses;
    private final Set> localAlternativeStereotypes;

    private final Comparator> decoratorComparator;
    private final Comparator> interceptorComparator;

    public ModuleEnablement(List> interceptors, List> decorators, Map, Integer> globalAlternatives,
            Set> localAlternativeClasses, Set> localAlternativeStereotypes) {
        this.interceptors = interceptors;
        this.decorators = decorators;

        this.interceptorMap = createLookupMap(interceptors);
        this.decoratorMap = createLookupMap(decorators);

        this.decoratorComparator = new EnablementComparator>(decoratorMap);
        this.interceptorComparator = new EnablementComparator>(interceptorMap);

        this.globalAlternatives = globalAlternatives;

        this.localAlternativeClasses = localAlternativeClasses;
        this.localAlternativeStereotypes = localAlternativeStereotypes;
    }

    private static Map, Integer> createLookupMap(List> list) {
        if (list.isEmpty()) {
            return Collections.emptyMap();
        }
        Map, Integer> result = new HashMap, Integer>();
        for (int i = 0; i < list.size(); i++) {
            result.put(list.get(i), i);
        }
        return ImmutableMap.copyOf(result);
    }

    public boolean isInterceptorEnabled(Class javaClass) {
        return interceptorMap.containsKey(javaClass);
    }

    public boolean isDecoratorEnabled(Class javaClass) {
        return decoratorMap.containsKey(javaClass);
    }

    public List> getInterceptors() {
        return interceptors;
    }

    public List> getDecorators() {
        return decorators;
    }

    public Comparator> getDecoratorComparator() {
        return decoratorComparator;
    }

    public Comparator> getInterceptorComparator() {
        return interceptorComparator;
    }

    public Integer getAlternativePriority(Class javaClass) {
        return globalAlternatives.get(javaClass);
    }

    public boolean isEnabledAlternativeClass(Class alternativeClass) {
        return globalAlternatives.containsKey(alternativeClass) || localAlternativeClasses.contains(alternativeClass);
    }

    public boolean isEnabledAlternativeStereotype(Class alternativeClass) {
        return globalAlternatives.containsKey(alternativeClass) || localAlternativeStereotypes.contains(alternativeClass);
    }

    public Set> getAlternativeClasses() {
        return localAlternativeClasses;
    }

    public Set> getAlternativeStereotypes() {
        return localAlternativeStereotypes;
    }

    public Set> getGlobalAlternatives() {
        return globalAlternatives.keySet();
    }

    public Set> getAllAlternatives() {
        return union(union(localAlternativeClasses, localAlternativeStereotypes), getGlobalAlternatives());
    }

    private static class EnablementComparator> implements Comparator, Serializable {

        private static final long serialVersionUID = -4757462262711016985L;
        private final Map, Integer> enabledClasses;

        public EnablementComparator(Map, Integer> enabledClasses) {
            this.enabledClasses = enabledClasses;
        }

        @Override
        public int compare(T o1, T o2) {
            int p1 = enabledClasses.get(o1.getBeanClass());
            int p2 = enabledClasses.get(o2.getBeanClass());
            return p1 - p2;
        }
    }

    @Override
    public String toString() {
        return "ModuleEnablement [interceptors=" + interceptors + ", decorators=" + decorators + ", alternatives=" + getAllAlternatives() + "]";
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy