1 /*
2  * Copyright 2002-2017 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.SimpMessageTypeMessageCondition;
18 
19 import hunt.stomp.simp.SimpMessageHeaderAccessor;
20 import hunt.stomp.simp.SimpMessageType;
21 import hunt.stomp.Message;
22 import hunt.stomp.handler.AbstractMessageCondition;
23 
24 import hunt.collection;
25 import hunt.Nullable;
26 
27 /**
28  * {@code MessageCondition} that matches by the message type obtained via
29  * {@link SimpMessageHeaderAccessor#getMessageType(Map)}.
30  *
31  * @author Rossen Stoyanchev
32  * @since 4.0
33  */
34 class SimpMessageTypeMessageCondition : 
35     AbstractMessageCondition!(SimpMessageTypeMessageCondition, SimpMessageType) {
36 
37 	__gshared SimpMessageTypeMessageCondition MESSAGE;
38 	__gshared SimpMessageTypeMessageCondition SUBSCRIBE;
39 
40 	private SimpMessageType messageType;
41 
42     shared static this() {
43         MESSAGE = new SimpMessageTypeMessageCondition(SimpMessageType.MESSAGE);
44         SUBSCRIBE = new SimpMessageTypeMessageCondition(SimpMessageType.SUBSCRIBE);
45     }
46 
47 
48 	/**
49 	 * A constructor accepting a message type.
50 	 * @param messageType the message type to match messages to
51 	 */
52 	this(SimpMessageType messageType) {
53 		assert(messageType, "MessageType must not be null");
54 		this.messageType = messageType;
55 	}
56 
57 
58 	SimpMessageType getMessageType() {
59 		return this.messageType;
60 	}
61 
62 // TODO: Tasks pending completion -@zxp at 10/31/2018, 10:40:58 AM
63 // 
64 	override
65 	protected Collection!SimpMessageType getContent() {
66 		return Collections.singletonList(this.messageType);
67 	}
68 
69 	override
70 	protected string getToStringInfix() {
71 		return " || ";
72 	}
73 
74 	// override
75 	SimpMessageTypeMessageCondition combine(SimpMessageTypeMessageCondition other) {
76 		return other;
77 	}
78 
79 	// override
80 	SimpMessageTypeMessageCondition getMatchingCondition(MessageBase message) {
81 		Nullable!SimpMessageType actual = SimpMessageHeaderAccessor.getMessageType(message.getHeaders());
82 		return (actual !is null && actual == this.messageType ? this : null);
83 	}
84 
85 	// override
86 	int compareTo(SimpMessageTypeMessageCondition other, MessageBase message) {
87 		Nullable!SimpMessageType actual = SimpMessageHeaderAccessor.getMessageType(message.getHeaders());
88 		if (actual !is null) {
89             SimpMessageType actualType = cast(SimpMessageType)actual;
90 			if (actualType == this.messageType && actualType == other.getMessageType()) {
91 				return 0;
92 			}
93 			else if (actualType == this.messageType) {
94 				return -1;
95 			}
96 			else if (actualType == other.getMessageType()) {
97 				return 1;
98 			}
99 		}
100 		return 0;
101 	}
102 
103 }