From feff53b92d11433ec24290936b682585297d680b Mon Sep 17 00:00:00 2001 From: Tobias Schneider Date: Sat, 4 Jan 2025 17:24:53 +0100 Subject: [PATCH] Immutable calendar hierarchy and toString --- .../jollyday/core/CalendarHierarchy.java | 84 ++++++++----------- .../core/impl/DefaultHolidayManager.java | 20 ++--- 2 files changed, 47 insertions(+), 57 deletions(-) diff --git a/jollyday-core/src/main/java/de/focus_shift/jollyday/core/CalendarHierarchy.java b/jollyday-core/src/main/java/de/focus_shift/jollyday/core/CalendarHierarchy.java index 1b75d32fc..a385e7d15 100644 --- a/jollyday-core/src/main/java/de/focus_shift/jollyday/core/CalendarHierarchy.java +++ b/jollyday-core/src/main/java/de/focus_shift/jollyday/core/CalendarHierarchy.java @@ -3,6 +3,8 @@ import java.util.HashMap; import java.util.Locale; import java.util.Map; +import java.util.Objects; +import java.util.StringJoiner; import static de.focus_shift.jollyday.core.util.ResourceUtil.UNDEFINED; import static de.focus_shift.jollyday.core.util.ResourceUtil.getCountryDescription; @@ -10,23 +12,24 @@ /** * Bean class for describing the configuration hierarchy. */ -public class CalendarHierarchy { +public final class CalendarHierarchy { private final String id; - private Map children = new HashMap<>(); private final CalendarHierarchy parent; - private String fallbackDescription; + private final String fallbackDescription; + private final Map children = new HashMap<>(); /** * Constructor which takes a eventually existing parent hierarchy node and * the ID of this hierarchy. * + * @param id a {@link String} object. * @param parent a {@link CalendarHierarchy} object. - * @param id a {@link java.lang.String} object. */ - public CalendarHierarchy(final CalendarHierarchy parent, final String id) { - this.parent = parent; + public CalendarHierarchy(final String id, final String fallbackDescription, final CalendarHierarchy parent) { this.id = id; + this.parent = parent; + this.fallbackDescription = fallbackDescription; } /** @@ -60,63 +63,50 @@ public String getDescription(Locale locale) { } /** - * Recursively returns the properties key to retrieve the description from - * the localized resource bundle. + *

+ * Getter for the field children. + *

* - * @return + * @return the children */ - private String getPropertiesKey() { - if (parent != null) { - return parent.getPropertiesKey() + "." + getId(); - } - return getId(); + public Map getChildren() { + return children; } - /** - * {@inheritDoc} - *

- * Compares Hierarchies by id. - */ @Override - public boolean equals(final Object obj) { - if (obj instanceof CalendarHierarchy) { - return ((CalendarHierarchy) obj).getId().equals(this.getId()); + public boolean equals(Object obj) { + if (!(obj instanceof CalendarHierarchy)) { + return false; } - return super.equals(obj); + + final CalendarHierarchy that = (CalendarHierarchy) obj; + return Objects.equals(id, that.id); } @Override public int hashCode() { - return getId().hashCode(); + return Objects.hashCode(id); } - /** - *

- * Setter for the field children. - *

- * - * @param children the children to set - */ - public void setChildren(final Map children) { - this.children = children; - } - - /** - *

- * Getter for the field children. - *

- * - * @return the children - */ - public Map getChildren() { - return children; + @Override + public String toString() { + return new StringJoiner(", ", CalendarHierarchy.class.getSimpleName() + "[", "]") + .add("id='" + id + "'") + .add("parentId='" + (parent != null ? parent.getId() : "") + "'") + .add("description='" + getDescription() + "'") + .add("children=" + children) + .toString(); } /** - * @param description the fallback description + * Recursively returns the properties key to retrieve the description from + * the localized resource bundle. */ - public void setFallbackDescription(final String description) { - this.fallbackDescription = description; + private String getPropertiesKey() { + if (parent != null) { + return parent.getPropertiesKey() + "." + getId(); + } + return getId(); } private String receiveFallbackDescription(final String description) { diff --git a/jollyday-core/src/main/java/de/focus_shift/jollyday/core/impl/DefaultHolidayManager.java b/jollyday-core/src/main/java/de/focus_shift/jollyday/core/impl/DefaultHolidayManager.java index 217c07717..8c23ca05b 100644 --- a/jollyday-core/src/main/java/de/focus_shift/jollyday/core/impl/DefaultHolidayManager.java +++ b/jollyday-core/src/main/java/de/focus_shift/jollyday/core/impl/DefaultHolidayManager.java @@ -164,7 +164,7 @@ public Set getHolidays(final LocalDate startDateInclusive, final LocalD */ @Override public CalendarHierarchy getCalendarHierarchy() { - return createConfigurationHierarchy(configuration, null); + return createConfigurationHierarchy(configuration, null, null); } /** @@ -329,15 +329,15 @@ protected static void validateConfigurationHierarchy(final Configuration configu * @param calendarHierarchy the calendars hierarchy * @return configuration hierarchy */ - private static CalendarHierarchy createConfigurationHierarchy(final Configuration configuration, final CalendarHierarchy calendarHierarchy) { - final CalendarHierarchy hierarchy = new CalendarHierarchy(calendarHierarchy, configuration.hierarchy()); - hierarchy.setFallbackDescription(configuration.description()); - configuration.subConfigurations() - .forEach(subConfiguration -> { - final CalendarHierarchy subHierarchy = createConfigurationHierarchy(subConfiguration, hierarchy); - hierarchy.getChildren().put(subHierarchy.getId(), subHierarchy); - } - ); + private static CalendarHierarchy createConfigurationHierarchy(final Configuration configuration, final CalendarHierarchy calendarHierarchy, final String fallbackDescription) { + final String description = configuration.description() != null ? configuration.description() : fallbackDescription; + final CalendarHierarchy hierarchy = new CalendarHierarchy(configuration.hierarchy(), description, calendarHierarchy); + + configuration.subConfigurations().forEach(subConfiguration -> { + final CalendarHierarchy subHierarchy = createConfigurationHierarchy(subConfiguration, hierarchy, subConfiguration.description()); + hierarchy.getChildren().put(subHierarchy.getId(), subHierarchy); + }); + return hierarchy; } }