001/*
002 * Copyright 2002-2014 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.messaging.simp;
018
019import org.springframework.messaging.Message;
020import org.springframework.messaging.handler.DestinationPatternsMessageCondition;
021import org.springframework.messaging.handler.MessageCondition;
022
023/**
024 * Encapsulates the following request mapping conditions:
025 * <ol>
026 * <li>{@link SimpMessageTypeMessageCondition}
027 * <li>{@link DestinationPatternsMessageCondition}
028 * </ol>
029 *
030 * @author Rossen Stoyanchev
031 * @since 4.0
032 */
033public class SimpMessageMappingInfo implements MessageCondition<SimpMessageMappingInfo> {
034
035        private final SimpMessageTypeMessageCondition messageTypeMessageCondition;
036
037        private final DestinationPatternsMessageCondition destinationConditions;
038
039
040        public SimpMessageMappingInfo(SimpMessageTypeMessageCondition messageTypeMessageCondition,
041                        DestinationPatternsMessageCondition destinationConditions) {
042
043                this.messageTypeMessageCondition = messageTypeMessageCondition;
044                this.destinationConditions = destinationConditions;
045        }
046
047
048        public SimpMessageTypeMessageCondition getMessageTypeMessageCondition() {
049                return this.messageTypeMessageCondition;
050        }
051
052        public DestinationPatternsMessageCondition getDestinationConditions() {
053                return this.destinationConditions;
054        }
055
056
057        @Override
058        public SimpMessageMappingInfo combine(SimpMessageMappingInfo other) {
059                SimpMessageTypeMessageCondition typeCond =
060                                this.getMessageTypeMessageCondition().combine(other.getMessageTypeMessageCondition());
061                DestinationPatternsMessageCondition destCond =
062                                this.destinationConditions.combine(other.getDestinationConditions());
063                return new SimpMessageMappingInfo(typeCond, destCond);
064        }
065
066        @Override
067        public SimpMessageMappingInfo getMatchingCondition(Message<?> message) {
068                SimpMessageTypeMessageCondition typeCond = this.messageTypeMessageCondition.getMatchingCondition(message);
069                if (typeCond == null) {
070                        return null;
071                }
072                DestinationPatternsMessageCondition destCond = this.destinationConditions.getMatchingCondition(message);
073                if (destCond == null) {
074                        return null;
075                }
076                return new SimpMessageMappingInfo(typeCond, destCond);
077        }
078
079        @Override
080        public int compareTo(SimpMessageMappingInfo other, Message<?> message) {
081                int result = this.messageTypeMessageCondition.compareTo(other.messageTypeMessageCondition, message);
082                if (result != 0) {
083                        return result;
084                }
085                result = this.destinationConditions.compareTo(other.destinationConditions, message);
086                if (result != 0) {
087                        return result;
088                }
089                return 0;
090        }
091
092
093        @Override
094        public boolean equals(Object obj) {
095                if (this == obj) {
096                        return true;
097                }
098                if (obj != null && obj instanceof SimpMessageMappingInfo) {
099                        SimpMessageMappingInfo other = (SimpMessageMappingInfo) obj;
100                        return (this.destinationConditions.equals(other.destinationConditions) &&
101                                        this.messageTypeMessageCondition.equals(other.messageTypeMessageCondition));
102                }
103                return false;
104        }
105
106        @Override
107        public int hashCode() {
108                return (this.destinationConditions.hashCode() * 31 + this.messageTypeMessageCondition.hashCode());
109        }
110
111        @Override
112        public String toString() {
113                StringBuilder builder = new StringBuilder("{");
114                builder.append(this.destinationConditions);
115                builder.append(",messageType=").append(this.messageTypeMessageCondition);
116                builder.append('}');
117                return builder.toString();
118        }
119
120}