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