    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     * Compares this object with another one. Two instances of {@code EventListenerRegistrationData} are considered equal if
    067     * they reference the same listener and event type.
    068     *
    069     * @param obj the object to be compared to
    070     * @return a flag whether these objects are equal
    071     */
    072    @Override
    073    public boolean equals(final Object obj) {
    074        if (this == obj) {
    075            return true;
    076        }
    077        if (!(obj instanceof EventListenerRegistrationData)) {
    078            return false;
    079        }
    080
    081        final EventListenerRegistrationData<?> c = (EventListenerRegistrationData<?>) obj;
    082        return getListener() == c.getListener() && getEventType().equals(c.getEventType());
    083    }
    084
    085    /**
    086     * Gets the event type for this listener registration.
    087     *
    088     * @return the event type
    089     */
    090    public EventType<T> getEventType() {
    091        return eventType;
    092    }
    093
    094    /**
    095     * Gets the listener this registration is about.
    096     *
    097     * @return the event listener
    098     */
    099    public EventListener<? super T> getListener() {
    100        return listener;
    101    }
    102
    103    @Override
    104    public int hashCode() {
    105        final int result = eventType.hashCode();
    106        return HASH_FACTOR * result + listener.hashCode();
    107    }
    108}