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: IdKeyComparator.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.io.Serializable;
53  import java.util.Comparator;
54  
55  import javax.jms.JMSException;
56  
57  
58  /***
59   * A comparator class that iplements a string comparison.
60   *
61   *
62   * <P>Note this class assumes that id's are in some sort of order, such that
63   * an earlier generated id is less than a later generated id.
64   *
65   * @version     $Revision: 1.4 $ $Date: 2003/08/17 01:32:25 $
66   * @author      <a href="mailto:mourikis@exolab.org">Jim Mourikis</a>
67   */
68  public class IdKeyComparator implements Comparator, Serializable {
69  
70      // All Message ID's are prefixed with "ID:"
71      private static final int PREFIX_LENGTH = 3;
72  
73      /***
74       * Default constructor does nothing.
75       *
76       */
77      public IdKeyComparator() {
78  
79      }
80  
81  
82      /***
83       * Implements the compare operator.
84       * Compares its two arguments for order. Returns a negative integer, zero,
85       * or a positive integer as the first argument is less than, equal to,
86       * or greater than the second.
87       *
88       * @param Object object 1 to compare
89       * @param Object object 2 ro compare to.
90       * @return int returns -1, 0, 1 if the first argument is less than,
91       *		   equal to, or greater than the second.
92       * @exception ClassCastException if object types are unknown
93       *
94       */
95      public int compare(Object o1, Object o2)
96          throws ClassCastException, NullPointerException {
97          int status = 0;
98  
99          if (o1 == null && o2 == null)
100             status = 0;
101         else if (o1 == null)
102             status = -1;
103         else if (o2 == null)
104             status = 1;
105         else if (o1 instanceof String &&
106             o2 instanceof String) {
107             String t1 = (String) o1;
108             String t2 = (String) o2;
109             try {
110                 long l1 = strip(t1);
111                 long l2 = strip(t2);
112 
113                 if (l1 == l2)
114                     status = 0;
115                 else if (l1 < l2)
116                     status = -1;
117                 else
118                     status = 1;
119             } catch (NumberFormatException err) {
120                 System.err.println("Invalid IDs " + t1 + ", " + t2);
121             }
122         } else {
123             throw new ClassCastException("Unknown Object");
124         }
125         return status;
126     }
127 
128 
129     /***
130      * Indicates whether some other object is "equal to" this Comparator.
131      * This method must obey the general contract of Object.equals(Object).
132      *
133      * @param Object the object to compare to
134      * @return boolean true iff objects are equal
135      *
136      */
137     public boolean equals(Object ob) {
138         boolean status = false;
139 
140         if (ob instanceof IdKeyComparator) status = true;
141 
142         return status;
143     }
144 
145 
146     /***
147      * Strip the "ID:" prefix from the string, and return the long number
148      * remaininng.
149      *
150      * @param st The string to strip.
151      * @return long the remaing long integer.
152      *
153      */
154     private long strip(String st) throws NumberFormatException {
155         return Long.parseLong(st.substring(PREFIX_LENGTH));
156     }
157 } // End IdKeyComparator
158 
159 
160