1 /*
2  * Copyright 2002-2016 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.support.AbstractSubscribableChannel;
18 
19 import hunt.stomp.support.AbstractMessageChannel;
20 import hunt.stomp.Message;
21 import hunt.stomp.MessageChannel;
22 
23 import hunt.collection;
24 import hunt.logging;
25 import hunt.util.ObjectUtils;
26 
27 import std.conv;
28 // import java.util.concurrent.CopyOnWriteArraySet;
29 
30 /**
31  * Abstract base class for {@link SubscribableChannel} implementations.
32  *
33  * @author Rossen Stoyanchev
34  * @since 4.0
35  */
36 abstract class AbstractSubscribableChannel : AbstractMessageChannel, SubscribableChannel {
37 
38 	private Set!(MessageHandler) handlers;
39 
40 	this() {
41 		handlers = new HashSet!(MessageHandler)(); //  new CopyOnWriteArraySet!(MessageHandler)();
42 		super();
43 	}
44 
45 	Set!(MessageHandler) getSubscribers() {
46 		// version(HUNT_DEBUG) {
47 		// 	tracef("%s@%s, handlers: %d", id, 
48 		// 		ObjectUtils.getIdentityHexString(this), this.handlers.size);
49 		// }
50 		return this.handlers; // Collections.unmodifiableSet!(MessageHandler)
51 	}
52 
53 	bool hasSubscription(MessageHandler handler) {
54 		return this.handlers.contains(handler);
55 	}
56 
57 	override
58 	bool subscribe(MessageHandler handler) {
59 		bool result = this.handlers.add(handler);
60 		version(HUNT_DEBUG) {
61 			if (result) 
62 				trace(getBeanName() ~ " added " ~ handler.to!string());
63 		}
64 		// tracef("xxxx=>%s@%s, %d", id, ObjectUtils.getIdentityHexString(this), this.handlers.size);
65 		
66 		return result;
67 	}
68 
69 	override
70 	bool unsubscribe(MessageHandler handler) {
71 		bool result = this.handlers.remove(handler);
72 		version(HUNT_DEBUG) {
73 			if (result)
74 				trace(getBeanName() ~ " removed " ~ handler.to!string());
75 		}
76 		return result;
77 	}
78 
79 }