View Javadoc
1   /*
2    * Copyright (C) 2016 Ronald Jack Jenkins Jr.
3    *
4    * This program is free software: you can redistribute it and/or modify
5    * it under the terms of the GNU General Public License as published by
6    * the Free Software Foundation, either version 3 of the License, or
7    * (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU General Public License
15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16   */
17  package info.ronjenkins.slf4bukkit;
18  
19  /**
20   * A {@link StringBuilder}-like class with a fluent API for adding content
21   * colored via {@link ColorMarker}s. You may call {@link #toString()} to get the
22   * current value and then continue adding content to this object. This class is
23   * thread-safe.
24   *
25   * @author Ronald Jack Jenkins Jr.
26   */
27  public final class ColorString {
28  
29    private ColorMarker         currentColor = ColorMarker.NONE;
30    private final StringBuilder value        = new StringBuilder();
31  
32    /** Constructor. */
33    public ColorString() {
34    }
35  
36    /**
37     * Sets the color to {@link ColorMarker#AQUA}, then calls
38     * {@link StringBuilder#append(String)}.
39     *
40     * @param append
41     *          the string to append.
42     * @return this.
43     */
44    public ColorString aqua(final String append) {
45      synchronized (this.value) {
46        this.setColor(ColorMarker.AQUA);
47        this.value.append(append);
48        return this;
49      }
50    }
51  
52    /**
53     * Sets the color to {@link ColorMarker#BLACK}, then calls
54     * {@link StringBuilder#append(String)}.
55     *
56     * @param append
57     *          the string to append.
58     * @return this.
59     */
60    public ColorString black(final String append) {
61      synchronized (this.value) {
62        this.setColor(ColorMarker.BLACK);
63        this.value.append(append);
64        return this;
65      }
66    }
67  
68    /**
69     * Sets the color to {@link ColorMarker#BLUE}, then calls
70     * {@link StringBuilder#append(String)}.
71     *
72     * @param append
73     *          the string to append.
74     * @return this.
75     */
76    public ColorString blue(final String append) {
77      synchronized (this.value) {
78        this.setColor(ColorMarker.BLUE);
79        this.value.append(append);
80        return this;
81      }
82    }
83  
84    /**
85     * Sets the color to {@link ColorMarker#DARK_AQUA}, then calls
86     * {@link StringBuilder#append(String)}.
87     *
88     * @param append
89     *          the string to append.
90     * @return this.
91     */
92    public ColorString darkAqua(final String append) {
93      synchronized (this.value) {
94        this.setColor(ColorMarker.DARK_AQUA);
95        this.value.append(append);
96        return this;
97      }
98    }
99  
100   /**
101    * Sets the color to {@link ColorMarker#DARK_BLUE}, then calls
102    * {@link StringBuilder#append(String)}.
103    *
104    * @param append
105    *          the string to append.
106    * @return this.
107    */
108   public ColorString darkBlue(final String append) {
109     synchronized (this.value) {
110       this.setColor(ColorMarker.DARK_BLUE);
111       this.value.append(append);
112       return this;
113     }
114   }
115 
116   /**
117    * Sets the color to {@link ColorMarker#DARK_GRAY}, then calls
118    * {@link StringBuilder#append(String)}.
119    *
120    * @param append
121    *          the string to append.
122    * @return this.
123    */
124   public ColorString darkGray(final String append) {
125     synchronized (this.value) {
126       this.setColor(ColorMarker.DARK_GRAY);
127       this.value.append(append);
128       return this;
129     }
130   }
131 
132   /**
133    * Sets the color to {@link ColorMarker#DARK_GREEN}, then calls
134    * {@link StringBuilder#append(String)}.
135    *
136    * @param append
137    *          the string to append.
138    * @return this.
139    */
140   public ColorString darkGreen(final String append) {
141     synchronized (this.value) {
142       this.setColor(ColorMarker.DARK_GREEN);
143       this.value.append(append);
144       return this;
145     }
146   }
147 
148   /**
149    * Sets the color to {@link ColorMarker#DARK_RED}, then calls
150    * {@link StringBuilder#append(String)}.
151    *
152    * @param append
153    *          the string to append.
154    * @return this.
155    */
156   public ColorString darkRed(final String append) {
157     synchronized (this.value) {
158       this.setColor(ColorMarker.DARK_RED);
159       this.value.append(append);
160       return this;
161     }
162   }
163 
164   /**
165    * Sets the color to {@link ColorMarker#GOLD}, then calls
166    * {@link StringBuilder#append(String)}.
167    *
168    * @param append
169    *          the string to append.
170    * @return this.
171    */
172   public ColorString gold(final String append) {
173     synchronized (this.value) {
174       this.setColor(ColorMarker.GOLD);
175       this.value.append(append);
176       return this;
177     }
178   }
179 
180   /**
181    * Sets the color to {@link ColorMarker#GRAY}, then calls
182    * {@link StringBuilder#append(String)}.
183    *
184    * @param append
185    *          the string to append.
186    * @return this.
187    */
188   public ColorString gray(final String append) {
189     synchronized (this.value) {
190       this.setColor(ColorMarker.GRAY);
191       this.value.append(append);
192       return this;
193     }
194   }
195 
196   /**
197    * Sets the color to {@link ColorMarker#GREEN}, then calls
198    * {@link StringBuilder#append(String)}.
199    *
200    * @param append
201    *          the string to append.
202    * @return this.
203    */
204   public ColorString green(final String append) {
205     synchronized (this.value) {
206       this.setColor(ColorMarker.GREEN);
207       this.value.append(append);
208       return this;
209     }
210   }
211 
212   /**
213    * Resets all formatting at the current position, then calls
214    * {@link StringBuilder#append(String)}.
215    *
216    * @param append
217    *          the string to append.
218    * @return this.
219    */
220   public ColorString none(final String append) {
221     synchronized (this.value) {
222       this.setColor(ColorMarker.NONE);
223       this.value.append(append);
224       return this;
225     }
226   }
227 
228   /**
229    * Sets the color to {@link ColorMarker#LIGHT_PURPLE}, then calls
230    * {@link StringBuilder#append(String)}.
231    *
232    * @param append
233    *          the string to append.
234    * @return this.
235    */
236   public ColorString pink(final String append) {
237     synchronized (this.value) {
238       this.setColor(ColorMarker.LIGHT_PURPLE);
239       this.value.append(append);
240       return this;
241     }
242   }
243 
244   /**
245    * Sets the color to {@link ColorMarker#DARK_PURPLE}, then calls
246    * {@link StringBuilder#append(String)}.
247    *
248    * @param append
249    *          the string to append.
250    * @return this.
251    */
252   public ColorString purple(final String append) {
253     synchronized (this.value) {
254       this.setColor(ColorMarker.DARK_PURPLE);
255       this.value.append(append);
256       return this;
257     }
258   }
259 
260   /**
261    * Sets the color to {@link ColorMarker#RED}, then calls
262    * {@link StringBuilder#append(String)}.
263    *
264    * @param append
265    *          the string to append.
266    * @return this.
267    */
268   public ColorString red(final String append) {
269     synchronized (this.value) {
270       this.setColor(ColorMarker.RED);
271       this.value.append(append);
272       return this;
273     }
274   }
275 
276   /**
277    * Resets all formatting at the current position, then returns the string's
278    * value.
279    *
280    * @return never null.
281    */
282   @Override
283   public String toString() {
284     synchronized (this.value) {
285       this.setColor(ColorMarker.NONE);
286       return this.value.toString();
287     }
288   }
289 
290   /**
291    * Appends the given color to the string sequence, then returns the string's
292    * value.
293    *
294    * @param color
295    *          the desired color suffix. Null is coerced to
296    *          {@link ColorMarker#NONE}, which resets all formatting at the
297    *          current position.
298    * @return never null. Returns {@link #toString()} if the color is null.
299    */
300   public String toString(final ColorMarker color) {
301     synchronized (this.value) {
302       if (color == null) { return this.toString(); }
303       this.setColor(color);
304       return this.value.toString();
305     }
306   }
307 
308   /**
309    * Sets the color to {@link ColorMarker#WHITE}, then calls
310    * {@link StringBuilder#append(String)}.
311    *
312    * @param append
313    *          the string to append.
314    * @return this.
315    */
316   public ColorString white(final String append) {
317     synchronized (this.value) {
318       this.setColor(ColorMarker.WHITE);
319       this.value.append(append);
320       return this;
321     }
322   }
323 
324   /**
325    * Sets the color to {@link ColorMarker#YELLOW}, then calls
326    * {@link StringBuilder#append(String)}.
327    *
328    * @param append
329    *          the string to append.
330    * @return this.
331    */
332   public ColorString yellow(final String append) {
333     synchronized (this.value) {
334       this.setColor(ColorMarker.YELLOW);
335       this.value.append(append);
336       return this;
337     }
338   }
339 
340   private void setColor(final ColorMarker color) {
341     synchronized (this.value) {
342       if (color != this.currentColor) {
343         this.value.append(color.getValue());
344         this.currentColor = color;
345       }
346     }
347   }
348 
349 }