From 73f0e97531cdd9b596222e26fa256c66af1982df Mon Sep 17 00:00:00 2001 From: James Strachan Date: Tue, 25 Apr 2006 14:05:03 +0000 Subject: [PATCH] added helper classes for comparing messages git-svn-id: https://svn.apache.org/repos/asf/incubator/activemq/trunk@396891 13f79535-47bb-0310-9956-ffa450edef68 --- .../util/MessageComparatorSupport.java | 48 ++++++++++++++++ .../util/MessageDestinationComparator.java | 56 +++++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 activemq-core/src/main/java/org/apache/activemq/util/MessageComparatorSupport.java create mode 100644 activemq-core/src/main/java/org/apache/activemq/util/MessageDestinationComparator.java diff --git a/activemq-core/src/main/java/org/apache/activemq/util/MessageComparatorSupport.java b/activemq-core/src/main/java/org/apache/activemq/util/MessageComparatorSupport.java new file mode 100644 index 0000000000..7062260062 --- /dev/null +++ b/activemq-core/src/main/java/org/apache/activemq/util/MessageComparatorSupport.java @@ -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; + } + +} diff --git a/activemq-core/src/main/java/org/apache/activemq/util/MessageDestinationComparator.java b/activemq-core/src/main/java/org/apache/activemq/util/MessageDestinationComparator.java new file mode 100644 index 0000000000..4f698abe72 --- /dev/null +++ b/activemq-core/src/main/java/org/apache/activemq/util/MessageDestinationComparator.java @@ -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; + } + + + +}