From ef5abfdd22a4b45d4f0e60e79f201a5c05bd5670 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20L=27hopital?= Date: Tue, 10 Dec 2024 10:21:22 +0100 Subject: [PATCH 1/2] Adds possibility to disable user defined holiday description files caching. This in interesting especially when defining/debugging these files. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Gaël L'hopital --- .../internal/EphemerisManagerImpl.java | 55 +++++++++++-------- .../resources/OH-INF/config/ephemeris.xml | 6 ++ 2 files changed, 37 insertions(+), 24 deletions(-) diff --git a/bundles/org.openhab.core.ephemeris/src/main/java/org/openhab/core/ephemeris/internal/EphemerisManagerImpl.java b/bundles/org.openhab.core.ephemeris/src/main/java/org/openhab/core/ephemeris/internal/EphemerisManagerImpl.java index cf94e9363ef..8febf2367ac 100644 --- a/bundles/org.openhab.core.ephemeris/src/main/java/org/openhab/core/ephemeris/internal/EphemerisManagerImpl.java +++ b/bundles/org.openhab.core.ephemeris/src/main/java/org/openhab/core/ephemeris/internal/EphemerisManagerImpl.java @@ -81,6 +81,7 @@ public class EphemerisManagerImpl implements EphemerisManager, ConfigOptionProvi public static final String CONFIG_COUNTRY = "country"; public static final String CONFIG_REGION = "region"; public static final String CONFIG_CITY = "city"; + public static final String CONFIG_CACHE_FILES = "cache"; private static final String RESOURCES_ROOT = "jollyday/"; private static final String JOLLYDAY_COUNTRY_DESCRIPTIONS = RESOURCES_ROOT @@ -88,11 +89,11 @@ public class EphemerisManagerImpl implements EphemerisManager, ConfigOptionProvi private static final String PROPERTY_COUNTRY_DESCRIPTION_PREFIX = "country.description."; private static final String PROPERTY_COUNTRY_DESCRIPTION_DELIMITER = "\\."; - final List countries = new ArrayList<>(); - final Map> regions = new HashMap<>(); - final Map> cities = new HashMap<>(); + private final List countries = new ArrayList<>(); + private final Map> regions = new HashMap<>(); + private final Map> cities = new HashMap<>(); - final Map> daysets = new HashMap<>(); + private final Map> daysets = new HashMap<>(); private final Map holidayManagers = new HashMap<>(); private final List countryParameters = new ArrayList<>(); @@ -101,6 +102,7 @@ public class EphemerisManagerImpl implements EphemerisManager, ConfigOptionProvi private @NonNullByDefault({}) String country; private @Nullable String region; + private boolean cacheFiles = true; @Activate public EphemerisManagerImpl(final @Reference LocaleProvider localeProvider, final BundleContext bundleContext) { @@ -141,8 +143,7 @@ protected void modified(Map config) { if (setNameParts.length > 1) { String setName = setNameParts[1]; Object entry = e.getValue(); - if (entry instanceof String) { - String value = entry.toString(); + if (entry instanceof String value) { while (value.startsWith("[")) { value = value.substring(1); } @@ -166,27 +167,26 @@ protected void modified(Map config) { } }); - Object configValue = config.get(CONFIG_COUNTRY); - if (configValue != null) { - country = configValue.toString().toLowerCase(); + if (config.get(CONFIG_COUNTRY) instanceof String string) { + country = string.toLowerCase(); } else { country = localeProvider.getLocale().getCountry().toLowerCase(); logger.debug("Using system default country '{}' ", country); } - configValue = config.get(CONFIG_REGION); - if (configValue != null) { - String region = configValue.toString().toLowerCase(); + if (config.get(CONFIG_REGION) instanceof String string) { + String region = string.toLowerCase(); countryParameters.add(region); this.region = region; } else { this.region = null; } - configValue = config.get(CONFIG_CITY); - if (configValue != null) { - countryParameters.add(configValue.toString()); + if (config.get(CONFIG_CITY) instanceof String city) { + countryParameters.add(city); } + + cacheFiles = config.get(CONFIG_CACHE_FILES) instanceof String cache ? Boolean.getBoolean(cache) : true; } @Override @@ -228,26 +228,31 @@ private URL getUrl(String filename) throws FileNotFoundException { } catch (MalformedURLException e) { throw new FileNotFoundException(e.getMessage()); } - } else { - throw new FileNotFoundException(filename); } + throw new FileNotFoundException(filename); } private HolidayManager getHolidayManager(Object managerKey) { HolidayManager holidayManager = holidayManagers.get(managerKey); if (holidayManager == null) { final ManagerParameter parameters; + boolean cacheManager = true; if (managerKey instanceof String stringKey) { URL urlOverride = bundle .getResource(RESOURCES_ROOT + CalendarPartManagerParameter.getConfigurationFileName(stringKey)); parameters = urlOverride != null // ? ManagerParameters.create(urlOverride) : ManagerParameters.create(stringKey); + } else if (managerKey instanceof URL url) { + parameters = ManagerParameters.create(url); + cacheManager = cacheFiles; } else { - parameters = ManagerParameters.create((URL) managerKey); + throw new IllegalArgumentException("managerKey has to be either a country either an URL"); } holidayManager = HolidayManager.getInstance(parameters); - holidayManagers.put(managerKey, holidayManager); + if (cacheManager) { + holidayManagers.put(managerKey, holidayManager); + } } return holidayManager; } @@ -279,7 +284,8 @@ private long getDaysUntil(ZonedDateTime from, String searchedHoliday, HolidayMan List sortedHolidays = getHolidays(from, 366, holidayManager); Optional result = sortedHolidays.stream() .filter(holiday -> searchedHoliday.equalsIgnoreCase(holiday.getPropertiesKey())).findFirst(); - return result.map(holiday -> from.toLocalDate().until(holiday.getDate(), ChronoUnit.DAYS)).orElse(-1L); + return result.get() instanceof Holiday holiday ? from.toLocalDate().until(holiday.getDate(), ChronoUnit.DAYS) + : -1L; } private @Nullable String getFirstBankHolidayKey(ZonedDateTime from, int span, HolidayManager holidayManager) { @@ -351,10 +357,11 @@ public boolean isInDayset(String daysetName, ZonedDateTime date) { private void addDayset(String setName, Iterable values) { Set dayset = new HashSet<>(); - for (Object day : values) { - // fix illegal entries by stripping all non A-Z characters - String dayString = day.toString().toUpperCase().replaceAll("[^A-Z]", ""); - dayset.add(DayOfWeek.valueOf(dayString)); + for (Object value : values) { + if (value instanceof String day) { + // fix illegal entries by stripping all non A-Z characters + dayset.add(DayOfWeek.valueOf(day.toUpperCase().replaceAll("[^A-Z]", ""))); + } } daysets.put(setName, dayset); } diff --git a/bundles/org.openhab.core.ephemeris/src/main/resources/OH-INF/config/ephemeris.xml b/bundles/org.openhab.core.ephemeris/src/main/resources/OH-INF/config/ephemeris.xml index 5487579a1ed..d7d3cffa86c 100644 --- a/bundles/org.openhab.core.ephemeris/src/main/resources/OH-INF/config/ephemeris.xml +++ b/bundles/org.openhab.core.ephemeris/src/main/resources/OH-INF/config/ephemeris.xml @@ -23,6 +23,12 @@ Get the holidays for a specific city (e.g. New York City = "nyc"). true + + + User defined configuration files are cached once loaded. When set to false, files will be read for each access. + true + true + From e37b1d75f1deb6c2c6d709d16cd0abf7a9f9edb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20L=27hopital?= Date: Tue, 10 Dec 2024 10:38:10 +0100 Subject: [PATCH 2/2] Applying spotless MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Gaël L'hopital --- .../src/main/resources/OH-INF/config/ephemeris.xml | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/bundles/org.openhab.core.ephemeris/src/main/resources/OH-INF/config/ephemeris.xml b/bundles/org.openhab.core.ephemeris/src/main/resources/OH-INF/config/ephemeris.xml index d7d3cffa86c..30ca1f33fb7 100644 --- a/bundles/org.openhab.core.ephemeris/src/main/resources/OH-INF/config/ephemeris.xml +++ b/bundles/org.openhab.core.ephemeris/src/main/resources/OH-INF/config/ephemeris.xml @@ -23,12 +23,13 @@ Get the holidays for a specific city (e.g. New York City = "nyc"). true - - - User defined configuration files are cached once loaded. When set to false, files will be read for each access. - true - true - + + + User defined configuration files are cached once loaded. When set to false, files will be read for each + access. + true + true +