    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 *     http://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
    025 * whether a specific event listener is to be invoked for a given event. The
    026 * class is used internally by {@link EventListenerList}, but is also useful in
    027 * general when information about event listeners is to be stored.
    028 * </p>
    029 * <p>
    030 * Implementation note: Instances of this class are immutable and can safely be
    031 * shared between multiple threads or components.
    032 * </p>
    033 *
    034 * @version $Id: EventListenerRegistrationData.java 1842194 2018-09-27 22:24:23Z ggregory $
    035 * @since 2.0
    036 * @param <T> the type of events processed by the listener
    037 */
    038public final class EventListenerRegistrationData<T extends Event>
    039{
    040    /** Constant for the factor used by the calculation of the hash code. */
    041    private static final int HASH_FACTOR = 31;
    042
    043    /** The event type. */
    044    private final EventType<T> eventType;
    045
    046    /** The event listener. */
    047    private final EventListener<? super T> listener;
    048
    049    /**
    050     * Creates a new instance of {@code EventListenerRegistrationData}.
    051     *
    052     * @param type the event type (must not be <b>null</b>)
    053     * @param lstnr the event listener (must not be <b>null</b>)
    054     * @throws IllegalArgumentException if a required parameter is <b>null</b>
    055     */
    056    public EventListenerRegistrationData(final EventType<T> type,
    057            final EventListener<? super T> lstnr)
    058    {
    059        if (type == null)
    060        {
    061            throw new IllegalArgumentException("Event type must not be null!");
    062        }
    063        if (lstnr == null)
    064        {
    065            throw new IllegalArgumentException(
    066                    "Listener to be registered must not be null!");
    067        }
    068
    069        eventType = type;
    070        listener = lstnr;
    071    }
    072
    073    /**
    074     * Returns the event type for this listener registration.
    075     *
    076     * @return the event type
    077     */
    078    public EventType<T> getEventType()
    079    {
    080        return eventType;
    081    }
    082
    083    /**
    084     * Returns the listener this registration is about.
    085     *
    086     * @return the event listener
    087     */
    088    public EventListener<? super T> getListener()
    089    {
    090        return listener;
    091    }
    092
    093    @Override
    094    public int hashCode()
    095    {
    096        int result = eventType.hashCode();
    097        result = HASH_FACTOR * result + listener.hashCode();
    098        return result;
    099    }
    100
    101    /**
    102     * Compares this object with another one. Two instances of
    103     * {@code EventListenerRegistrationData} are considered equal if they
    104     * reference the same listener and event type.
    105     *
    106     * @param obj the object to be compared to
    107     * @return a flag whether these objects are equal
    108     */
    109    @Override
    110    public boolean equals(final Object obj)
    111    {
    112        if (this == obj)
    113        {
    114            return true;
    115        }
    116        if (!(obj instanceof EventListenerRegistrationData))
    117        {
    118            return false;
    119        }
    120
    121        final EventListenerRegistrationData<?> c =
    122                (EventListenerRegistrationData<?>) obj;
    123        return getListener() == c.getListener()
    124                && getEventType().equals(c.getEventType());
    125    }
    126}