View Javadoc

1   /*
2    * Copyright (C) The Apache Software Foundation. All rights reserved.
3    *
4    * This software is published under the terms of the Apache Software License
5    * version 1.1, a copy of which has been included with this distribution in
6    * the LICENSE file.
7    * 
8    * $Id: ListenerKey.java 155459 2005-02-26 13:24:44Z dirkv $
9    */
10  package org.apache.commons.messenger;
11  
12  import javax.jms.Destination;
13  import javax.jms.MessageListener;
14  
15  
16  /** <p><code>ListenerKey</code> is an implementation class allowing a Destination, 
17    * MessageListener and an optional selector to be used as a key to a Map so that 
18    * a single subscription can be easily associated with a MessageConsumer.
19    *
20    * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
21    * @version $Revision: 155459 $
22    */
23  class ListenerKey {
24  
25      private Destination destination;
26      private MessageListener messageListener;
27      private String selector;
28      private int hashCode;
29      
30      public ListenerKey(Destination destination, MessageListener messageListener) {
31          this( destination, messageListener, null );
32      }
33      
34      public ListenerKey(Destination destination, MessageListener messageListener, String selector) {
35          this.destination = destination;
36          this.messageListener = messageListener;
37          this.selector = selector;
38          this.hashCode = destination.hashCode() ^ messageListener.hashCode();
39          if ( selector != null ) {
40              this.hashCode ^= selector.hashCode();
41          }
42      }
43      
44      public int hashCode() {
45          return hashCode;
46      }
47      
48      public boolean equals(Object that) {
49          if ( this == that ) {
50              return true;
51          }
52          if (that instanceof ListenerKey) {
53              return equals((ListenerKey) that);
54          }
55          return false;
56      }
57      
58      public boolean equals(ListenerKey that) {
59          if ( this == that ) {
60              return true;
61          }
62          if ( this.hashCode == that.hashCode ) {
63              if ( this.destination.equals( that.destination ) && this.messageListener.equals( that.messageListener) ) {
64                  if ( this.selector == that.selector ) {
65                      return true;
66                  }
67                  return this.selector != null && this.selector.equals( that.selector );
68              }
69          }
70          return false;
71      }
72          
73  }
74