View Javadoc
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  /**
20   * <p>
21   * A data class holding information about an event listener registration.
22   * </p>
23   * <p>
24   * An instance of this class stores all information required to determine whether a specific event listener is to be
25   * invoked for a given event. The class is used internally by {@link EventListenerList}, but is also useful in general
26   * when information about event listeners is to be stored.
27   * </p>
28   * <p>
29   * Implementation note: Instances of this class are immutable and can safely be shared between multiple threads or
30   * components.
31   * </p>
32   *
33   * @param <T> the type of events processed by the listener
34   * @since 2.0
35   */
36  public final class EventListenerRegistrationData<T extends Event> {
37      /** Constant for the factor used by the calculation of the hash code. */
38      private static final int HASH_FACTOR = 31;
39  
40      /** The event type. */
41      private final EventType<T> eventType;
42  
43      /** The event listener. */
44      private final EventListener<? super T> listener;
45  
46      /**
47       * Creates a new instance of {@code EventListenerRegistrationData}.
48       *
49       * @param type the event type (must not be <strong>null</strong>)
50       * @param lstnr the event listener (must not be <strong>null</strong>)
51       * @throws IllegalArgumentException if a required parameter is <strong>null</strong>
52       */
53      public EventListenerRegistrationData(final EventType<T> type, final EventListener<? super T> lstnr) {
54          if (type == null) {
55              throw new IllegalArgumentException("Event type must not be null!");
56          }
57          if (lstnr == null) {
58              throw new IllegalArgumentException("Listener to be registered must not be null!");
59          }
60  
61          eventType = type;
62          listener = lstnr;
63      }
64  
65      /**
66       * Compares this object with another one. Two instances of {@code EventListenerRegistrationData} are considered equal if
67       * they reference the same listener and event type.
68       *
69       * @param obj the object to be compared to
70       * @return a flag whether these objects are equal
71       */
72      @Override
73      public boolean equals(final Object obj) {
74          if (this == obj) {
75              return true;
76          }
77          if (!(obj instanceof EventListenerRegistrationData)) {
78              return false;
79          }
80  
81          final EventListenerRegistrationData<?> c = (EventListenerRegistrationData<?>) obj;
82          return getListener() == c.getListener() && getEventType().equals(c.getEventType());
83      }
84  
85      /**
86       * Gets the event type for this listener registration.
87       *
88       * @return the event type
89       */
90      public EventType<T> getEventType() {
91          return eventType;
92      }
93  
94      /**
95       * Gets the listener this registration is about.
96       *
97       * @return the event listener
98       */
99      public EventListener<? super T> getListener() {
100         return listener;
101     }
102 
103     @Override
104     public int hashCode() {
105         final int result = eventType.hashCode();
106         return HASH_FACTOR * result + listener.hashCode();
107     }
108 }