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