From 1a705e2b55c6fd3c5ad81ce3eebe6c44e2641f1b Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Tue, 11 Apr 2006 22:04:10 +0000 Subject: [PATCH] Type safety. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@4011 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- .../crowd/chat/client/ChatDirector.java | 112 +++++++++--------- 1 file changed, 58 insertions(+), 54 deletions(-) diff --git a/src/java/com/threerings/crowd/chat/client/ChatDirector.java b/src/java/com/threerings/crowd/chat/client/ChatDirector.java index a8df0649c..d86040180 100644 --- a/src/java/com/threerings/crowd/chat/client/ChatDirector.java +++ b/src/java/com/threerings/crowd/chat/client/ChatDirector.java @@ -270,7 +270,7 @@ public class ChatDirector extends BasicDirector */ public String getCommandHistory (int index) { - return (String)_history.get(index); + return _history.get(index); } /** @@ -286,9 +286,9 @@ public class ChatDirector extends BasicDirector */ public void clearDisplays () { - _displays.apply(new ObserverList.ObserverOp() { - public boolean apply (Object observer) { - ((ChatDisplay)observer).clear(); + _displays.apply(new ObserverList.ObserverOp() { + public boolean apply (ChatDisplay observer) { + observer.clear(); return true; } }); @@ -380,7 +380,8 @@ public class ChatDirector extends BasicDirector args = text.substring(sidx+1).trim(); } - HashMap possibleCommands = getCommandHandlers(command); + HashMap possibleCommands = + getCommandHandlers(command); switch (possibleCommands.size()) { case 0: StringTokenizer tok = new StringTokenizer(text); @@ -388,12 +389,13 @@ public class ChatDirector extends BasicDirector "m.unknown_command", tok.nextToken()); case 1: - Iterator itr = possibleCommands.entrySet().iterator(); - Map.Entry entry = (Map.Entry) itr.next(); - String cmdName = (String) entry.getKey(); - CommandHandler cmd = (CommandHandler) entry.getValue(); + Map.Entry entry = + possibleCommands.entrySet().iterator().next(); + String cmdName = entry.getKey(); + CommandHandler cmd = entry.getValue(); - String result = cmd.handleCommand(speakSvc, cmdName, args, hist); + String result = cmd.handleCommand( + speakSvc, cmdName, args, hist); if (!result.equals(ChatCodes.SUCCESS)) { return result; } @@ -411,10 +413,10 @@ public class ChatDirector extends BasicDirector default: String alternativeCommands = ""; - itr = Collections.getSortedIterator(possibleCommands.keySet()); + Iterator itr = Collections.getSortedIterator( + possibleCommands.keySet()); while (itr.hasNext()) { - cmdName = (String)itr.next(); - alternativeCommands += " /" + cmdName; + alternativeCommands += " /" + itr.next(); } return MessageBundle.tcompose( "m.unspecific_command", alternativeCommands); @@ -923,18 +925,17 @@ public class ChatDirector extends BasicDirector * specified command (i.e. the specified command is a prefix of their * registered command string). */ - protected HashMap getCommandHandlers (String command) + protected HashMap getCommandHandlers (String command) { - HashMap matches = new HashMap(); + HashMap matches = + new HashMap(); BodyObject user = (BodyObject)_ctx.getClient().getClientObject(); - Iterator itr = _handlers.entrySet().iterator(); - while (itr.hasNext()) { - Map.Entry entry = (Map.Entry) itr.next(); - String cmd = (String) entry.getKey(); + for (Map.Entry entry : _handlers.entrySet()) { + String cmd = entry.getKey(); if (!cmd.startsWith(command)) { continue; } - CommandHandler handler = (CommandHandler)entry.getValue(); + CommandHandler handler = entry.getValue(); if (!handler.checkAccess(user)) { continue; } @@ -972,10 +973,9 @@ public class ChatDirector extends BasicDirector */ protected void notifyChatterObservers () { - _chatterObservers.apply(new ObserverList.ObserverOp() { - public boolean apply (Object observer) { - ((ChatterObserver)observer).chattersUpdated( - _chatters.listIterator()); + _chatterObservers.apply(new ObserverList.ObserverOp() { + public boolean apply (ChatterObserver observer) { + observer.chattersUpdated(_chatters.listIterator()); return true; } }); @@ -1022,7 +1022,7 @@ public class ChatDirector extends BasicDirector */ protected String getLocalType (int oid) { - String type = (String)_auxes.get(oid); + String type = _auxes.get(oid); return (type == null) ? PLACE_CHAT_TYPE : type; } @@ -1037,7 +1037,8 @@ public class ChatDirector extends BasicDirector * An operation that checks with all chat filters to properly filter * a message prior to sending to the server or displaying. */ - protected static class FilterMessageOp implements ObserverList.ObserverOp + protected static class FilterMessageOp + implements ObserverList.ObserverOp { public void setMessage (String msg, Name otherUser, boolean outgoing) { @@ -1046,10 +1047,10 @@ public class ChatDirector extends BasicDirector _out = outgoing; } - public boolean apply (Object observer) + public boolean apply (ChatFilter observer) { if (_msg != null) { - _msg = ((ChatFilter) observer).filter(_msg, _otherUser, _out); + _msg = observer.filter(_msg, _otherUser, _out); } return true; } @@ -1067,16 +1068,17 @@ public class ChatDirector extends BasicDirector /** * An observer op used to dispatch ChatMessages on the client. */ - protected static class DisplayMessageOp implements ObserverList.ObserverOp + protected static class DisplayMessageOp + implements ObserverList.ObserverOp { public void setMessage (ChatMessage message) { _message = message; } - public boolean apply (Object observer) + public boolean apply (ChatDisplay observer) { - ((ChatDisplay)observer).displayMessage(_message); + observer.displayMessage(_message); return true; } @@ -1086,8 +1088,8 @@ public class ChatDirector extends BasicDirector /** Implements /help. */ protected class HelpHandler extends CommandHandler { - public String handleCommand ( - SpeakService speakSvc, String command, String args, String[] history) + public String handleCommand (SpeakService speakSvc, String command, + String args, String[] history) { String hcmd = ""; @@ -1106,7 +1108,8 @@ public class ChatDirector extends BasicDirector } // handle "/help help" and "/help someboguscommand" - HashMap possibleCommands = getCommandHandlers(hcmd); + HashMap possibleCommands = + getCommandHandlers(hcmd); if (hcmd.equals("help") || possibleCommands.isEmpty()) { possibleCommands = getCommandHandlers(""); possibleCommands.remove("help"); // remove help from the list @@ -1115,12 +1118,12 @@ public class ChatDirector extends BasicDirector // if there is only one possible command display its usage switch (possibleCommands.size()) { case 1: - Iterator itr = possibleCommands.keySet().iterator(); + Iterator itr = possibleCommands.keySet().iterator(); // this is a little funny, but we display the feeback // message by hand and return SUCCESS so that the chat // entry field doesn't think that we've failed and // preserve our command text - displayFeedback(null, "m.usage_" + (String)itr.next()); + displayFeedback(null, "m.usage_" + itr.next()); return ChatCodes.SUCCESS; default: @@ -1138,8 +1141,8 @@ public class ChatDirector extends BasicDirector /** Implements /clear. */ protected class ClearHandler extends CommandHandler { - public String handleCommand ( - SpeakService speakSvc, String command, String args, String[] history) + public String handleCommand (SpeakService speakSvc, String command, + String args, String[] history) { clearDisplays(); return ChatCodes.SUCCESS; @@ -1149,8 +1152,8 @@ public class ChatDirector extends BasicDirector /** Implements /speak. */ protected class SpeakHandler extends CommandHandler { - public String handleCommand ( - SpeakService speakSvc, String command, String args, String[] history) + public String handleCommand (SpeakService speakSvc, String command, + String args, String[] history) { if (StringUtil.isBlank(args)) { return "m.usage_speak"; @@ -1164,8 +1167,8 @@ public class ChatDirector extends BasicDirector /** Implements /emote. */ protected class EmoteHandler extends CommandHandler { - public String handleCommand ( - SpeakService speakSvc, String command, String args, String[] history) + public String handleCommand (SpeakService speakSvc, String command, + String args, String[] history) { if (StringUtil.isBlank(args)) { return "m.usage_emote"; @@ -1179,8 +1182,8 @@ public class ChatDirector extends BasicDirector /** Implements /think. */ protected class ThinkHandler extends CommandHandler { - public String handleCommand ( - SpeakService speakSvc, String command, String args, String[] history) + public String handleCommand (SpeakService speakSvc, String command, + String args, String[] history) { if (StringUtil.isBlank(args)) { return "m.usage_think"; @@ -1210,32 +1213,33 @@ public class ChatDirector extends BasicDirector protected ClientObject _clobj; /** A list of registered chat displays. */ - protected ObserverList _displays = - new ObserverList(ObserverList.FAST_UNSAFE_NOTIFY); + protected ObserverList _displays = + new ObserverList(ObserverList.FAST_UNSAFE_NOTIFY); /** A list of registered chat filters. */ - protected ObserverList _filters = - new ObserverList(ObserverList.FAST_UNSAFE_NOTIFY); + protected ObserverList _filters = + new ObserverList(ObserverList.FAST_UNSAFE_NOTIFY); /** A mapping from auxiliary chat objects to the types under which * they are registered. */ - protected HashIntMap _auxes = new HashIntMap(); + protected HashIntMap _auxes = new HashIntMap(); /** Validator of who may be added to the chatters list. */ protected ChatterValidator _chatterValidator; /** Usernames of users we've recently chatted with. */ - protected LinkedList _chatters = new LinkedList(); + protected LinkedList _chatters = new LinkedList(); /** Observers that are watching our chatters list. */ - protected ObserverList _chatterObservers = - new ObserverList(ObserverList.FAST_UNSAFE_NOTIFY); + protected ObserverList _chatterObservers = + new ObserverList(ObserverList.FAST_UNSAFE_NOTIFY); /** Registered chat command handlers. */ - protected static HashMap _handlers = new HashMap(); + protected static HashMap _handlers = + new HashMap(); /** A history of chat commands. */ - protected static ArrayList _history = new ArrayList(); + protected static ArrayList _history = new ArrayList(); /** Operation used to filter chat messages. */ protected FilterMessageOp _filterMessageOp = new FilterMessageOp();