Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source
   * Copyright 2008, 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.event;
 
 import static org.jboss.weld.util.collections.WeldCollections.putIfAbsent;
 
 
 
Implementation of the Event interface

Parameters:
<T> The type of event being wrapped
Author(s):
David Allen
See also:
javax.enterprise.event.Event
 
 @edu.umd.cs.findbugs.annotations.SuppressWarnings(value = "SE_NO_SUITABLE_CONSTRUCTOR", justification = "Uses SerializationProxy")
 public class EventImpl<T> extends AbstractFacade<T, Event<T>> implements Event<T>, Serializable {
 
     private static final String SUBTYPE_ARGUMENT_NAME = "subtype";
     private static final long serialVersionUID = 656782657242515455L;
     private static final int DEFAULT_CACHE_CAPACITY = 4;
 
     public static <E> EventImpl<E> of(InjectionPoint injectionPointBeanManagerImpl beanManager) {
         return new EventImpl<E>(injectionPointbeanManager);
     }
 
     private final transient HierarchyDiscovery injectionPointTypeHierarchy;
     private transient volatile CachedObservers lastCachedObservers;
     private final transient ConcurrentMap<Class<?>, CachedObserverscachedObservers;
 
     private EventImpl(InjectionPoint injectionPointBeanManagerImpl beanManager) {
         super(injectionPointnullbeanManager);
         this. = new HierarchyDiscovery(getType());
     }

    
Gets a string representation

Returns:
A string representation
 
     @Override
     public String toString() {
         return Formats.formatAnnotations(getQualifiers()) + " Event<" + Formats.formatType(getType()) + ">";
     }
 
     @Override
     public void fire(T event) {
         Preconditions.checkArgumentNotNull(event"event");
         CachedObservers observers = getObservers(event);
         // we can do lenient here as the event type is checked within #getObservers()
         getBeanManager().getGlobalLenientObserverNotifier().notify(observers.observerseventobservers.metadata);
     }
 
     private CachedObservers getObservers(T event) {
         Class<?> runtimeType = event.getClass();
         CachedObservers lastResolvedObservers = this.;
         // fast track for cases when the same type is used repeatedly
         if (lastResolvedObservers != null && lastResolvedObservers.rawType.equals(runtimeType)) {
             return lastResolvedObservers;
         }
         lastResolvedObservers = .get(runtimeType);
        if (lastResolvedObservers == null) {
            // this is not atomic and less elegant than computeIfAbsent but is faster and atomicity does not really matter here
            // as createCachedObservers() does not have any side effects
            lastResolvedObservers = putIfAbsent(runtimeTypecreateCachedObservers(runtimeType));
        }
        return this. = lastResolvedObservers;
    }
    private CachedObservers createCachedObservers(Class<?> runtimeType) {
        final Type eventType = getEventType(runtimeType);
        // this performs type check
        final EventMetadata metadata = new EventMetadataImpl(eventTypegetInjectionPoint(), getQualifiers());
        return new CachedObservers(runtimeTypeobserversmetadata);
    }
    @Override
    public Event<T> select(Annotation... qualifiers) {
        return selectEvent(this.getType(), qualifiers);
    }
    @Override
    public <U extends T> Event<U> select(Class<U> subtypeAnnotation... qualifiers) {
        Preconditions.checkArgumentNotNull(subtype);
        return selectEvent(subtypequalifiers);
    }
    @Override
    public <U extends T> Event<U> select(TypeLiteral<U> subtypeAnnotation... qualifiers) {
        Preconditions.checkArgumentNotNull(subtype);
        return selectEvent(subtype.getType(), qualifiers);
    }
    private <U extends T> Event<U> selectEvent(Type subtypeAnnotation[] newQualifiers) {
        return new EventImpl<U>(new FacadeInjectionPoint(getBeanManager(), getInjectionPoint(), subtypegetQualifiers(), newQualifiers),
                getBeanManager());
    }
    protected Type getEventType(Class<?> runtimeType) {
        Type resolvedType = runtimeType;
        if (Types.containsUnresolvedTypeVariableOrWildcard(resolvedType)) {
            /*
             * If the container is unable to resolve the parameterized type of the event object, it uses the specified type to infer the parameterized type of the event types.
             */
            resolvedType = .resolveType(resolvedType);
        }
        if (Types.containsUnresolvedTypeVariableOrWildcard(resolvedType)) {
            /*
             * Examining the hierarchy of the specified type did not help. This may still be one of the cases when combining the
             * event type and the specified type reveals the actual values for type variables. Let's try that.
             */
            Type canonicalEventType = Types.getCanonicalType(runtimeType);
            TypeResolver objectTypeResolver = new EventObjectTypeResolverBuilder(.getResolver()
                    .getResolvedTypeVariables(), new HierarchyDiscovery(canonicalEventType).getResolver()
                    .getResolvedTypeVariables()).build();
            resolvedType = objectTypeResolver.resolveType(canonicalEventType);
        }
        return resolvedType;
    }
    // Serialization
    private Object writeReplace() throws ObjectStreamException {
        return new SerializationProxy<T>(this);
    }
    private void readObject(ObjectInputStream streamthrows InvalidObjectException {
        throw ..proxyRequired();
    }
    private static class SerializationProxy<T> extends AbstractFacadeSerializationProxy<T, Event<T>> {
        private static final long serialVersionUID = 9181171328831559650L;
        public SerializationProxy(EventImpl<T> event) {
            super(event);
        }
        private Object readResolve() throws ObjectStreamException {
            return EventImpl.of(getInjectionPoint(), getBeanManager());
        }
    }
    private class CachedObservers {
        private final Class<?> rawType;
        private final ResolvedObservers<T> observers;
        private final EventMetadata metadata;
        private CachedObservers(Class<?> rawTypeResolvedObservers<T> observersEventMetadata metadata) {
            this. = rawType;
            this. = observers;
            this. = metadata;
        }
    }
New to GrepCode? Check out our FAQ X