[1] | 1 | package de.ugoe.cs.util.console;
|
---|
| 2 |
|
---|
[199] | 3 | import java.util.Collection;
|
---|
| 4 | import java.util.LinkedHashSet;
|
---|
[1] | 5 |
|
---|
| 6 | import de.ugoe.cs.util.StringTools;
|
---|
[199] | 7 | import de.ugoe.cs.util.console.listener.ICommandListener;
|
---|
| 8 | import de.ugoe.cs.util.console.listener.IErrorListener;
|
---|
| 9 | import de.ugoe.cs.util.console.listener.IExceptionListener;
|
---|
| 10 | import de.ugoe.cs.util.console.listener.IOutputListener;
|
---|
| 11 | import de.ugoe.cs.util.console.listener.ITraceListener;
|
---|
[1] | 12 |
|
---|
| 13 | /**
|
---|
| 14 | * <p>
|
---|
| 15 | * This class provides an interface for communication with the user without have
|
---|
| 16 | * to rely on a specific user interface. Thus, it can be used to decouple the
|
---|
| 17 | * programs logic from its user interface.
|
---|
| 18 | * </p>
|
---|
| 19 | * <p>
|
---|
| 20 | * {@link Command} objects can be used to execute behavior.
|
---|
| 21 | * </p>
|
---|
| 22 | * <p>
|
---|
| 23 | * To send output to the user interface, the Observer pattern is used. The
|
---|
| 24 | * Console is an observable, the concrete user interfaces are the observers. The
|
---|
| 25 | * interface for the observers is {@link ConsoleObserver}.
|
---|
| 26 | * </p>
|
---|
| 27 | *
|
---|
| 28 | * @author Steffen Herbold
|
---|
[175] | 29 | * @version 1.0
|
---|
[1] | 30 | */
|
---|
| 31 | public final class Console {
|
---|
| 32 |
|
---|
[201] | 33 | /**
|
---|
| 34 | * <p>
|
---|
| 35 | * Listeners for the output stream.
|
---|
| 36 | * </p>
|
---|
| 37 | */
|
---|
[199] | 38 | private Collection<IOutputListener> outputListener;
|
---|
[201] | 39 |
|
---|
| 40 | /**
|
---|
| 41 | * <p>
|
---|
| 42 | * Listeners for the error stream.
|
---|
| 43 | * </p>
|
---|
| 44 | */
|
---|
[199] | 45 | private Collection<IErrorListener> errorListener;
|
---|
[201] | 46 |
|
---|
| 47 | /**
|
---|
| 48 | * <p>
|
---|
| 49 | * Listeners for the trace stream.
|
---|
| 50 | * </p>
|
---|
| 51 | */
|
---|
[199] | 52 | private Collection<ITraceListener> traceListener;
|
---|
[201] | 53 |
|
---|
| 54 | /**
|
---|
| 55 | * <p>
|
---|
| 56 | * Listeners for the command stream.
|
---|
| 57 | * </p>
|
---|
| 58 | */
|
---|
[199] | 59 | private Collection<ICommandListener> commandListener;
|
---|
[201] | 60 |
|
---|
| 61 | /**
|
---|
| 62 | * <p>
|
---|
| 63 | * Listeners for the exception stream.
|
---|
| 64 | * </p>
|
---|
| 65 | */
|
---|
[199] | 66 | private Collection<IExceptionListener> exceptionListener;
|
---|
[1] | 67 |
|
---|
| 68 | /**
|
---|
| 69 | * <p>
|
---|
| 70 | * Handle of the Console instance.
|
---|
| 71 | * </p>
|
---|
| 72 | */
|
---|
[250] | 73 | private static Console theInstance = new Console();
|
---|
[1] | 74 |
|
---|
| 75 | /**
|
---|
| 76 | * <p>
|
---|
| 77 | * Returns the instance of Console. If no instances exists yet, a new one is
|
---|
| 78 | * created.
|
---|
| 79 | * </p>
|
---|
| 80 | *
|
---|
| 81 | * @return instance of this class
|
---|
| 82 | */
|
---|
| 83 | public static Console getInstance() {
|
---|
| 84 | return theInstance;
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | /**
|
---|
| 88 | * <p>
|
---|
[250] | 89 | * Resets the Console by creating a new instance that has no registered
|
---|
| 90 | * observers.
|
---|
| 91 | * </p>
|
---|
| 92 | */
|
---|
| 93 | public static void reset() {
|
---|
| 94 | theInstance.init();
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | /**
|
---|
| 98 | * <p>
|
---|
[1] | 99 | * Creates a new Console. Private to prevent multiple instances (Singleton).
|
---|
| 100 | * </p>
|
---|
| 101 | */
|
---|
| 102 | private Console() {
|
---|
[250] | 103 | init();
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | /**
|
---|
| 107 | * <p>
|
---|
| 108 | * Initializes the console.
|
---|
| 109 | * </p>
|
---|
| 110 | */
|
---|
| 111 | private void init() {
|
---|
[199] | 112 | outputListener = new LinkedHashSet<IOutputListener>();
|
---|
| 113 | errorListener = new LinkedHashSet<IErrorListener>();
|
---|
| 114 | traceListener = new LinkedHashSet<ITraceListener>();
|
---|
| 115 | commandListener = new LinkedHashSet<ICommandListener>();
|
---|
| 116 | exceptionListener = new LinkedHashSet<IExceptionListener>();
|
---|
[1] | 117 | }
|
---|
| 118 |
|
---|
| 119 | /**
|
---|
| 120 | * <p>
|
---|
| 121 | * Register a new observer.
|
---|
| 122 | * </p>
|
---|
| 123 | *
|
---|
[199] | 124 | * @deprecated use registerXYZListener instead
|
---|
[1] | 125 | * @param observer
|
---|
| 126 | * observer to be added
|
---|
| 127 | */
|
---|
| 128 | public void registerObserver(ConsoleObserver observer) {
|
---|
[199] | 129 | registerOutputListener(observer);
|
---|
| 130 | registerErrorListener(observer);
|
---|
| 131 | registerTraceListener(observer);
|
---|
| 132 | registerCommandListener(observer);
|
---|
| 133 | registerExceptionListener(observer);
|
---|
[1] | 134 | }
|
---|
| 135 |
|
---|
| 136 | /**
|
---|
| 137 | * <p>
|
---|
[199] | 138 | * Registers an output listener.
|
---|
| 139 | * </p>
|
---|
| 140 | *
|
---|
| 141 | * @param listener
|
---|
| 142 | * listener that is registered
|
---|
| 143 | */
|
---|
| 144 | public void registerOutputListener(IOutputListener listener) {
|
---|
| 145 | outputListener.add(listener);
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | /**
|
---|
| 149 | * <p>
|
---|
| 150 | * Registers an error listener.
|
---|
| 151 | * </p>
|
---|
| 152 | *
|
---|
| 153 | * @param listener
|
---|
| 154 | * listener that is registered
|
---|
| 155 | */
|
---|
| 156 | public void registerErrorListener(IErrorListener listener) {
|
---|
| 157 | errorListener.add(listener);
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | /**
|
---|
| 161 | * <p>
|
---|
| 162 | * Registers a trace listener.
|
---|
| 163 | * </p>
|
---|
| 164 | *
|
---|
| 165 | * @param listener
|
---|
| 166 | * listener that is registered
|
---|
| 167 | */
|
---|
| 168 | public void registerTraceListener(ITraceListener listener) {
|
---|
| 169 | traceListener.add(listener);
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | /**
|
---|
| 173 | * <p>
|
---|
| 174 | * Registers a command listener.
|
---|
| 175 | * </p>
|
---|
| 176 | *
|
---|
| 177 | * @param listener
|
---|
| 178 | * listener that is registered
|
---|
| 179 | */
|
---|
| 180 | public void registerCommandListener(ICommandListener listener) {
|
---|
| 181 | commandListener.add(listener);
|
---|
| 182 | }
|
---|
| 183 |
|
---|
| 184 | /**
|
---|
| 185 | * <p>
|
---|
| 186 | * Registers an exception listener.
|
---|
| 187 | * </p>
|
---|
| 188 | *
|
---|
| 189 | * @param listener
|
---|
| 190 | * listener that is registered
|
---|
| 191 | */
|
---|
| 192 | public void registerExceptionListener(IExceptionListener listener) {
|
---|
| 193 | exceptionListener.add(listener);
|
---|
| 194 | }
|
---|
| 195 |
|
---|
| 196 | /**
|
---|
| 197 | * <p>
|
---|
[1] | 198 | * Remove an observer. If the observer is not found, nothing is done.
|
---|
| 199 | * </p>
|
---|
| 200 | *
|
---|
[199] | 201 | * @deprecated use removeXYZListener instead
|
---|
[1] | 202 | * @param observer
|
---|
| 203 | * observer to be removed
|
---|
| 204 | */
|
---|
| 205 | public void deleteObserver(ConsoleObserver observer) {
|
---|
[199] | 206 | removeOutputListener(observer);
|
---|
| 207 | removeErrorListener(observer);
|
---|
| 208 | removeTraceListener(observer);
|
---|
| 209 | removeCommandListener(observer);
|
---|
| 210 | removeExceptionListener(observer);
|
---|
[1] | 211 | }
|
---|
| 212 |
|
---|
| 213 | /**
|
---|
| 214 | * <p>
|
---|
[199] | 215 | * Removes an output listener.
|
---|
| 216 | * </p>
|
---|
| 217 | *
|
---|
| 218 | * @param listener
|
---|
| 219 | * listener that is removed
|
---|
| 220 | */
|
---|
| 221 | public void removeOutputListener(IOutputListener listener) {
|
---|
| 222 | outputListener.remove(listener);
|
---|
| 223 | }
|
---|
| 224 |
|
---|
| 225 | /**
|
---|
| 226 | * <p>
|
---|
| 227 | * Removes an error listener.
|
---|
| 228 | * </p>
|
---|
| 229 | *
|
---|
| 230 | * @param listener
|
---|
| 231 | * listener that is removed
|
---|
| 232 | */
|
---|
| 233 | public void removeErrorListener(IErrorListener listener) {
|
---|
| 234 | errorListener.remove(listener);
|
---|
| 235 | }
|
---|
| 236 |
|
---|
| 237 | /**
|
---|
| 238 | * <p>
|
---|
| 239 | * Removes an trace listener.
|
---|
| 240 | * </p>
|
---|
| 241 | *
|
---|
| 242 | * @param listener
|
---|
| 243 | * listener that is removed
|
---|
| 244 | */
|
---|
| 245 | public void removeTraceListener(ITraceListener listener) {
|
---|
| 246 | traceListener.remove(listener);
|
---|
| 247 | }
|
---|
| 248 |
|
---|
| 249 | /**
|
---|
| 250 | * <p>
|
---|
| 251 | * Removes a command listener.
|
---|
| 252 | * </p>
|
---|
| 253 | *
|
---|
| 254 | * @param listener
|
---|
| 255 | * listener that is removed
|
---|
| 256 | */
|
---|
| 257 | public void removeCommandListener(ICommandListener listener) {
|
---|
| 258 | commandListener.remove(listener);
|
---|
| 259 | }
|
---|
| 260 |
|
---|
| 261 | /**
|
---|
| 262 | * <p>
|
---|
| 263 | * Removes an exception listener.
|
---|
| 264 | * </p>
|
---|
| 265 | *
|
---|
| 266 | * @param listener
|
---|
| 267 | * listener that is removed
|
---|
| 268 | */
|
---|
| 269 | public void removeExceptionListener(IExceptionListener listener) {
|
---|
| 270 | exceptionListener.remove(listener);
|
---|
| 271 | }
|
---|
| 272 |
|
---|
| 273 | /**
|
---|
| 274 | * <p>
|
---|
[250] | 275 | * Checks if a listener is registered.
|
---|
| 276 | * </p>
|
---|
| 277 | *
|
---|
| 278 | * @param listener
|
---|
| 279 | * listener that is checked
|
---|
| 280 | * @return true, is listener is registered; false, otherwise
|
---|
| 281 | */
|
---|
| 282 | public boolean hasOutputListener(IOutputListener listener) {
|
---|
| 283 | return outputListener.contains(listener);
|
---|
| 284 | }
|
---|
| 285 |
|
---|
| 286 | /**
|
---|
| 287 | * <p>
|
---|
| 288 | * Checks if a listener is registered.
|
---|
| 289 | * </p>
|
---|
| 290 | *
|
---|
| 291 | * @param listener
|
---|
| 292 | * listener that is checked
|
---|
| 293 | * @return true, is listener is registered; false, otherwise
|
---|
| 294 | */
|
---|
| 295 | public boolean hasErrorListener(IErrorListener listener) {
|
---|
| 296 | return errorListener.contains(listener);
|
---|
| 297 | }
|
---|
| 298 |
|
---|
| 299 | /**
|
---|
| 300 | * <p>
|
---|
| 301 | * Checks if a listener is registered.
|
---|
| 302 | * </p>
|
---|
| 303 | *
|
---|
| 304 | * @param listener
|
---|
| 305 | * listener that is checked
|
---|
| 306 | * @return true, is listener is registered; false, otherwise
|
---|
| 307 | */
|
---|
| 308 | public boolean hasTraceListener(ITraceListener listener) {
|
---|
| 309 | return traceListener.contains(listener);
|
---|
| 310 | }
|
---|
| 311 |
|
---|
| 312 | /**
|
---|
| 313 | * <p>
|
---|
| 314 | * Checks if a listener is registered.
|
---|
| 315 | * </p>
|
---|
| 316 | *
|
---|
| 317 | * @param listener
|
---|
| 318 | * listener that is checked
|
---|
| 319 | * @return true, is listener is registered; false, otherwise
|
---|
| 320 | */
|
---|
| 321 | public boolean hasCommandListener(ICommandListener listener) {
|
---|
| 322 | return commandListener.contains(listener);
|
---|
| 323 | }
|
---|
| 324 |
|
---|
| 325 | /**
|
---|
| 326 | * <p>
|
---|
| 327 | * Checks if a listener is registered.
|
---|
| 328 | * </p>
|
---|
| 329 | *
|
---|
| 330 | * @param listener
|
---|
| 331 | * listener that is checked
|
---|
| 332 | * @return true, is listener is registered; false, otherwise
|
---|
| 333 | */
|
---|
| 334 | public boolean hasExceptionListener(IExceptionListener listener) {
|
---|
| 335 | return exceptionListener.contains(listener);
|
---|
| 336 | }
|
---|
| 337 |
|
---|
| 338 | /**
|
---|
| 339 | * <p>
|
---|
[1] | 340 | * Sends a message to all observers containing the message that was passed
|
---|
| 341 | * to this function.
|
---|
| 342 | * </p>
|
---|
| 343 | *
|
---|
| 344 | * @param msg
|
---|
| 345 | * message that is send to the console
|
---|
| 346 | */
|
---|
| 347 | public static void print(String msg) {
|
---|
[199] | 348 | for (IOutputListener observer : theInstance.outputListener) {
|
---|
[201] | 349 | observer.outputMsg(msg);
|
---|
[1] | 350 | }
|
---|
| 351 | }
|
---|
| 352 |
|
---|
| 353 | /**
|
---|
| 354 | * <p>
|
---|
| 355 | * Sends a message to all observers containing the message that was passed
|
---|
| 356 | * to this function and adds an endline to the message.
|
---|
| 357 | * </p>
|
---|
| 358 | *
|
---|
| 359 | * @param msg
|
---|
| 360 | * message that is send to the observers
|
---|
| 361 | */
|
---|
| 362 | public static void println(String msg) {
|
---|
[199] | 363 | for (IOutputListener observer : theInstance.outputListener) {
|
---|
[201] | 364 | observer.outputMsg(msg + StringTools.ENDLINE);
|
---|
[1] | 365 | }
|
---|
| 366 | }
|
---|
| 367 |
|
---|
| 368 | /**
|
---|
| 369 | * <p>
|
---|
| 370 | * Sends an error message to all observers containing the message that was
|
---|
| 371 | * passed to this function.
|
---|
| 372 | * </p>
|
---|
| 373 | *
|
---|
| 374 | * @param errMsg
|
---|
| 375 | * message that is send to the observers
|
---|
| 376 | */
|
---|
| 377 | public static void printerr(String errMsg) {
|
---|
[199] | 378 | for (IErrorListener observer : theInstance.errorListener) {
|
---|
[201] | 379 | observer.errorMsg(errMsg);
|
---|
[1] | 380 | }
|
---|
| 381 | }
|
---|
| 382 |
|
---|
| 383 | /**
|
---|
| 384 | * <p>
|
---|
| 385 | * Sends an error message to all observers containing the message that was
|
---|
| 386 | * passed to this function and adds an endline to the message.
|
---|
| 387 | * </p>
|
---|
| 388 | *
|
---|
| 389 | * @param errMsg
|
---|
| 390 | * message that is send to the observers
|
---|
| 391 | */
|
---|
| 392 | public static void printerrln(String errMsg) {
|
---|
[199] | 393 | for (IErrorListener observer : theInstance.errorListener) {
|
---|
[201] | 394 | observer.errorMsg(errMsg + StringTools.ENDLINE);
|
---|
[1] | 395 | }
|
---|
| 396 | }
|
---|
| 397 |
|
---|
| 398 | /**
|
---|
| 399 | * <p>
|
---|
| 400 | * Sends an exception to all observers to print its stack trace.
|
---|
| 401 | * </p>
|
---|
| 402 | *
|
---|
| 403 | * @param e
|
---|
| 404 | * exception whose stack trace is to be printed
|
---|
| 405 | */
|
---|
[210] | 406 | public static void logException(Exception e) {
|
---|
[199] | 407 | for (IExceptionListener observer : theInstance.exceptionListener) {
|
---|
[210] | 408 | observer.logException(e);
|
---|
[1] | 409 | }
|
---|
| 410 | }
|
---|
| 411 |
|
---|
| 412 | /**
|
---|
| 413 | * <p>
|
---|
| 414 | * Sends a debug message to all observers containing the message that was
|
---|
| 415 | * passed to this function.
|
---|
| 416 | * </p>
|
---|
| 417 | *
|
---|
| 418 | * @param traceMsg
|
---|
| 419 | * message that is send to the observers
|
---|
| 420 | */
|
---|
| 421 | public static void trace(String traceMsg) {
|
---|
[199] | 422 | for (ITraceListener observer : theInstance.traceListener) {
|
---|
[201] | 423 | observer.traceMsg(traceMsg);
|
---|
[1] | 424 | }
|
---|
| 425 | }
|
---|
| 426 |
|
---|
| 427 | /**
|
---|
| 428 | * <p>
|
---|
| 429 | * Sends a debug message to all observers containing the message that was
|
---|
[175] | 430 | * passed to this function and adds an {@link StringTools#ENDLINE} to the
|
---|
| 431 | * message.
|
---|
[1] | 432 | * </p>
|
---|
| 433 | *
|
---|
| 434 | * @param traceMsg
|
---|
| 435 | * message that is send to the observers
|
---|
| 436 | */
|
---|
| 437 | public static void traceln(String traceMsg) {
|
---|
[199] | 438 | for (ITraceListener observer : theInstance.traceListener) {
|
---|
[201] | 439 | observer.traceMsg(traceMsg + StringTools.ENDLINE);
|
---|
[1] | 440 | }
|
---|
| 441 | }
|
---|
| 442 |
|
---|
[186] | 443 | /**
|
---|
| 444 | * <p>
|
---|
| 445 | * Called by {@link CommandExecuter#exec(String)}.
|
---|
| 446 | * </p>
|
---|
| 447 | *
|
---|
| 448 | * @param command
|
---|
| 449 | * command that is executed
|
---|
| 450 | */
|
---|
| 451 | static void commandNotification(String command) {
|
---|
[199] | 452 | for (ICommandListener observer : theInstance.commandListener) {
|
---|
[186] | 453 | observer.commandNotification(command);
|
---|
| 454 | }
|
---|
| 455 | }
|
---|
| 456 |
|
---|
[1] | 457 | }
|
---|