Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
HolidayManager |
|
| 1.28;1,28 | ||||
HolidayManager$1 |
|
| 1.28;1,28 |
1 | /** | |
2 | * Copyright 2010 Sven Diedrichsen | |
3 | * | |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | * you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at | |
7 | * | |
8 | * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | * | |
10 | * Unless required by applicable law or agreed to in writing, software | |
11 | * distributed under the License is distributed on an | |
12 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | |
13 | * express or implied. See the License for the specific language | |
14 | * governing permissions and limitations under the License. | |
15 | */ | |
16 | package de.jollyday; | |
17 | ||
18 | import de.jollyday.caching.HolidayManagerValueHandler; | |
19 | import de.jollyday.configuration.ConfigurationProviderManager; | |
20 | import de.jollyday.datasource.ConfigurationDataSource; | |
21 | import de.jollyday.util.Cache; | |
22 | import de.jollyday.util.Cache.ValueHandler; | |
23 | import de.jollyday.util.CalendarUtil; | |
24 | ||
25 | import java.time.LocalDate; | |
26 | import java.util.Calendar; | |
27 | import java.util.HashSet; | |
28 | import java.util.Properties; | |
29 | import java.util.Set; | |
30 | import java.util.logging.Level; | |
31 | import java.util.logging.Logger; | |
32 | ||
33 | /** | |
34 | * Abstract base class for all holiday manager implementations. Upon call of | |
35 | * getInstance method the implementing class will be read from the | |
36 | * jollyday.properties file and instantiated. | |
37 | * | |
38 | * @author Sven Diedrichsen | |
39 | * @version $Id: $ | |
40 | */ | |
41 | 76 | public abstract class HolidayManager { |
42 | ||
43 | 2 | private static final Logger LOG = Logger.getLogger(HolidayManager.class |
44 | 1 | .getName()); |
45 | /** | |
46 | * Signifies if caching of manager instances is enabled. If not every call | |
47 | * to getInstance will return a newly instantiated and initialized manager. | |
48 | */ | |
49 | 1 | private static boolean CACHING_ENABLED = true; |
50 | /** | |
51 | * Cache for manager instances on a per country basis. | |
52 | */ | |
53 | 1 | private static final Cache<HolidayManager> HOLIDAY_MANAGER_CACHE = new Cache<>(); |
54 | /** | |
55 | * Manager for configuration providers. Delivers the jollyday configuration. | |
56 | */ | |
57 | 1 | private static ConfigurationProviderManager CONFIGURATION_MANAGER_PROVIDER = new ConfigurationProviderManager(); |
58 | /** | |
59 | * the holiday cache | |
60 | */ | |
61 | 76 | private Cache<Set<Holiday>> holidayCache = new Cache<>(); |
62 | /** | |
63 | * Utility for calendar operations | |
64 | */ | |
65 | 76 | protected CalendarUtil calendarUtil = new CalendarUtil(); |
66 | /** | |
67 | * The datasource to get the holiday data from. | |
68 | */ | |
69 | private ConfigurationDataSource configurationDataSource; | |
70 | /** | |
71 | * the manager parameter | |
72 | */ | |
73 | private ManagerParameter managerParameter; | |
74 | ||
75 | /** | |
76 | * Creates a HolidayManager instance for the default locale country using | |
77 | * the configured properties from the configuration file. | |
78 | * @return a eventually cached HolidayManager instance | |
79 | */ | |
80 | public static HolidayManager getInstance() { | |
81 | 4 | return getInstance(ManagerParameters.create((String)null, null)); |
82 | } | |
83 | ||
84 | /** | |
85 | * Creates a HolidayManager instance for the default locale country using | |
86 | * the provided properties. | |
87 | * @param properties the overriding configuration properties. | |
88 | * @return a eventually cached HolidayManager instance | |
89 | */ | |
90 | public static HolidayManager getInstance(Properties properties) { | |
91 | 0 | return getInstance(ManagerParameters.create((String)null, properties)); |
92 | } | |
93 | ||
94 | /** | |
95 | * @deprecated Use {@link #getInstance(ManagerParameter)} instead. | |
96 | * @param c the {@link HolidayCalendar} to use for creating a {@link HolidayManager} instance | |
97 | * @return the eventually cached {@link HolidayManager} | |
98 | */ | |
99 | @Deprecated | |
100 | public static HolidayManager getInstance(final HolidayCalendar c) { | |
101 | 70 | return getInstance(ManagerParameters.create(c, null)); |
102 | } | |
103 | ||
104 | /** | |
105 | * @deprecated Use {@link #getInstance(ManagerParameter)} instead. | |
106 | * @param c the {@link HolidayCalendar} to use for creating a {@link HolidayManager} instance | |
107 | * @param properties the configuration overriding {@link Properties} | |
108 | * @return the eventually cached {@link HolidayManager} | |
109 | */ | |
110 | @Deprecated | |
111 | public static HolidayManager getInstance(final HolidayCalendar c, | |
112 | Properties properties) { | |
113 | 0 | return getInstance(ManagerParameters.create(c, properties)); |
114 | } | |
115 | ||
116 | /** | |
117 | * @deprecated Use {@link #getInstance(ManagerParameter)} instead. | |
118 | * @param calendar the calendar to use for creating a {@link HolidayManager} instance | |
119 | * @return the eventually cached {@link HolidayManager} | |
120 | */ | |
121 | @Deprecated | |
122 | public static HolidayManager getInstance(final String calendar) { | |
123 | 1175 | return getInstance(ManagerParameters.create(calendar, null)); |
124 | } | |
125 | ||
126 | /** | |
127 | * @deprecated Use {@link #getInstance(ManagerParameter)} instead. | |
128 | * @param calendar the calendar to use for creating a {@link HolidayManager} instance | |
129 | * @param properties the configuration overriding {@link Properties} | |
130 | * @return the eventually cached {@link HolidayManager} | |
131 | */ | |
132 | @Deprecated | |
133 | public static HolidayManager getInstance(final String calendar, | |
134 | Properties properties) { | |
135 | 0 | return getInstance(ManagerParameters.create(calendar, properties)); |
136 | } | |
137 | ||
138 | /** | |
139 | * Creates and returns a {@link HolidayManager} for the provided | |
140 | * {@link ManagerParameters} | |
141 | * | |
142 | * @param parameter | |
143 | * the {@link ManagerParameters} to create the manager with | |
144 | * @return the {@link HolidayManager} instance | |
145 | */ | |
146 | public static HolidayManager getInstance(ManagerParameter parameter) { | |
147 | 1262 | return createManager(parameter); |
148 | } | |
149 | ||
150 | /** | |
151 | * Creates a new <code>HolidayManager</code> instance for the country and | |
152 | * puts it to the manager cache. | |
153 | * | |
154 | * @param parameter the parameter will be merged into the current configuration | |
155 | * @return created or cached holiday manager | |
156 | */ | |
157 | private static HolidayManager createManager(final ManagerParameter parameter) { | |
158 | 1262 | if (LOG.isLoggable(Level.FINER)) { |
159 | 0 | LOG.finer("Creating HolidayManager for calendar '" + parameter |
160 | 0 | + "'. Caching enabled: " + isManagerCachingEnabled()); |
161 | } | |
162 | 1256 | CONFIGURATION_MANAGER_PROVIDER.mergeConfigurationProperties(parameter); |
163 | 1262 | final String managerImplClassName = readManagerImplClassName(parameter); |
164 | 1261 | HolidayManagerValueHandler holidayManagerValueHandler = new HolidayManagerValueHandler( |
165 | parameter, managerImplClassName); | |
166 | 1260 | if(isManagerCachingEnabled()){ |
167 | 1259 | return HOLIDAY_MANAGER_CACHE.get(holidayManagerValueHandler); |
168 | }else{ | |
169 | 0 | return holidayManagerValueHandler.createValue(); |
170 | } | |
171 | } | |
172 | ||
173 | ||
174 | /** | |
175 | * Reads the managers implementation class from the properties config file. | |
176 | * @param parameter the parameter to read the manager implementation class from | |
177 | * @return the manager implementation class name | |
178 | */ | |
179 | private static String readManagerImplClassName(ManagerParameter parameter) { | |
180 | 1262 | String className = parameter.getManangerImplClassName(); |
181 | 1260 | if (className == null) { |
182 | 0 | throw new IllegalStateException("Missing configuration '" |
183 | + ManagerParameter.MANAGER_IMPL_CLASS_PREFIX | |
184 | + "'. Cannot create manager."); | |
185 | } | |
186 | 1260 | return className; |
187 | } | |
188 | ||
189 | /** | |
190 | * If true, instantiated managers will be cached. If false every call to | |
191 | * getInstance will create new manager. True by default. | |
192 | * | |
193 | * @param managerCachingEnabled | |
194 | * the managerCachingEnabled to set | |
195 | */ | |
196 | public static void setManagerCachingEnabled(boolean managerCachingEnabled) { | |
197 | 0 | CACHING_ENABLED = managerCachingEnabled; |
198 | 0 | } |
199 | ||
200 | /** | |
201 | * <p> | |
202 | * isManagerCachingEnabled. | |
203 | * </p> | |
204 | * | |
205 | * @return the CACHING_ENABLED | |
206 | */ | |
207 | public static boolean isManagerCachingEnabled() { | |
208 | 1260 | return CACHING_ENABLED; |
209 | } | |
210 | ||
211 | /** | |
212 | * Clears the manager cache from all cached manager instances. | |
213 | */ | |
214 | public static void clearManagerCache() { | |
215 | 0 | synchronized (HOLIDAY_MANAGER_CACHE) { |
216 | 0 | HOLIDAY_MANAGER_CACHE.clear(); |
217 | 0 | } |
218 | 0 | } |
219 | ||
220 | /** | |
221 | * Calls isHoliday with JODA time object. | |
222 | * | |
223 | * @param c | |
224 | * a {@link java.util.Calendar} object. | |
225 | * @param args | |
226 | * a {@link java.lang.String} object. | |
227 | * @return a boolean. | |
228 | */ | |
229 | public boolean isHoliday(final Calendar c, final String... args) { | |
230 | 2 | return isHoliday(calendarUtil.create(c), args); |
231 | } | |
232 | ||
233 | /** | |
234 | * Show if the requested date is a holiday. | |
235 | * | |
236 | * @param c | |
237 | * The potential holiday. | |
238 | * @param args | |
239 | * Hierarchy to request the holidays for. i.e. args = {'ny'} -> | |
240 | * New York holidays | |
241 | * @return is a holiday in the state/region | |
242 | */ | |
243 | public boolean isHoliday(final LocalDate c, final String... args) { | |
244 | 1097 | final StringBuilder keyBuilder = new StringBuilder(); |
245 | 1098 | keyBuilder.append(c.getYear()); |
246 | 1097 | for (String arg : args) { |
247 | 0 | keyBuilder.append("_"); |
248 | 0 | keyBuilder.append(arg); |
249 | } | |
250 | 1098 | Set<Holiday> holidays = holidayCache.get(new ValueHandler<Set<Holiday>>() { |
251 | @Override | |
252 | public String getKey() { | |
253 | 1095 | return keyBuilder.toString(); |
254 | } | |
255 | @Override | |
256 | public Set<Holiday> createValue() { | |
257 | 3 | return getHolidays(c.getYear(), args); |
258 | } | |
259 | }); | |
260 | 1098 | return calendarUtil.contains(holidays,c); |
261 | } | |
262 | ||
263 | /** | |
264 | * Returns a set of all currently supported calendar codes. | |
265 | * | |
266 | * @return Set of supported calendar codes. | |
267 | */ | |
268 | public static Set<String> getSupportedCalendarCodes() { | |
269 | 1 | Set<String> supportedCalendars = new HashSet<>(); |
270 | 65 | for (HolidayCalendar c : HolidayCalendar.values()) { |
271 | 64 | supportedCalendars.add(c.getId()); |
272 | } | |
273 | 1 | return supportedCalendars; |
274 | } | |
275 | ||
276 | /** | |
277 | * Sets the configuration datasource with this holiday manager. | |
278 | * | |
279 | * @param configurationDataSource | |
280 | * the {@link ConfigurationDataSource} to use. | |
281 | */ | |
282 | public void setConfigurationDataSource( | |
283 | ConfigurationDataSource configurationDataSource) { | |
284 | 76 | this.configurationDataSource = configurationDataSource; |
285 | 76 | } |
286 | ||
287 | /** | |
288 | * Returns the {@link ConfigurationDataSource} to be used to retrieve | |
289 | * holiday data. | |
290 | * | |
291 | * @return the {@link ConfigurationDataSource} to use. | |
292 | */ | |
293 | public ConfigurationDataSource getConfigurationDataSource() { | |
294 | 76 | return configurationDataSource; |
295 | } | |
296 | ||
297 | public ManagerParameter getManagerParameter() { | |
298 | 123 | return managerParameter; |
299 | } | |
300 | ||
301 | /** | |
302 | * Initializes the implementing manager for the provided calendar. | |
303 | * | |
304 | * @param parameters | |
305 | * i.e. us, uk, de | |
306 | */ | |
307 | public void init(ManagerParameter parameters) { | |
308 | 76 | this.managerParameter = parameters; |
309 | 76 | this.doInit(); |
310 | 74 | } |
311 | ||
312 | abstract public void doInit(); | |
313 | ||
314 | /** | |
315 | * Returns the holidays for the requested year and hierarchy structure. | |
316 | * | |
317 | * @param year | |
318 | * i.e. 2010 | |
319 | * @param args | |
320 | * i.e. args = {'ny'}. returns US/New York holidays. No args -> | |
321 | * holidays common to whole country | |
322 | * @return the list of holidays for the requested year | |
323 | */ | |
324 | abstract public Set<Holiday> getHolidays(int year, String... args); | |
325 | ||
326 | /** | |
327 | * Returns the holidays for the requested interval and hierarchy structure. | |
328 | * | |
329 | * @param startDateInclusive | |
330 | * the start date of the interval in which holidays lie, inclusive | |
331 | * @param endDateInclusive | |
332 | * the end date of the interval in which holidays lie, inclusive | |
333 | * @param args | |
334 | * a {@link java.lang.String} object. | |
335 | * @return list of holidays within the interval | |
336 | */ | |
337 | abstract public Set<Holiday> getHolidays(LocalDate startDateInclusive, | |
338 | LocalDate endDateInclusive, String... args); | |
339 | ||
340 | /** | |
341 | * Returns the configured hierarchy structure for the specific manager. This | |
342 | * hierarchy shows how the configured holidays are structured and can be | |
343 | * retrieved. | |
344 | * | |
345 | * @return Current calendars hierarchy | |
346 | */ | |
347 | abstract public CalendarHierarchy getCalendarHierarchy(); | |
348 | ||
349 | } |