Skip to content

Commit

Permalink
Merge pull request #718 from focus-shift/use-default-methods-in-resou…
Browse files Browse the repository at this point in the history
…rceutil

Use methods to receive holiday and country description from Resource …
  • Loading branch information
derTobsch authored Jan 4, 2025
2 parents b4fe326 + 4da5468 commit e2ba34b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package de.focus_shift.jollyday.core;

import de.focus_shift.jollyday.core.util.ResourceUtil;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import static de.focus_shift.jollyday.core.util.ResourceUtil.UNDEFINED;
import static de.focus_shift.jollyday.core.util.ResourceUtil.getCountryDescription;

/**
* Bean class for describing the configuration hierarchy.
*/
Expand Down Expand Up @@ -45,21 +46,17 @@ public String getId() {
* @return the description
*/
public String getDescription() {
return getDescription(Locale.getDefault());
return receiveFallbackDescription(getCountryDescription(getPropertiesKey()));
}

/**
* Returns the hierarchies description text from the resource bundle.
*
* @param l Locale to return the description text for.
* @param locale Locale to return the description text for.
* @return Description text
*/
public String getDescription(Locale l) {
String descr = ResourceUtil.getCountryDescription(l, getPropertiesKey());
if (ResourceUtil.UNDEFINED.equals(descr) && fallbackDescription != null) {
descr = fallbackDescription;
}
return descr;
public String getDescription(Locale locale) {
return receiveFallbackDescription(getCountryDescription(locale, getPropertiesKey()));
}

/**
Expand All @@ -81,7 +78,7 @@ private String getPropertiesKey() {
* Compares Hierarchies by id.
*/
@Override
public boolean equals(Object obj) {
public boolean equals(final Object obj) {
if (obj instanceof CalendarHierarchy) {
return ((CalendarHierarchy) obj).getId().equals(this.getId());
}
Expand Down Expand Up @@ -118,8 +115,14 @@ public Map<String, CalendarHierarchy> getChildren() {
/**
* @param description the fallback description
*/
public void setFallbackDescription(String description) {
public void setFallbackDescription(final String description) {
this.fallbackDescription = description;
}

private String receiveFallbackDescription(final String description) {
if (UNDEFINED.equals(description) && fallbackDescription != null) {
return fallbackDescription;
}
return description;
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package de.focus_shift.jollyday.core;

import de.focus_shift.jollyday.core.util.ResourceUtil;

import java.time.LocalDate;
import java.util.Locale;
import java.util.Objects;
import java.util.Optional;

import static de.focus_shift.jollyday.core.util.ResourceUtil.getHolidayDescription;

/**
* Represents the holiday and contains the actual date and an localized
* description.
Expand Down Expand Up @@ -118,7 +118,7 @@ public String getPropertiesKey() {
* @return Description for this holiday
*/
public String getDescription() {
return ResourceUtil.getHolidayDescription(Locale.getDefault(), getPropertiesKey());
return getHolidayDescription(getPropertiesKey());
}

/**
Expand All @@ -128,7 +128,7 @@ public String getDescription() {
* @return Description for this holiday
*/
public String getDescription(Locale locale) {
return ResourceUtil.getHolidayDescription(locale, getPropertiesKey());
return getHolidayDescription(locale, getPropertiesKey());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@

import static java.util.ResourceBundle.getBundle;

/**
* ResourceUtil class.
*/
public class ResourceUtil {

private ResourceUtil() {
Expand All @@ -21,31 +18,33 @@ private ResourceUtil() {
* Property prefix for country descriptions.
*/
private static final String COUNTRY_PROPERTY_PREFIX = "country.description";
/**
* Property prefix for holiday descriptions.
*/
private static final String HOLIDAY_PROPERTY_PREFIX = "holiday.description";
/**
* The prefix of the country description file.
*/
private static final String COUNTRY_DESCRIPTIONS_FILE_PREFIX = "descriptions.country_descriptions";
/**
* The prefix of the holiday descriptions file.
* Cache for the country descriptions resource bundles.
*/
private static final String HOLIDAY_DESCRIPTIONS_FILE_PREFIX = "descriptions.holiday_descriptions";
private static final Map<Locale, ResourceBundle> COUNTRY_DESCRIPTIONS_CACHE = new ConcurrentHashMap<>();

/**
* Unknown constant will be returned when there is no description
* configured.
* Property prefix for holiday descriptions.
*/
public static final String UNDEFINED = "undefined";
private static final String HOLIDAY_PROPERTY_PREFIX = "holiday.description";
/**
* The prefix of the holiday descriptions file.
*/
private static final String HOLIDAY_DESCRIPTIONS_FILE_PREFIX = "descriptions.holiday_descriptions";
/**
* Cache for the holiday descriptions resource bundles.
*/
private static final Map<Locale, ResourceBundle> HOLIDAY_DESCRIPTION_CACHE = new ConcurrentHashMap<>();

/**
* Cache for the country descriptions resource bundles.
* Unknown constant will be returned when there is no description
* configured.
*/
private static final Map<Locale, ResourceBundle> COUNTRY_DESCRIPTIONS_CACHE = new ConcurrentHashMap<>();
public static final String UNDEFINED = "undefined";

/**
* The description read with the default locale.
Expand Down Expand Up @@ -91,7 +90,7 @@ public static String getCountryDescription(final Locale locale, final String key
if (key != null) {
return getDescription(COUNTRY_PROPERTY_PREFIX + "." + key.toLowerCase(), getCountryDescriptions(locale));
}
return ResourceUtil.UNDEFINED;
return UNDEFINED;
}

/**
Expand Down

0 comments on commit e2ba34b

Please sign in to comment.