View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.shiro.event.support;
20  
21  import java.util.Comparator;
22  
23  /**
24   * Compares two event listeners to determine the order in which they should be invoked when an event is dispatched.
25   * The lower the order, the sooner it will be invoked (the higher its precedence).  The higher the order, the later
26   * it will be invoked (the lower its precedence).
27   * <p/>
28   * TypedEventListeners have a higher precedence (i.e. a lower order) than standard EventListener instances.  Standard
29   * EventListener instances have the same order priority.
30   * <p/>
31   * When both objects being compared are TypedEventListeners, they are ordered according to the rules of the
32   * {@link EventClassComparator}, using the TypedEventListeners'
33   * {@link TypedEventListener#getEventType() eventType}.
34   *
35   * @since 1.3
36   */
37  public class EventListenerComparator implements Comparator<EventListener> {
38  
39      //event class comparator is stateless, so we can retain an instance:
40      private static final EventClassComparator EVENT_CLASS_COMPARATOR = new EventClassComparator();
41  
42      public int compare(EventListener a, EventListener b) {
43          if (a == null) {
44              if (b == null) {
45                  return 0;
46              } else {
47                  return -1;
48              }
49          } else if (b == null) {
50              return 1;
51          } else if (a == b || a.equals(b)) {
52              return 0;
53          } else {
54              if (a instanceof TypedEventListener) {
55                  TypedEventListener ta = (TypedEventListener)a;
56                  if (b instanceof TypedEventListener) {
57                      TypedEventListener tb = (TypedEventListener)b;
58                      return EVENT_CLASS_COMPARATOR.compare(ta.getEventType(), tb.getEventType());
59                  } else {
60                      return -1; //TypedEventListeners are 'less than' (higher priority) than non typed
61                  }
62              } else {
63                  if (b instanceof TypedEventListener) {
64                      return 1;
65                  } else {
66                      return 0;
67                  }
68              }
69          }
70      }
71  }