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