📄 idkeycomparator.java
字号:
/**
* Redistribution and use of this software and associated documentation
* ("Software"), with or without modification, are permitted provided
* that the following conditions are met:
*
* 1. Redistributions of source code must retain copyright
* statements and notices. Redistributions must also contain a
* copy of this document.
*
* 2. Redistributions in binary form must reproduce the
* above copyright notice, this list of conditions and the
* following disclaimer in the documentation and/or other
* materials provided with the distribution.
*
* 3. The name "Exolab" must not be used to endorse or promote
* products derived from this Software without prior written
* permission of Exoffice Technologies. For written permission,
* please contact info@exolab.org.
*
* 4. Products derived from this Software may not be called "Exolab"
* nor may "Exolab" appear in their names without prior written
* permission of Exoffice Technologies. Exolab is a registered
* trademark of Exoffice Technologies.
*
* 5. Due credit should be given to the Exolab Project
* (http://www.exolab.org/).
*
* THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Copyright 2000 (C) Exoffice Technologies Inc. All Rights Reserved.
*
* $Id: IdKeyComparator.java,v 1.4 2003/08/17 01:32:25 tanderson Exp $
*
* Date Author Changes
* $Date jimm Created
*/
package org.exolab.jms.persistence;
import java.io.Serializable;
import java.util.Comparator;
import javax.jms.JMSException;
/**
* A comparator class that iplements a string comparison.
*
*
* <P>Note this class assumes that id's are in some sort of order, such that
* an earlier generated id is less than a later generated id.
*
* @version $Revision: 1.4 $ $Date: 2003/08/17 01:32:25 $
* @author <a href="mailto:mourikis@exolab.org">Jim Mourikis</a>
*/
public class IdKeyComparator implements Comparator, Serializable {
// All Message ID's are prefixed with "ID:"
private static final int PREFIX_LENGTH = 3;
/**
* Default constructor does nothing.
*
*/
public IdKeyComparator() {
}
/**
* Implements the compare operator.
* Compares its two arguments for order. Returns a negative integer, zero,
* or a positive integer as the first argument is less than, equal to,
* or greater than the second.
*
* @param Object object 1 to compare
* @param Object object 2 ro compare to.
* @return int returns -1, 0, 1 if the first argument is less than,
* equal to, or greater than the second.
* @exception ClassCastException if object types are unknown
*
*/
public int compare(Object o1, Object o2)
throws ClassCastException, NullPointerException {
int status = 0;
if (o1 == null && o2 == null)
status = 0;
else if (o1 == null)
status = -1;
else if (o2 == null)
status = 1;
else if (o1 instanceof String &&
o2 instanceof String) {
String t1 = (String) o1;
String t2 = (String) o2;
try {
long l1 = strip(t1);
long l2 = strip(t2);
if (l1 == l2)
status = 0;
else if (l1 < l2)
status = -1;
else
status = 1;
} catch (NumberFormatException err) {
System.err.println("Invalid IDs " + t1 + ", " + t2);
}
} else {
throw new ClassCastException("Unknown Object");
}
return status;
}
/**
* Indicates whether some other object is "equal to" this Comparator.
* This method must obey the general contract of Object.equals(Object).
*
* @param Object the object to compare to
* @return boolean true iff objects are equal
*
*/
public boolean equals(Object ob) {
boolean status = false;
if (ob instanceof IdKeyComparator) status = true;
return status;
}
/**
* Strip the "ID:" prefix from the string, and return the long number
* remaininng.
*
* @param st The string to strip.
* @return long the remaing long integer.
*
*/
private long strip(String st) throws NumberFormatException {
return Long.parseLong(st.substring(PREFIX_LENGTH));
}
} // End IdKeyComparator
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -