|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
CommandHandler.java | 62.5% | 77.8% | 75% | 71.4% |
|
1 |
/*
|
|
2 |
|
|
3 |
VRMoo Common - Virtual Reality Object Oriented MUD Common Code
|
|
4 |
Copyright (C) 2003 VRMoo Development Team
|
|
5 |
|
|
6 |
|
|
7 |
This program is free software; you can redistribute it and/or modify
|
|
8 |
it under the terms of the GNU General Public License as published by
|
|
9 |
the Free Software Foundation; either version 2 of the License, or
|
|
10 |
(at your option) any later version.
|
|
11 |
|
|
12 |
This program is distributed in the hope that it will be useful,
|
|
13 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
14 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
15 |
GNU General Public License for more details.
|
|
16 |
|
|
17 |
You should have received a copy of the GNU General Public License
|
|
18 |
along with this program; if not, write to the Free Software
|
|
19 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
20 |
|
|
21 |
|
|
22 |
For information about VRMoo and its authors, please visit the website:
|
|
23 |
http://www.vrmoo.org/
|
|
24 |
|
|
25 |
*/
|
|
26 |
|
|
27 |
package org.vrmoo.common.util;
|
|
28 |
|
|
29 |
import java.util.ArrayList;
|
|
30 |
import java.util.List;
|
|
31 |
|
|
32 |
import org.vrmoo.common.exception.VRMooException;
|
|
33 |
|
|
34 |
/**
|
|
35 |
* Superclass for those classes that need to handle commands. This class
|
|
36 |
* provides the basic functionality for setting up a list of
|
|
37 |
* <code>CommandListener</code> instances, adding and removing said instances,
|
|
38 |
* and properly propogating commands to those <code>CommandListener</code>
|
|
39 |
* instances.
|
|
40 |
*
|
|
41 |
* @author Jeff Weston
|
|
42 |
*/
|
|
43 |
public abstract class CommandHandler |
|
44 |
{ |
|
45 |
/**
|
|
46 |
* The list of command processors.
|
|
47 |
*/
|
|
48 |
private List commandProcessors;
|
|
49 |
|
|
50 |
/**
|
|
51 |
* Protected default constructor for setting up the command processors list.
|
|
52 |
*/
|
|
53 | 45 |
protected CommandHandler( )
|
54 |
{ |
|
55 | 45 |
commandProcessors = new ArrayList( );
|
56 |
} |
|
57 |
|
|
58 |
/**
|
|
59 |
* Add a new <code>CommandProcessor</code> to the list of command
|
|
60 |
* processors for this class.
|
|
61 |
*
|
|
62 |
* @param commandProcessor the <code>CommandProcessor</code> to add
|
|
63 |
*/
|
|
64 | 51 |
public synchronized void addCommandProcessor( |
65 |
CommandProcessor commandProcessor ) |
|
66 |
{ |
|
67 | 51 |
if ( ! commandProcessors.contains( commandProcessor ) )
|
68 |
{ |
|
69 | 51 |
commandProcessors.add( commandProcessor ); |
70 |
} |
|
71 |
} |
|
72 |
|
|
73 |
/**
|
|
74 |
* Remove a <code>CommandProcessor</code> from the list of command
|
|
75 |
* processors for this class.
|
|
76 |
*
|
|
77 |
* @param commandProcessor the <code>CommandProcessor</code> to remove
|
|
78 |
*/
|
|
79 | 0 |
public synchronized void removeCommandProcessor( |
80 |
CommandProcessor commandProcessor ) |
|
81 |
{ |
|
82 | 0 |
if ( commandProcessors.contains( commandProcessor ) )
|
83 |
{ |
|
84 | 0 |
commandProcessors.remove( commandProcessor ); |
85 |
} |
|
86 |
} |
|
87 |
|
|
88 |
/**
|
|
89 |
* Handle a command by delegating it to the <code>CommandProcessor</code>
|
|
90 |
* instances registered with this <code>CommandHandler</code>.
|
|
91 |
*
|
|
92 |
* @param command the command to handle
|
|
93 |
* @param extraData extra data that isn't part of the command but may be
|
|
94 |
* needed for dealing with the command (such as the ID
|
|
95 |
* of the client that sent the command)
|
|
96 |
*
|
|
97 |
* @throws VRMooException for any errors that occur
|
|
98 |
*/
|
|
99 | 48 |
public synchronized void handleCommand( |
100 |
String command, String extraData ) throws VRMooException
|
|
101 |
{ |
|
102 | 48 |
for ( int i = 0; i < commandProcessors.size( ); i++ ) |
103 |
{ |
|
104 | 124 |
CommandProcessor commandProcessor = |
105 |
( CommandProcessor ) commandProcessors.get( i ); |
|
106 |
|
|
107 | 124 |
if ( command.indexOf( commandProcessor.getCommandPrefix( ) ) == 0 )
|
108 |
{ |
|
109 | 30 |
commandProcessor.processCommand( command, extraData ); |
110 |
} |
|
111 |
} |
|
112 |
} |
|
113 |
} |
|
114 |
|
|