1 /*
2  * Copyright 2002-2018 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 module hunt.stomp.simp.SimpMessageMappingInfo;
18 
19 import hunt.stomp.simp.SimpMessageTypeMessageCondition;
20 import hunt.stomp.Message;
21 import hunt.stomp.handler.DestinationPatternsMessageCondition;
22 import hunt.stomp.handler.MessageCondition;
23 
24 /**
25  * {@link MessageCondition} for SImple Messaging Protocols. Encapsulates the following
26  * request mapping conditions:
27  * <ol>
28  * <li>{@link SimpMessageTypeMessageCondition}
29  * <li>{@link DestinationPatternsMessageCondition}
30  * </ol>
31  *
32  * @author Rossen Stoyanchev
33  * @since 4.0
34  */
35 class SimpMessageMappingInfo : MessageCondition!(SimpMessageMappingInfo) {
36 
37 	private SimpMessageTypeMessageCondition messageTypeMessageCondition;
38 
39 	private DestinationPatternsMessageCondition destinationConditions;
40 
41 
42 	this(SimpMessageTypeMessageCondition messageTypeMessageCondition,
43 			DestinationPatternsMessageCondition destinationConditions) {
44 
45 		this.messageTypeMessageCondition = messageTypeMessageCondition;
46 		this.destinationConditions = destinationConditions;
47 	}
48 
49 
50 	SimpMessageTypeMessageCondition getMessageTypeMessageCondition() {
51 		return this.messageTypeMessageCondition;
52 	}
53 
54 	DestinationPatternsMessageCondition getDestinationConditions() {
55 		return this.destinationConditions;
56 	}
57 
58 
59 	override
60 	SimpMessageMappingInfo combine(SimpMessageMappingInfo other) {
61 		SimpMessageTypeMessageCondition typeCond =
62 				this.getMessageTypeMessageCondition().combine(other.getMessageTypeMessageCondition());
63 		DestinationPatternsMessageCondition destCond =
64 				this.destinationConditions.combine(other.getDestinationConditions());
65 		return new SimpMessageMappingInfo(typeCond, destCond);
66 	}
67 
68 	override
69 	
70 	SimpMessageMappingInfo getMatchingCondition(MessageBase message) {
71 		SimpMessageTypeMessageCondition typeCond = this.messageTypeMessageCondition.getMatchingCondition(message);
72 		if (typeCond is null) {
73 			return null;
74 		}
75 		DestinationPatternsMessageCondition destCond = this.destinationConditions.getMatchingCondition(message);
76 		if (destCond is null) {
77 			return null;
78 		}
79 		return new SimpMessageMappingInfo(typeCond, destCond);
80 	}
81 
82 	override
83 	int compareTo(SimpMessageMappingInfo other, MessageBase message) {
84 		int result = this.messageTypeMessageCondition.compareTo(other.messageTypeMessageCondition, message);
85 		if (result != 0) {
86 			return result;
87 		}
88 		result = this.destinationConditions.compareTo(other.destinationConditions, message);
89 		if (result != 0) {
90 			return result;
91 		}
92 		return 0;
93 	}
94 
95 
96 	override
97 	bool opEquals(Object other) {
98 		if (this is other) {
99 			return true;
100 		}
101 		SimpMessageMappingInfo otherInfo = cast(SimpMessageMappingInfo) other;
102         if(otherInfo is null)
103             return false;
104 
105 		return (this.destinationConditions == (otherInfo.destinationConditions) &&
106 				this.messageTypeMessageCondition == (otherInfo.messageTypeMessageCondition));
107 	}
108 
109 	override
110 	size_t toHash() @trusted nothrow {
111 		return (this.destinationConditions.toHash() * 31 + this.messageTypeMessageCondition.toHash());
112 	}
113 
114 	override
115 	string toString() {
116 		return "{" ~ this.destinationConditions.toString()
117              ~ ",messageType=" ~ this.messageTypeMessageCondition.toString() ~ "}";
118 	}
119 
120 }