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

org.jboss.weld.bootstrap.events.builder.ObserverMethodBuilderImpl 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.lang.reflect.Type;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

import javax.enterprise.event.Reception;
import javax.enterprise.event.TransactionPhase;
import javax.enterprise.inject.spi.EventMetadata;
import javax.enterprise.inject.spi.ObserverMethod;
import javax.enterprise.inject.spi.builder.ObserverMethodConfigurator;

import org.jboss.weld.event.SyntheticObserverMethod;
import org.jboss.weld.util.collections.ImmutableSet;

/**
 *
 * @author Martin Kouba
 */
public class ObserverMethodBuilderImpl {
    // implements ObserverMethodBuilder {

    private final ObserverMethodConfiguratorImpl configurator;

    /**
     *
     * @param configurator
     */
    public ObserverMethodBuilderImpl(ObserverMethodConfiguratorImpl configurator) {
        this.configurator = configurator;
    }

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

    // @Override
    public ObserverMethod build() {
        return new ImmutableObserverMethod<>(configurator);
    }

    /**
     *
     * @author Martin Kouba
     *
     * @param 
     */
    static class ImmutableObserverMethod implements SyntheticObserverMethod {

        private final Class beanClass;

        private final Type observedType;

        private final Set observedQualifiers;

        private final Reception reception;

        private final TransactionPhase txPhase;

        private final int priority;

        private final boolean isAsync;

        private final NotificationCallback notificationCallback;

        /**
         *
         * @param configurator
         */
        ImmutableObserverMethod(ObserverMethodConfiguratorImpl configurator) {
            this.beanClass = configurator.getBeanClass();
            this.observedType = configurator.getObservedType();
            this.observedQualifiers = ImmutableSet.copyOf(configurator.getObservedQualifiers());
            this.reception = configurator.getReception();
            this.txPhase = configurator.getTxPhase();
            this.priority = configurator.getPriority();
            this.isAsync = configurator.isAsync();
            this.notificationCallback = NotificationCallback.from(configurator);
        }

        @Override
        public int getPriority() {
            return priority;
        }

        @Override
        public Class getBeanClass() {
            return beanClass;
        }

        @Override
        public Type getObservedType() {
            return observedType;
        }

        @Override
        public Set getObservedQualifiers() {
            return observedQualifiers;
        }

        @Override
        public Reception getReception() {
            return reception;
        }

        @Override
        public TransactionPhase getTransactionPhase() {
            return txPhase;
        }

        @Override
        public void notify(T event) {
            notificationCallback.notify(event, null);
        }

        @Override
        public boolean isAsync() {
            return isAsync;
        }

        @Override
        public void notify(T event, EventMetadata eventMetadata) {
            notificationCallback.notify(event, eventMetadata);
        }

        @Override
        public boolean isEventMetadataRequired() {
            return notificationCallback.isMetadataRequired();
        }

    }

    public static final class NotificationCallback {

        private final Consumer notifySimple;

        private final BiConsumer notifyMetadata;

        static  NotificationCallback from(ObserverMethodConfiguratorImpl configurator) {
            return configurator.getNotifySimple() != null ? new NotificationCallback<>(configurator.getNotifySimple(), null)
                    : new NotificationCallback<>(null, configurator.getNotifyMetadata());
        }

        private NotificationCallback(Consumer notifySimple, BiConsumer notifyMetadata) {
            this.notifySimple = notifySimple;
            this.notifyMetadata = notifyMetadata;
        }

        void notify(T event, EventMetadata metadata) {
            if (notifySimple != null) {
                notifySimple.accept(event);
            } else {
                notifyMetadata.accept(event, metadata);
            }
        }

        boolean isMetadataRequired() {
            return notifyMetadata != null;
        }

    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy