Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
FormatterFactory |
|
| 1.8333333333333333;1,833 |
1 | /* | |
2 | * This file is part of AceLogger. | |
3 | * | |
4 | * AceLogger is free software: you can redistribute it and/or modify it under the terms of the GNU | |
5 | * Lesser General Public License as published by the Free Software Foundation, either version 3 of | |
6 | * the License, or (at your option) any later version. | |
7 | * | |
8 | * AceLogger is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without | |
9 | * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
10 | * Lesser General Public License for more details. | |
11 | * | |
12 | * You should have received a copy of the GNU Lesser General Public License along with AceLogger. | |
13 | * If not, see <http://www.gnu.org/licenses/lgpl-3.0.html>. | |
14 | */ | |
15 | package net.sourceforge.acelogger.factory; | |
16 | ||
17 | import java.util.ArrayList; | |
18 | import java.util.HashMap; | |
19 | import java.util.List; | |
20 | import java.util.Map; | |
21 | ||
22 | import net.sourceforge.acelogger.constants.FormatterConstants; | |
23 | import net.sourceforge.acelogger.execution.LogController; | |
24 | import net.sourceforge.acelogger.formatter.Formatter; | |
25 | ||
26 | /** | |
27 | * Factory for managing instances of configured formatters. | |
28 | * | |
29 | * @author Zardi (https://sourceforge.net/users/daniel_zardi) | |
30 | * @version 1.0.0 | |
31 | * @since 1.0.0 | |
32 | */ | |
33 | public final class FormatterFactory { | |
34 | ||
35 | /** | |
36 | * A instance of this class (the only one). | |
37 | */ | |
38 | 1 | private static final FormatterFactory INSTANCE = new FormatterFactory(); |
39 | ||
40 | /** | |
41 | * Used when no configured formatter is found for a given identifier. | |
42 | */ | |
43 | private Formatter defaultFormatter; | |
44 | ||
45 | /** | |
46 | * Maps each configured formatter to a unique identifier. | |
47 | */ | |
48 | private final Map<String, Formatter> registeredFormatters; | |
49 | ||
50 | /** | |
51 | * Creates a new FormatterFactory (This class is a singleton). | |
52 | * | |
53 | * @since 1.0.0 | |
54 | */ | |
55 | 1 | private FormatterFactory() { |
56 | 1 | registeredFormatters = new HashMap<String, Formatter>(); |
57 | 1 | defaultFormatter = FormatterConstants.EMPTY; |
58 | 1 | } |
59 | ||
60 | /** | |
61 | * Obtains a properly configured formatter for the given identifier. | |
62 | * | |
63 | * @param identifier | |
64 | * The identifier of the intended formatter. | |
65 | * @return A properly configured formatter for the given identifier. If the identifier is not | |
66 | * found, then the default formatter is returned. | |
67 | * @see FormatterFactory#getDefaultFormatter() | |
68 | * @since 1.0.0 | |
69 | */ | |
70 | public static Formatter getFormatter(String identifier) { | |
71 | 0 | LogController.ensureInitialization(); |
72 | 0 | Formatter intendedFormatter = INSTANCE.registeredFormatters.get(identifier); |
73 | 0 | if (intendedFormatter == null) { |
74 | 0 | int lastDotIndex = identifier.lastIndexOf('.'); |
75 | 0 | if (lastDotIndex > 0) { |
76 | 0 | intendedFormatter = getFormatter(identifier.substring(0, lastDotIndex)); |
77 | } else { | |
78 | 0 | intendedFormatter = INSTANCE.defaultFormatter; |
79 | } | |
80 | } | |
81 | 0 | return intendedFormatter; |
82 | } | |
83 | ||
84 | /** | |
85 | * Sets the default formatter of this factory, the one used when no formatter is found for a | |
86 | * given identifier. | |
87 | * | |
88 | * @param defaultFormatter | |
89 | * The formatter that will be used as the default one from now. | |
90 | * @return The previous default formatter. | |
91 | * @since 1.0.0 | |
92 | */ | |
93 | public static Formatter setDefaultFormatter(Formatter defaultFormatter) { | |
94 | 0 | LogController.ensureInitialization(); |
95 | 0 | Formatter previous = INSTANCE.defaultFormatter; |
96 | 0 | if (defaultFormatter != null) { |
97 | 0 | INSTANCE.defaultFormatter = defaultFormatter; |
98 | } | |
99 | 0 | return previous; |
100 | } | |
101 | ||
102 | /** | |
103 | * Returns the default formatter of this factory, the one used when no formatter is found for a | |
104 | * given identifier. | |
105 | * | |
106 | * @return The default formatter. | |
107 | * @since 1.0.0 | |
108 | */ | |
109 | public static Formatter getDefaultFormatter() { | |
110 | 1 | LogController.ensureInitialization(); |
111 | 1 | return INSTANCE.defaultFormatter; |
112 | } | |
113 | ||
114 | /** | |
115 | * Returns a list of all formatters registered in this factory. | |
116 | * | |
117 | * @return A list of formatters. | |
118 | * @since 1.0.0 | |
119 | */ | |
120 | public static List<Formatter> getRegisteredFormatters() { | |
121 | 4 | LogController.ensureInitialization(); |
122 | 4 | List<Formatter> allFormatters = new ArrayList<Formatter>(INSTANCE.registeredFormatters |
123 | .values()); | |
124 | 4 | return allFormatters; |
125 | } | |
126 | ||
127 | /** | |
128 | * Registers an appender within this factory. The mapped identifier will be the formatters | |
129 | * identifier. | |
130 | * | |
131 | * @param formatter | |
132 | * The formatter to be registered. | |
133 | * @return The default formatter if there was no previous mapping for the identifier or the | |
134 | * previous configured formatter otherwise. | |
135 | * @see FormatterFactory#getDefaultFormatter() | |
136 | * @since 1.0.0 | |
137 | */ | |
138 | public static Formatter registerFormatter(Formatter formatter) { | |
139 | 4 | LogController.ensureInitialization(); |
140 | 4 | Formatter intendedFormatter = null; |
141 | 4 | if (formatter != null) { |
142 | 3 | intendedFormatter = INSTANCE.registeredFormatters.put(formatter.getIdentifier(), |
143 | formatter); | |
144 | } | |
145 | 4 | if (intendedFormatter == null) { |
146 | 4 | intendedFormatter = INSTANCE.defaultFormatter; |
147 | } | |
148 | 4 | return intendedFormatter; |
149 | } | |
150 | ||
151 | } |