001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * https://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.configuration2.event; 018 019/** 020 * <p> 021 * A data class holding information about an event listener registration. 022 * </p> 023 * <p> 024 * An instance of this class stores all information required to determine whether a specific event listener is to be 025 * invoked for a given event. The class is used internally by {@link EventListenerList}, but is also useful in general 026 * when information about event listeners is to be stored. 027 * </p> 028 * <p> 029 * Implementation note: Instances of this class are immutable and can safely be shared between multiple threads or 030 * components. 031 * </p> 032 * 033 * @param <T> the type of events processed by the listener 034 * @since 2.0 035 */ 036public final class EventListenerRegistrationData<T extends Event> { 037 038 /** Constant for the factor used by the calculation of the hash code. */ 039 private static final int HASH_FACTOR = 31; 040 041 /** The event type. */ 042 private final EventType<T> eventType; 043 044 /** The event listener. */ 045 private final EventListener<? super T> listener; 046 047 /** 048 * Creates a new instance of {@code EventListenerRegistrationData}. 049 * 050 * @param type the event type (must not be <strong>null</strong>) 051 * @param lstnr the event listener (must not be <strong>null</strong>) 052 * @throws IllegalArgumentException if a required parameter is <strong>null</strong> 053 */ 054 public EventListenerRegistrationData(final EventType<T> type, final EventListener<? super T> lstnr) { 055 if (type == null) { 056 throw new IllegalArgumentException("Event type must not be null!"); 057 } 058 if (lstnr == null) { 059 throw new IllegalArgumentException("Listener to be registered must not be null!"); 060 } 061 062 eventType = type; 063 listener = lstnr; 064 } 065 066 /** 067 * Compares this object with another one. Two instances of {@code EventListenerRegistrationData} are considered equal if 068 * they reference the same listener and event type. 069 * 070 * @param obj the object to be compared to 071 * @return a flag whether these objects are equal 072 */ 073 @Override 074 public boolean equals(final Object obj) { 075 if (this == obj) { 076 return true; 077 } 078 if (!(obj instanceof EventListenerRegistrationData)) { 079 return false; 080 } 081 082 final EventListenerRegistrationData<?> c = (EventListenerRegistrationData<?>) obj; 083 return getListener() == c.getListener() && getEventType().equals(c.getEventType()); 084 } 085 086 /** 087 * Gets the event type for this listener registration. 088 * 089 * @return the event type 090 */ 091 public EventType<T> getEventType() { 092 return eventType; 093 } 094 095 /** 096 * Gets the listener this registration is about. 097 * 098 * @return the event listener 099 */ 100 public EventListener<? super T> getListener() { 101 return listener; 102 } 103 104 @Override 105 public int hashCode() { 106 final int result = eventType.hashCode(); 107 return HASH_FACTOR * result + listener.hashCode(); 108 } 109}