|
1 | 1 | package wa.status; |
| 2 | + |
2 | 3 | import org.apache.logging.log4j.LogManager; |
3 | 4 | import org.apache.logging.log4j.Logger; |
4 | 5 |
|
|
19 | 20 | */ |
20 | 21 |
|
21 | 22 | public class StatusConsole implements StatusIndicator { |
22 | | - // Initialize our logger |
23 | | - private static final Logger LOG = LogManager.getLogger(StatusConsole.class); |
| 23 | + // Initialize our logger |
| 24 | + private static final Logger LOG = LogManager.getLogger(StatusConsole.class); |
24 | 25 |
|
25 | | - private volatile boolean statusOn = false; |
26 | | - private volatile boolean blink = false; |
27 | | - private Thread blinkThread = null; |
| 26 | + private volatile boolean statusOn = false; |
| 27 | + private volatile boolean blink = false; |
| 28 | + private Thread blinkThread = null; |
28 | 29 |
|
29 | | - public StatusConsole() { |
30 | | - } |
| 30 | + public StatusConsole() { |
| 31 | + } |
31 | 32 |
|
32 | | - private void cancelBlink() { |
33 | | - blink = false; |
34 | | - statusOn = false; |
35 | | - if (null != blinkThread) { |
36 | | - blinkThread.interrupt(); |
37 | | - try { |
38 | | - blinkThread.join(5000); |
39 | | - } catch (InterruptedException ignore) { |
40 | | - // Ignore... |
41 | | - } |
42 | | - blinkThread = null; |
43 | | - } |
44 | | - } |
| 33 | + private void cancelBlink() { |
| 34 | + blink = false; |
| 35 | + statusOn = false; |
| 36 | + if (null != blinkThread) { |
| 37 | + blinkThread.interrupt(); |
| 38 | + try { |
| 39 | + blinkThread.join(5000); |
| 40 | + } catch (InterruptedException ignore) { |
| 41 | + // Ignore... |
| 42 | + } |
| 43 | + blinkThread = null; |
| 44 | + } |
| 45 | + } |
45 | 46 |
|
46 | | - private Thread createBlinkThread(long onOffTime) { |
47 | | - Thread t = new Thread(() -> { |
48 | | - try { |
49 | | - statusOn = false; |
50 | | - while (blink) { |
51 | | - char statusChar = (toggle() ? '\u00D6' : '\u00F8'); // on='large circle with dots' : off='small circle with diagonal cross' |
52 | | - System.out.print("<<" + statusChar + ">>"); |
53 | | - Thread.sleep(onOffTime); |
54 | | - } |
55 | | - } catch (InterruptedException ie) { |
56 | | - // Cancel using 'finally'... |
57 | | - } finally { |
58 | | - System.out.println("\nStatus indicator: blink off"); |
59 | | - blink = false; |
60 | | - statusOn = false; |
61 | | - } |
62 | | - }); |
63 | | - t.setName("Status-blink-thread"); |
64 | | - return t; |
65 | | - } |
| 47 | + private Thread createBlinkThread(long onOffTime) { |
| 48 | + Thread t = new Thread(() -> { |
| 49 | + try { |
| 50 | + statusOn = false; |
| 51 | + while (blink) { |
| 52 | + char statusChar = (toggle() ? '\u00D6' : '\u00F8'); // on='large circle with dots' : off='small circle with diagonal cross' |
| 53 | + System.out.print("<<" + statusChar + ">>"); |
| 54 | + Thread.sleep(onOffTime); |
| 55 | + } |
| 56 | + } catch (InterruptedException ie) { |
| 57 | + // Cancel using 'finally'... |
| 58 | + } finally { |
| 59 | + System.out.println("\nStatus indicator: blink off"); |
| 60 | + blink = false; |
| 61 | + statusOn = false; |
| 62 | + } |
| 63 | + }); |
| 64 | + t.setName("Status-blink-thread"); |
| 65 | + return t; |
| 66 | + } |
66 | 67 |
|
67 | | - private boolean toggle() { |
68 | | - statusOn = !statusOn; |
69 | | - return statusOn; |
70 | | - } |
| 68 | + private boolean toggle() { |
| 69 | + statusOn = !statusOn; |
| 70 | + return statusOn; |
| 71 | + } |
71 | 72 |
|
72 | | - /* (non-Javadoc) |
73 | | - * @see wa.status.StatusIndicator#on() |
74 | | - */ |
75 | | - @Override |
76 | | - public void on() { |
77 | | - cancelBlink(); |
78 | | - this.statusOn = true; |
79 | | - LOG.info("\nStatus indicator: on"); |
80 | | - } |
| 73 | + /* |
| 74 | + * (non-Javadoc) |
| 75 | + * |
| 76 | + * @see wa.status.StatusIndicator#on() |
| 77 | + */ |
| 78 | + @Override |
| 79 | + public void on() { |
| 80 | + cancelBlink(); |
| 81 | + this.statusOn = true; |
| 82 | + LOG.info("\nStatus indicator: on"); |
| 83 | + } |
81 | 84 |
|
82 | | - /* (non-Javadoc) |
83 | | - * @see wa.status.StatusIndicator#isOn() |
84 | | - */ |
85 | | - @Override |
86 | | - public boolean isOn() { |
87 | | - return this.statusOn; |
88 | | - } |
| 85 | + /* |
| 86 | + * (non-Javadoc) |
| 87 | + * |
| 88 | + * @see wa.status.StatusIndicator#isOn() |
| 89 | + */ |
| 90 | + @Override |
| 91 | + public boolean isOn() { |
| 92 | + return this.statusOn; |
| 93 | + } |
89 | 94 |
|
90 | | - /* (non-Javadoc) |
91 | | - * @see wa.status.StatusIndicator#off() |
92 | | - */ |
93 | | - @Override |
94 | | - public void off() { |
95 | | - cancelBlink(); |
96 | | - this.statusOn = false; |
97 | | - LOG.info("\nStatus indicator: off"); |
98 | | - } |
| 95 | + /* |
| 96 | + * (non-Javadoc) |
| 97 | + * |
| 98 | + * @see wa.status.StatusIndicator#off() |
| 99 | + */ |
| 100 | + @Override |
| 101 | + public void off() { |
| 102 | + cancelBlink(); |
| 103 | + this.statusOn = false; |
| 104 | + LOG.info("\nStatus indicator: off"); |
| 105 | + } |
99 | 106 |
|
100 | | - /* (non-Javadoc) |
101 | | - * @see wa.status.StatusIndicator#isOff() |
102 | | - */ |
103 | | - @Override |
104 | | - public boolean isOff() { |
105 | | - return !this.statusOn; |
106 | | - } |
| 107 | + /* |
| 108 | + * (non-Javadoc) |
| 109 | + * |
| 110 | + * @see wa.status.StatusIndicator#isOff() |
| 111 | + */ |
| 112 | + @Override |
| 113 | + public boolean isOff() { |
| 114 | + return !this.statusOn; |
| 115 | + } |
107 | 116 |
|
108 | | - /* (non-Javadoc) |
109 | | - * @see wa.status.StatusIndicator#blink(long onOffTime) |
110 | | - */ |
111 | | - @Override |
112 | | - public void blink(long onOffTime) { |
113 | | - cancelBlink(); |
114 | | - blink = true; |
115 | | - blinkThread = createBlinkThread(onOffTime); |
116 | | - blinkThread.start(); |
117 | | - LOG.info("Status indicator: blink on/off: " + onOffTime); |
118 | | - } |
| 117 | + /* |
| 118 | + * (non-Javadoc) |
| 119 | + * |
| 120 | + * @see wa.status.StatusIndicator#blink(long onOffTime) |
| 121 | + */ |
| 122 | + @Override |
| 123 | + public void blink(long onOffTime) { |
| 124 | + cancelBlink(); |
| 125 | + blink = true; |
| 126 | + blinkThread = createBlinkThread(onOffTime); |
| 127 | + blinkThread.start(); |
| 128 | + LOG.info("Status indicator: blink on/off: " + onOffTime); |
| 129 | + } |
119 | 130 |
|
120 | | - /* (non-Javadoc) |
121 | | - * @see wa.status.StatusIndicator#isBlinking() |
122 | | - */ |
123 | | - @Override |
124 | | - public boolean isBlinking() { |
125 | | - return (null != blinkThread); |
126 | | - } |
| 131 | + /* |
| 132 | + * (non-Javadoc) |
| 133 | + * |
| 134 | + * @see wa.status.StatusIndicator#isBlinking() |
| 135 | + */ |
| 136 | + @Override |
| 137 | + public boolean isBlinking() { |
| 138 | + return (null != blinkThread); |
| 139 | + } |
127 | 140 | } |
0 commit comments