01: package org.claros.chat.controllers;
02:
03: import java.util.HashMap;
04:
05: import org.claros.chat.threads.ChatListener;
06: import org.claros.chat.threads.ChatSender;
07:
08: public class TrafficController {
09: private static HashMap listeners = new HashMap();
10: private static HashMap senders = new HashMap();
11:
12: /**
13: *
14: * @param user
15: * @param listener
16: */
17: public static void addListener(String user, ChatListener listener) {
18: String usrTrim = getTrimmed(user);
19:
20: ChatListener tmp = (ChatListener) listeners.get(usrTrim);
21: if (tmp != null) {
22: tmp.terminate();
23: }
24: listeners.put(usrTrim, listener);
25: }
26:
27: /**
28: *
29: * @param user
30: */
31: public static void removeListener(String user) {
32: String usrTrim = getTrimmed(user);
33: listeners.remove(usrTrim);
34: }
35:
36: /**
37: *
38: * @param user
39: * @return
40: */
41: public static ChatListener getListener(String user) {
42: String usrTrim = getTrimmed(user);
43: return (ChatListener) listeners.get(usrTrim);
44: }
45:
46: /**
47: *
48: * @param user
49: * @param sender
50: */
51: public static void addSender(String user, ChatSender sender) {
52: String usrTrim = getTrimmed(user);
53:
54: ChatSender tmp = (ChatSender) senders.get(usrTrim);
55: if (tmp != null) {
56: tmp.terminate();
57: }
58: senders.put(usrTrim, sender);
59: }
60:
61: /**
62: *
63: * @param user
64: */
65: public static void removeSender(String user) {
66: String usrTrim = getTrimmed(user);
67: senders.remove(usrTrim);
68: }
69:
70: /**
71: *
72: * @param user
73: * @return
74: */
75: public static ChatSender getSender(String user) {
76: String usrTrim = getTrimmed(user);
77: return (ChatSender) senders.get(usrTrim);
78: }
79:
80: /**
81: *
82: * @param user
83: * @return
84: */
85: private static String getTrimmed(String user) {
86: String usrTrim = user;
87: if (usrTrim.indexOf("/") > 0) {
88: usrTrim = usrTrim.substring(0, usrTrim.indexOf("/"));
89: }
90: return usrTrim;
91: }
92:
93: }
|