1 | package de.ugoe.cs.util.console;
|
---|
2 |
|
---|
3 | import java.util.ArrayList;
|
---|
4 | import java.util.List;
|
---|
5 |
|
---|
6 | import de.ugoe.cs.util.StringTools;
|
---|
7 |
|
---|
8 |
|
---|
9 | /**
|
---|
10 | * <p>
|
---|
11 | * This class provides an interface for communication with the user without have
|
---|
12 | * to rely on a specific user interface. Thus, it can be used to decouple the
|
---|
13 | * programs logic from its user interface.
|
---|
14 | * </p>
|
---|
15 | * <p>
|
---|
16 | * {@link Command} objects can be used to execute behavior.
|
---|
17 | * </p>
|
---|
18 | * <p>
|
---|
19 | * To send output to the user interface, the Observer pattern is used. The
|
---|
20 | * Console is an observable, the concrete user interfaces are the observers. The
|
---|
21 | * interface for the observers is {@link ConsoleObserver}.
|
---|
22 | * </p>
|
---|
23 | *
|
---|
24 | * @author Steffen Herbold
|
---|
25 | */
|
---|
26 | public final class Console {
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * <p>
|
---|
30 | * List of observers.
|
---|
31 | * </p>
|
---|
32 | */
|
---|
33 | private List<ConsoleObserver> observers;
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * <p>
|
---|
37 | * Handle of the Console instance.
|
---|
38 | * </p>
|
---|
39 | */
|
---|
40 | private static Console theInstance = null;
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * <p>
|
---|
44 | * Returns the instance of Console. If no instances exists yet, a new one is
|
---|
45 | * created.
|
---|
46 | * </p>
|
---|
47 | *
|
---|
48 | * @return instance of this class
|
---|
49 | */
|
---|
50 | public static Console getInstance() {
|
---|
51 | if (theInstance == null) {
|
---|
52 | theInstance = new Console();
|
---|
53 | }
|
---|
54 | return theInstance;
|
---|
55 | }
|
---|
56 |
|
---|
57 | /**
|
---|
58 | * <p>
|
---|
59 | * Creates a new Console. Private to prevent multiple instances (Singleton).
|
---|
60 | * </p>
|
---|
61 | */
|
---|
62 | private Console() {
|
---|
63 | observers = new ArrayList<ConsoleObserver>();
|
---|
64 | }
|
---|
65 |
|
---|
66 | /**
|
---|
67 | * <p>
|
---|
68 | * Register a new observer.
|
---|
69 | * </p>
|
---|
70 | *
|
---|
71 | * @param observer
|
---|
72 | * observer to be added
|
---|
73 | */
|
---|
74 | public void registerObserver(ConsoleObserver observer) {
|
---|
75 | observers.add(observer);
|
---|
76 | }
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * <p>
|
---|
80 | * Remove an observer. If the observer is not found, nothing is done.
|
---|
81 | * </p>
|
---|
82 | *
|
---|
83 | * @param observer
|
---|
84 | * observer to be removed
|
---|
85 | */
|
---|
86 | public void deleteObserver(ConsoleObserver observer) {
|
---|
87 | observers.remove(observer);
|
---|
88 | }
|
---|
89 |
|
---|
90 | /**
|
---|
91 | * <p>
|
---|
92 | * Sends a message to all observers containing the message that was passed
|
---|
93 | * to this function.
|
---|
94 | * </p>
|
---|
95 | *
|
---|
96 | * @param msg
|
---|
97 | * message that is send to the console
|
---|
98 | */
|
---|
99 | public static void print(String msg) {
|
---|
100 | if (theInstance == null) {
|
---|
101 | getInstance();
|
---|
102 | }
|
---|
103 | for (ConsoleObserver observer : theInstance.observers) {
|
---|
104 | observer.updateText(msg);
|
---|
105 | }
|
---|
106 | }
|
---|
107 |
|
---|
108 | /**
|
---|
109 | * <p>
|
---|
110 | * Sends a message to all observers containing the message that was passed
|
---|
111 | * to this function and adds an endline to the message.
|
---|
112 | * </p>
|
---|
113 | *
|
---|
114 | * @param msg
|
---|
115 | * message that is send to the observers
|
---|
116 | */
|
---|
117 | public static void println(String msg) {
|
---|
118 | if (theInstance == null) {
|
---|
119 | getInstance();
|
---|
120 | }
|
---|
121 | for (ConsoleObserver observer : theInstance.observers) {
|
---|
122 | observer.updateText(msg
|
---|
123 | + StringTools.ENDLINE);
|
---|
124 | }
|
---|
125 | }
|
---|
126 |
|
---|
127 | /**
|
---|
128 | * <p>
|
---|
129 | * Sends an error message to all observers containing the message that was
|
---|
130 | * passed to this function.
|
---|
131 | * </p>
|
---|
132 | *
|
---|
133 | * @param errMsg
|
---|
134 | * message that is send to the observers
|
---|
135 | */
|
---|
136 | public static void printerr(String errMsg) {
|
---|
137 | if (theInstance == null) {
|
---|
138 | getInstance();
|
---|
139 | }
|
---|
140 | for (ConsoleObserver observer : theInstance.observers) {
|
---|
141 | observer.errStream(errMsg);
|
---|
142 | }
|
---|
143 | }
|
---|
144 |
|
---|
145 | /**
|
---|
146 | * <p>
|
---|
147 | * Sends an error message to all observers containing the message that was
|
---|
148 | * passed to this function and adds an endline to the message.
|
---|
149 | * </p>
|
---|
150 | *
|
---|
151 | * @param errMsg
|
---|
152 | * message that is send to the observers
|
---|
153 | */
|
---|
154 | public static void printerrln(String errMsg) {
|
---|
155 | if (theInstance == null) {
|
---|
156 | getInstance();
|
---|
157 | }
|
---|
158 | for (ConsoleObserver observer : theInstance.observers) {
|
---|
159 | observer.errStream(errMsg
|
---|
160 | + StringTools.ENDLINE);
|
---|
161 | }
|
---|
162 | }
|
---|
163 |
|
---|
164 | /**
|
---|
165 | * <p>
|
---|
166 | * Sends an exception to all observers to print its stack trace.
|
---|
167 | * </p>
|
---|
168 | *
|
---|
169 | * @param e
|
---|
170 | * exception whose stack trace is to be printed
|
---|
171 | */
|
---|
172 | public static void printStacktrace(Exception e) {
|
---|
173 | if (theInstance == null) {
|
---|
174 | getInstance();
|
---|
175 | }
|
---|
176 | for (ConsoleObserver observer : theInstance.observers) {
|
---|
177 | observer.printStacktrace(e);
|
---|
178 | }
|
---|
179 | }
|
---|
180 |
|
---|
181 | /**
|
---|
182 | * <p>
|
---|
183 | * Sends a debug message to all observers containing the message that was
|
---|
184 | * passed to this function.
|
---|
185 | * </p>
|
---|
186 | *
|
---|
187 | * @param traceMsg
|
---|
188 | * message that is send to the observers
|
---|
189 | */
|
---|
190 | public static void trace(String traceMsg) {
|
---|
191 | if (theInstance == null) {
|
---|
192 | getInstance();
|
---|
193 | }
|
---|
194 | for (ConsoleObserver observer : theInstance.observers) {
|
---|
195 | observer.trace(traceMsg);
|
---|
196 | }
|
---|
197 | }
|
---|
198 |
|
---|
199 | /**
|
---|
200 | * <p>
|
---|
201 | * Sends a debug message to all observers containing the message that was
|
---|
202 | * passed to this function and adds an endline to the message.
|
---|
203 | * </p>
|
---|
204 | *
|
---|
205 | * @param traceMsg
|
---|
206 | * message that is send to the observers
|
---|
207 | */
|
---|
208 | public static void traceln(String traceMsg) {
|
---|
209 | if (theInstance == null) {
|
---|
210 | getInstance();
|
---|
211 | }
|
---|
212 | for (ConsoleObserver observer : theInstance.observers) {
|
---|
213 | observer.trace(traceMsg
|
---|
214 | + StringTools.ENDLINE);
|
---|
215 | }
|
---|
216 | }
|
---|
217 |
|
---|
218 | }
|
---|