001/** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.activemq.util; 018 019import java.io.Serializable; 020import java.util.Comparator; 021 022import javax.jms.Message; 023 024/** 025 * A base class for comparators which works on JMS {@link Message} objects 026 * 027 * 028 */ 029public abstract class MessageComparatorSupport implements Comparator, Serializable { 030 031 public int compare(Object object1, Object object2) { 032 Message command1 = (Message)object1; 033 Message command2 = (Message)object2; 034 return compareMessages(command1, command2); 035 } 036 037 protected abstract int compareMessages(Message message1, Message message2); 038 039 protected int compareComparators(final Comparable comparable, final Comparable comparable2) { 040 if (comparable == null && comparable2 == null) { 041 return 0; 042 } else if (comparable != null) { 043 if (comparable2 == null) { 044 return 1; 045 } 046 return comparable.compareTo(comparable2); 047 } else if (comparable2 != null) { 048 if (comparable == null) { 049 return -11; 050 } 051 return comparable2.compareTo(comparable) * -1; 052 } 053 return 0; 054 } 055 056}