View Javadoc

1   /***
2    * Redistribution and use of this software and associated documentation
3    * ("Software"), with or without modification, are permitted provided
4    * that the following conditions are met:
5    *
6    * 1. Redistributions of source code must retain copyright
7    *    statements and notices.  Redistributions must also contain a
8    *    copy of this document.
9    *
10   * 2. Redistributions in binary form must reproduce the
11   *    above copyright notice, this list of conditions and the
12   *    following disclaimer in the documentation and/or other
13   *    materials provided with the distribution.
14   *
15   * 3. The name "Exolab" must not be used to endorse or promote
16   *    products derived from this Software without prior written
17   *    permission of Exoffice Technologies.  For written permission,
18   *    please contact info@exolab.org.
19   *
20   * 4. Products derived from this Software may not be called "Exolab"
21   *    nor may "Exolab" appear in their names without prior written
22   *    permission of Exoffice Technologies. Exolab is a registered
23   *    trademark of Exoffice Technologies.
24   *
25   * 5. Due credit should be given to the Exolab Project
26   *    (http://www.exolab.org/).
27   *
28   * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29   * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
32   * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39   * OF THE POSSIBILITY OF SUCH DAMAGE.
40   *
41   * Copyright 2000 (C) Exoffice Technologies Inc. All Rights Reserved.
42   *
43   * $Id: IdMessageComparator.java,v 1.4 2003/08/17 01:32:25 tanderson Exp $
44   *
45   * Date         Author  Changes
46   * $Date	    jimm    Created
47   */
48  
49  
50  package org.exolab.jms.persistence;
51  
52  import java.util.Comparator;
53  
54  import javax.jms.JMSException;
55  
56  
57  /***
58   * A comparator class that iplements a message id comparison for
59   * PersistentMessage.
60   *
61   * <P>Note this class assumes that id's are in some sort of order, such that
62   * an earlier generated id is less than a later generated id.
63   *
64   * @version     $Revision: 1.4 $ $Date: 2003/08/17 01:32:25 $
65   * @author      <a href="mailto:mourikis@exolab.org">Jim Mourikis</a>
66   */
67  public class IdMessageComparator implements Comparator {
68  
69      /***
70       * Default constructor does nothing.
71       *
72       */
73      public IdMessageComparator() {
74  
75      }
76  
77  
78      /***
79       * Implements the compare operator.
80       * Compares its two arguments for order. Returns a negative integer, zero,
81       * or a positive integer as the first argument is less than, equal to,
82       * or greater than the second.
83       *
84       * @param Object object 1 to compare
85       * @param Object object 2 ro compare to.
86       * @return int returns -1, 0, 1 if the first argument is less than,
87       *		   equal to, or greater than the second.
88       * @exception ClassCastException if object types are unknown
89       *
90       */
91      public int compare(Object o1, Object o2)
92          throws ClassCastException, NullPointerException {
93          int status;
94  
95          if (o1 == null && o2 == null)
96              status = 0;
97          else if (o1 == null)
98              status = -1;
99          else if (o2 == null)
100             status = 1;
101         else if (o1 instanceof PersistentMessage &&
102             o2 instanceof PersistentMessage) {
103             PersistentMessage m1 = (PersistentMessage) o1;
104             PersistentMessage m2 = (PersistentMessage) o2;
105             String t1;
106             String t2;
107 
108             try {
109                 t1 = m1.getMessage().getJMSMessageID();
110             } catch (JMSException e1) {
111                 t1 = "";
112             }
113             try {
114                 t2 = m2.getMessage().getJMSMessageID();
115             } catch (JMSException e2) {
116                 t2 = "";
117             }
118             try {
119                 status = t1.compareTo(t2);
120             } catch (NullPointerException err) {
121                 // should never happen but!
122                 System.err.println("Messages constain invalid id's");
123                 throw err;
124             }
125         } else {
126             throw new ClassCastException("Unknown Object");
127         }
128         return status;
129     }
130 
131 
132     /***
133      * Indicates whether some other object is "equal to" this Comparator.
134      * This method must obey the general contract of Object.equals(Object).
135      *
136      * @param Object the object to compare to
137      * @return boolean true iff objects are equal
138      *
139      */
140     public boolean equals(Object ob) {
141         boolean status = false;
142 
143         if (ob instanceof IdMessageComparator) status = true;
144 
145         return status;
146     }
147 
148 
149 } // End IdMessageComparator