added helper classes for comparing messages

git-svn-id: https://svn.apache.org/repos/asf/incubator/activemq/trunk@396891 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
James Strachan 2006-04-25 14:05:03 +00:00
parent a306ba3061
commit 73f0e97531
2 changed files with 104 additions and 0 deletions

View File

@ -0,0 +1,48 @@
/**
*
* Copyright 2005-2006 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.activemq.util;
import javax.jms.Message;
import java.util.Comparator;
/**
* A base class for comparators which works on JMS {@link Message} objects
*
* @version $Revision$
*/
public abstract class MessageComparatorSupport implements Comparator {
public int compare(Object object1, Object object2) {
Message command1 = (Message) object1;
Message command2 = (Message) object2;
return compareMessages(command1, command2);
}
protected abstract int compareMessages(Message message1, Message message2);
protected int compareComparators(Comparable comparable, Comparable comparable2) {
if (comparable != null) {
return comparable.compareTo(comparable2);
}
else if (comparable2 != null) {
return comparable2.compareTo(comparable) * -1;
}
return 0;
}
}

View File

@ -0,0 +1,56 @@
/**
*
* Copyright 2005-2006 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.activemq.util;
import org.apache.activemq.command.ActiveMQMessage;
import javax.jms.*;
/**
* A comparator which works on SendCommand objects to compare the destinations
*
* @version $Revision$
*/
public class MessageDestinationComparator extends MessageComparatorSupport {
protected int compareMessages(Message message1, Message message2) {
return compareComparators(getComparable(getDestination(message1)), getComparable(getDestination(message2)));
}
protected Destination getDestination(Message message) {
if (message instanceof ActiveMQMessage) {
ActiveMQMessage amqMessage = (ActiveMQMessage) message;
return amqMessage.getDestination();
}
try {
return message.getJMSDestination();
}
catch (JMSException e) {
return null;
}
}
protected Comparable getComparable(Destination destination) {
if (destination != null) {
return destination.toString();
}
return null;
}
}