[927] | 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 |
|
---|
[1] | 15 | package de.ugoe.cs.util.console;
|
---|
| 16 |
|
---|
[199] | 17 | import java.util.Collection;
|
---|
| 18 | import java.util.LinkedHashSet;
|
---|
[633] | 19 | import java.util.logging.Level;
|
---|
[1] | 20 |
|
---|
| 21 | import de.ugoe.cs.util.StringTools;
|
---|
[199] | 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;
|
---|
[1] | 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
|
---|
[175] | 44 | * @version 1.0
|
---|
[1] | 45 | */
|
---|
| 46 | public final class Console {
|
---|
| 47 |
|
---|
[201] | 48 | /**
|
---|
| 49 | * <p>
|
---|
| 50 | * Listeners for the output stream.
|
---|
| 51 | * </p>
|
---|
| 52 | */
|
---|
[199] | 53 | private Collection<IOutputListener> outputListener;
|
---|
[201] | 54 |
|
---|
| 55 | /**
|
---|
| 56 | * <p>
|
---|
| 57 | * Listeners for the error stream.
|
---|
| 58 | * </p>
|
---|
| 59 | */
|
---|
[199] | 60 | private Collection<IErrorListener> errorListener;
|
---|
[201] | 61 |
|
---|
| 62 | /**
|
---|
| 63 | * <p>
|
---|
| 64 | * Listeners for the trace stream.
|
---|
| 65 | * </p>
|
---|
| 66 | */
|
---|
[199] | 67 | private Collection<ITraceListener> traceListener;
|
---|
[201] | 68 |
|
---|
| 69 | /**
|
---|
| 70 | * <p>
|
---|
| 71 | * Listeners for the command stream.
|
---|
| 72 | * </p>
|
---|
| 73 | */
|
---|
[199] | 74 | private Collection<ICommandListener> commandListener;
|
---|
[201] | 75 |
|
---|
| 76 | /**
|
---|
| 77 | * <p>
|
---|
| 78 | * Listeners for the exception stream.
|
---|
| 79 | * </p>
|
---|
| 80 | */
|
---|
[199] | 81 | private Collection<IExceptionListener> exceptionListener;
|
---|
[1] | 82 |
|
---|
| 83 | /**
|
---|
| 84 | * <p>
|
---|
| 85 | * Handle of the Console instance.
|
---|
| 86 | * </p>
|
---|
| 87 | */
|
---|
[250] | 88 | private static Console theInstance = new Console();
|
---|
[1] | 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>
|
---|
[250] | 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>
|
---|
[1] | 114 | * Creates a new Console. Private to prevent multiple instances (Singleton).
|
---|
| 115 | * </p>
|
---|
| 116 | */
|
---|
| 117 | private Console() {
|
---|
[250] | 118 | init();
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | /**
|
---|
| 122 | * <p>
|
---|
| 123 | * Initializes the console.
|
---|
| 124 | * </p>
|
---|
| 125 | */
|
---|
| 126 | private void init() {
|
---|
[199] | 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>();
|
---|
[1] | 132 | }
|
---|
| 133 |
|
---|
| 134 | /**
|
---|
| 135 | * <p>
|
---|
| 136 | * Register a new observer.
|
---|
| 137 | * </p>
|
---|
| 138 | *
|
---|
[199] | 139 | * @deprecated use registerXYZListener instead
|
---|
[1] | 140 | * @param observer
|
---|
| 141 | * observer to be added
|
---|
| 142 | */
|
---|
| 143 | public void registerObserver(ConsoleObserver observer) {
|
---|
[199] | 144 | registerOutputListener(observer);
|
---|
| 145 | registerErrorListener(observer);
|
---|
| 146 | registerTraceListener(observer);
|
---|
| 147 | registerCommandListener(observer);
|
---|
| 148 | registerExceptionListener(observer);
|
---|
[1] | 149 | }
|
---|
| 150 |
|
---|
| 151 | /**
|
---|
| 152 | * <p>
|
---|
[199] | 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>
|
---|
[1] | 213 | * Remove an observer. If the observer is not found, nothing is done.
|
---|
| 214 | * </p>
|
---|
| 215 | *
|
---|
[199] | 216 | * @deprecated use removeXYZListener instead
|
---|
[1] | 217 | * @param observer
|
---|
| 218 | * observer to be removed
|
---|
| 219 | */
|
---|
| 220 | public void deleteObserver(ConsoleObserver observer) {
|
---|
[199] | 221 | removeOutputListener(observer);
|
---|
| 222 | removeErrorListener(observer);
|
---|
| 223 | removeTraceListener(observer);
|
---|
| 224 | removeCommandListener(observer);
|
---|
| 225 | removeExceptionListener(observer);
|
---|
[1] | 226 | }
|
---|
| 227 |
|
---|
| 228 | /**
|
---|
| 229 | * <p>
|
---|
[199] | 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>
|
---|
[250] | 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>
|
---|
[1] | 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) {
|
---|
[199] | 363 | for (IOutputListener observer : theInstance.outputListener) {
|
---|
[201] | 364 | observer.outputMsg(msg);
|
---|
[1] | 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) {
|
---|
[199] | 378 | for (IOutputListener observer : theInstance.outputListener) {
|
---|
[201] | 379 | observer.outputMsg(msg + StringTools.ENDLINE);
|
---|
[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.
|
---|
| 387 | * </p>
|
---|
| 388 | *
|
---|
| 389 | * @param errMsg
|
---|
| 390 | * message that is send to the observers
|
---|
| 391 | */
|
---|
| 392 | public static void printerr(String errMsg) {
|
---|
[199] | 393 | for (IErrorListener observer : theInstance.errorListener) {
|
---|
[201] | 394 | observer.errorMsg(errMsg);
|
---|
[1] | 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) {
|
---|
[199] | 408 | for (IErrorListener observer : theInstance.errorListener) {
|
---|
[201] | 409 | observer.errorMsg(errMsg + StringTools.ENDLINE);
|
---|
[1] | 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 | */
|
---|
[210] | 421 | public static void logException(Exception e) {
|
---|
[199] | 422 | for (IExceptionListener observer : theInstance.exceptionListener) {
|
---|
[210] | 423 | observer.logException(e);
|
---|
[1] | 424 | }
|
---|
| 425 | }
|
---|
| 426 |
|
---|
| 427 | /**
|
---|
| 428 | * <p>
|
---|
| 429 | * Sends a debug message to all observers containing the message that was
|
---|
[633] | 430 | * passed to this function. The default log {@link Level} is {@link Level#INFO}.
|
---|
[1] | 431 | * </p>
|
---|
| 432 | *
|
---|
| 433 | * @param traceMsg
|
---|
| 434 | * message that is send to the observers
|
---|
| 435 | */
|
---|
[633] | 436 | @Deprecated
|
---|
[1] | 437 | public static void trace(String traceMsg) {
|
---|
[199] | 438 | for (ITraceListener observer : theInstance.traceListener) {
|
---|
[633] | 439 | observer.traceMsg(traceMsg, Level.INFO);
|
---|
[1] | 440 | }
|
---|
| 441 | }
|
---|
[633] | 442 |
|
---|
| 443 | /**
|
---|
| 444 | *
|
---|
| 445 | * <p>
|
---|
| 446 | * Sends a debug message to all trace listeners containing the message that was passed to this function.
|
---|
| 447 | * </p>
|
---|
| 448 | *
|
---|
[635] | 449 | * @param logLevel log level of the message
|
---|
[633] | 450 | * @param traceMsg message that is send to the trace listener
|
---|
| 451 | */
|
---|
[635] | 452 | public static void trace(Level logLevel, String traceMsg) {
|
---|
[633] | 453 | for( ITraceListener observer : theInstance.traceListener) {
|
---|
| 454 | observer.traceMsg(traceMsg, logLevel);
|
---|
| 455 | }
|
---|
| 456 | }
|
---|
[1] | 457 |
|
---|
| 458 | /**
|
---|
| 459 | * <p>
|
---|
| 460 | * Sends a debug message to all observers containing the message that was
|
---|
[175] | 461 | * passed to this function and adds an {@link StringTools#ENDLINE} to the
|
---|
[633] | 462 | * message. The default log {@link Level} is {@link Level#INFO}.
|
---|
[1] | 463 | * </p>
|
---|
| 464 | *
|
---|
| 465 | * @param traceMsg
|
---|
| 466 | * message that is send to the observers
|
---|
| 467 | */
|
---|
[633] | 468 | @Deprecated
|
---|
[1] | 469 | public static void traceln(String traceMsg) {
|
---|
[199] | 470 | for (ITraceListener observer : theInstance.traceListener) {
|
---|
[633] | 471 | observer.traceMsg(traceMsg + StringTools.ENDLINE, Level.INFO);
|
---|
[1] | 472 | }
|
---|
| 473 | }
|
---|
[633] | 474 |
|
---|
| 475 | /**
|
---|
| 476 | * <p>
|
---|
| 477 | * Sends a debug message to all observers containing the message that was
|
---|
| 478 | * passed to this function and adds an {@link StringTools#ENDLINE} to the
|
---|
| 479 | * message.
|
---|
| 480 | * </p>
|
---|
| 481 | *
|
---|
[635] | 482 | * @param logLevel log level of the message
|
---|
[633] | 483 | * @param traceMsg message that is send to the observers
|
---|
| 484 | */
|
---|
[635] | 485 | public static void traceln(Level logLevel, String traceMsg) {
|
---|
[633] | 486 | for( ITraceListener observer : theInstance.traceListener) {
|
---|
| 487 | observer.traceMsg(traceMsg + StringTools.ENDLINE, logLevel);
|
---|
| 488 | }
|
---|
| 489 | }
|
---|
[1] | 490 |
|
---|
[186] | 491 | /**
|
---|
| 492 | * <p>
|
---|
| 493 | * Called by {@link CommandExecuter#exec(String)}.
|
---|
| 494 | * </p>
|
---|
| 495 | *
|
---|
| 496 | * @param command
|
---|
| 497 | * command that is executed
|
---|
| 498 | */
|
---|
| 499 | static void commandNotification(String command) {
|
---|
[199] | 500 | for (ICommandListener observer : theInstance.commandListener) {
|
---|
[186] | 501 | observer.commandNotification(command);
|
---|
| 502 | }
|
---|
| 503 | }
|
---|
| 504 |
|
---|
[1] | 505 | }
|
---|