EventListenerRegistrationData.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.configuration2.event;

  18. /**
  19.  * <p>
  20.  * A data class holding information about an event listener registration.
  21.  * </p>
  22.  * <p>
  23.  * An instance of this class stores all information required to determine whether a specific event listener is to be
  24.  * invoked for a given event. The class is used internally by {@link EventListenerList}, but is also useful in general
  25.  * when information about event listeners is to be stored.
  26.  * </p>
  27.  * <p>
  28.  * Implementation note: Instances of this class are immutable and can safely be shared between multiple threads or
  29.  * components.
  30.  * </p>
  31.  *
  32.  * @param <T> the type of events processed by the listener
  33.  * @since 2.0
  34.  */
  35. public final class EventListenerRegistrationData<T extends Event> {
  36.     /** Constant for the factor used by the calculation of the hash code. */
  37.     private static final int HASH_FACTOR = 31;

  38.     /** The event type. */
  39.     private final EventType<T> eventType;

  40.     /** The event listener. */
  41.     private final EventListener<? super T> listener;

  42.     /**
  43.      * Creates a new instance of {@code EventListenerRegistrationData}.
  44.      *
  45.      * @param type the event type (must not be <strong>null</strong>)
  46.      * @param lstnr the event listener (must not be <strong>null</strong>)
  47.      * @throws IllegalArgumentException if a required parameter is <strong>null</strong>
  48.      */
  49.     public EventListenerRegistrationData(final EventType<T> type, final EventListener<? super T> lstnr) {
  50.         if (type == null) {
  51.             throw new IllegalArgumentException("Event type must not be null!");
  52.         }
  53.         if (lstnr == null) {
  54.             throw new IllegalArgumentException("Listener to be registered must not be null!");
  55.         }

  56.         eventType = type;
  57.         listener = lstnr;
  58.     }

  59.     /**
  60.      * Compares this object with another one. Two instances of {@code EventListenerRegistrationData} are considered equal if
  61.      * they reference the same listener and event type.
  62.      *
  63.      * @param obj the object to be compared to
  64.      * @return a flag whether these objects are equal
  65.      */
  66.     @Override
  67.     public boolean equals(final Object obj) {
  68.         if (this == obj) {
  69.             return true;
  70.         }
  71.         if (!(obj instanceof EventListenerRegistrationData)) {
  72.             return false;
  73.         }

  74.         final EventListenerRegistrationData<?> c = (EventListenerRegistrationData<?>) obj;
  75.         return getListener() == c.getListener() && getEventType().equals(c.getEventType());
  76.     }

  77.     /**
  78.      * Gets the event type for this listener registration.
  79.      *
  80.      * @return the event type
  81.      */
  82.     public EventType<T> getEventType() {
  83.         return eventType;
  84.     }

  85.     /**
  86.      * Gets the listener this registration is about.
  87.      *
  88.      * @return the event listener
  89.      */
  90.     public EventListener<? super T> getListener() {
  91.         return listener;
  92.     }

  93.     @Override
  94.     public int hashCode() {
  95.         final int result = eventType.hashCode();
  96.         return HASH_FACTOR * result + listener.hashCode();
  97.     }
  98. }