package de.ugoe.cs.util.console; import java.util.ArrayList; import java.util.List; import de.ugoe.cs.util.StringTools; /** *
* This class provides an interface for communication with the user without have * to rely on a specific user interface. Thus, it can be used to decouple the * programs logic from its user interface. *
** {@link Command} objects can be used to execute behavior. *
** To send output to the user interface, the Observer pattern is used. The * Console is an observable, the concrete user interfaces are the observers. The * interface for the observers is {@link ConsoleObserver}. *
* * @author Steffen Herbold */ public final class Console { /** ** List of observers. *
*/ private List* Handle of the Console instance. *
*/ private static Console theInstance = null; /** ** Returns the instance of Console. If no instances exists yet, a new one is * created. *
* * @return instance of this class */ public static Console getInstance() { if (theInstance == null) { theInstance = new Console(); } return theInstance; } /** ** Creates a new Console. Private to prevent multiple instances (Singleton). *
*/ private Console() { observers = new ArrayList* Register a new observer. *
* * @param observer * observer to be added */ public void registerObserver(ConsoleObserver observer) { observers.add(observer); } /** ** Remove an observer. If the observer is not found, nothing is done. *
* * @param observer * observer to be removed */ public void deleteObserver(ConsoleObserver observer) { observers.remove(observer); } /** ** Sends a message to all observers containing the message that was passed * to this function. *
* * @param msg * message that is send to the console */ public static void print(String msg) { if (theInstance == null) { getInstance(); } for (ConsoleObserver observer : theInstance.observers) { observer.updateText(msg); } } /** ** Sends a message to all observers containing the message that was passed * to this function and adds an endline to the message. *
* * @param msg * message that is send to the observers */ public static void println(String msg) { if (theInstance == null) { getInstance(); } for (ConsoleObserver observer : theInstance.observers) { observer.updateText(msg + StringTools.ENDLINE); } } /** ** Sends an error message to all observers containing the message that was * passed to this function. *
* * @param errMsg * message that is send to the observers */ public static void printerr(String errMsg) { if (theInstance == null) { getInstance(); } for (ConsoleObserver observer : theInstance.observers) { observer.errStream(errMsg); } } /** ** Sends an error message to all observers containing the message that was * passed to this function and adds an endline to the message. *
* * @param errMsg * message that is send to the observers */ public static void printerrln(String errMsg) { if (theInstance == null) { getInstance(); } for (ConsoleObserver observer : theInstance.observers) { observer.errStream(errMsg + StringTools.ENDLINE); } } /** ** Sends an exception to all observers to print its stack trace. *
* * @param e * exception whose stack trace is to be printed */ public static void printStacktrace(Exception e) { if (theInstance == null) { getInstance(); } for (ConsoleObserver observer : theInstance.observers) { observer.printStacktrace(e); } } /** ** Sends a debug message to all observers containing the message that was * passed to this function. *
* * @param traceMsg * message that is send to the observers */ public static void trace(String traceMsg) { if (theInstance == null) { getInstance(); } for (ConsoleObserver observer : theInstance.observers) { observer.trace(traceMsg); } } /** ** Sends a debug message to all observers containing the message that was * passed to this function and adds an endline to the message. *
* * @param traceMsg * message that is send to the observers */ public static void traceln(String traceMsg) { if (theInstance == null) { getInstance(); } for (ConsoleObserver observer : theInstance.observers) { observer.trace(traceMsg + StringTools.ENDLINE); } } }