001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.shiro.event.support;
020
021import java.util.Comparator;
022
023/**
024 * Compares two event listeners to determine the order in which they should be invoked when an event is dispatched.
025 * The lower the order, the sooner it will be invoked (the higher its precedence).  The higher the order, the later
026 * it will be invoked (the lower its precedence).
027 * <p/>
028 * TypedEventListeners have a higher precedence (i.e. a lower order) than standard EventListener instances.  Standard
029 * EventListener instances have the same order priority.
030 * <p/>
031 * When both objects being compared are TypedEventListeners, they are ordered according to the rules of the
032 * {@link EventClassComparator}, using the TypedEventListeners'
033 * {@link TypedEventListener#getEventType() eventType}.
034 *
035 * @since 1.3
036 */
037public class EventListenerComparator implements Comparator<EventListener> {
038
039    //event class comparator is stateless, so we can retain an instance:
040    private static final EventClassComparator EVENT_CLASS_COMPARATOR = new EventClassComparator();
041
042    public int compare(EventListener a, EventListener b) {
043        if (a == null) {
044            if (b == null) {
045                return 0;
046            } else {
047                return -1;
048            }
049        } else if (b == null) {
050            return 1;
051        } else if (a == b || a.equals(b)) {
052            return 0;
053        } else {
054            if (a instanceof TypedEventListener) {
055                TypedEventListener ta = (TypedEventListener)a;
056                if (b instanceof TypedEventListener) {
057                    TypedEventListener tb = (TypedEventListener)b;
058                    return EVENT_CLASS_COMPARATOR.compare(ta.getEventType(), tb.getEventType());
059                } else {
060                    return -1; //TypedEventListeners are 'less than' (higher priority) than non typed
061                }
062            } else {
063                if (b instanceof TypedEventListener) {
064                    return 1;
065                } else {
066                    return 0;
067                }
068            }
069        }
070    }
071}