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 * @since 2.0 34 * @param <T> the type of events processed by the listener 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 <b>null</b>) 50 * @param lstnr the event listener (must not be <b>null</b>) 51 * @throws IllegalArgumentException if a required parameter is <b>null</b> 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 * Gets the event type for this listener registration. 67 * 68 * @return the event type 69 */ 70 public EventType<T> getEventType() { 71 return eventType; 72 } 73 74 /** 75 * Gets the listener this registration is about. 76 * 77 * @return the event listener 78 */ 79 public EventListener<? super T> getListener() { 80 return listener; 81 } 82 83 @Override 84 public int hashCode() { 85 final int result = eventType.hashCode(); 86 return HASH_FACTOR * result + listener.hashCode(); 87 } 88 89 /** 90 * Compares this object with another one. Two instances of {@code EventListenerRegistrationData} are considered equal if 91 * they reference the same listener and event type. 92 * 93 * @param obj the object to be compared to 94 * @return a flag whether these objects are equal 95 */ 96 @Override 97 public boolean equals(final Object obj) { 98 if (this == obj) { 99 return true; 100 } 101 if (!(obj instanceof EventListenerRegistrationData)) { 102 return false; 103 } 104 105 final EventListenerRegistrationData<?> c = (EventListenerRegistrationData<?>) obj; 106 return getListener() == c.getListener() && getEventType().equals(c.getEventType()); 107 } 108 }