    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 * @since 2.0
    035 * @param <T> the type of events processed by the listener
    036 */
    037public final class EventListenerRegistrationData<T extends Event>
    038{
    039    /** Constant for the factor used by the calculation of the hash code. */
    040    private static final int HASH_FACTOR = 31;
    041
    042    /** The event type. */
    043    private final EventType<T> eventType;
    044
    045    /** The event listener. */
    046    private final EventListener<? super T> listener;
    047
    048    /**
    049     * Creates a new instance of {@code EventListenerRegistrationData}.
    050     *
    051     * @param type the event type (must not be <b>null</b>)
    052     * @param lstnr the event listener (must not be <b>null</b>)
    053     * @throws IllegalArgumentException if a required parameter is <b>null</b>
    054     */
    055    public EventListenerRegistrationData(final EventType<T> type,
    056            final EventListener<? super T> lstnr)
    057    {
    058        if (type == null)
    059        {
    060            throw new IllegalArgumentException("Event type must not be null!");
    061        }
    062        if (lstnr == null)
    063        {
    064            throw new IllegalArgumentException(
    065                    "Listener to be registered must not be null!");
    066        }
    067
    068        eventType = type;
    069        listener = lstnr;
    070    }
    071
    072    /**
    073     * Returns the event type for this listener registration.
    074     *
    075     * @return the event type
    076     */
    077    public EventType<T> getEventType()
    078    {
    079        return eventType;
    080    }
    081
    082    /**
    083     * Returns the listener this registration is about.
    084     *
    085     * @return the event listener
    086     */
    087    public EventListener<? super T> getListener()
    088    {
    089        return listener;
    090    }
    091
    092    @Override
    093    public int hashCode()
    094    {
    095        int result = eventType.hashCode();
    096        result = HASH_FACTOR * result + listener.hashCode();
    097        return result;
    098    }
    099
    100    /**
    101     * Compares this object with another one. Two instances of
    102     * {@code EventListenerRegistrationData} are considered equal if they
    103     * reference the same listener and event type.
    104     *
    105     * @param obj the object to be compared to
    106     * @return a flag whether these objects are equal
    107     */
    108    @Override
    109    public boolean equals(final Object obj)
    110    {
    111        if (this == obj)
    112        {
    113            return true;
    114        }
    115        if (!(obj instanceof EventListenerRegistrationData))
    116        {
    117            return false;
    118        }
    119
    120        final EventListenerRegistrationData<?> c =
    121                (EventListenerRegistrationData<?>) obj;
    122        return getListener() == c.getListener()
    123                && getEventType().equals(c.getEventType());
    124    }
    125}