Skip to content

Commit

Permalink
Immutable calendar hierarchy and toString
Browse files Browse the repository at this point in the history
  • Loading branch information
derTobsch committed Jan 4, 2025
1 parent bf34c98 commit feff53b
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,33 @@
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;

/**
* Bean class for describing the configuration hierarchy.
*/
public class CalendarHierarchy {
public final class CalendarHierarchy {

private final String id;
private Map<String, CalendarHierarchy> children = new HashMap<>();
private final CalendarHierarchy parent;
private String fallbackDescription;
private final String fallbackDescription;
private final Map<String, CalendarHierarchy> 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;
}

/**
Expand Down Expand Up @@ -60,63 +63,50 @@ public String getDescription(Locale locale) {
}

/**
* Recursively returns the properties key to retrieve the description from
* the localized resource bundle.
* <p>
* Getter for the field <code>children</code>.
* </p>
*
* @return
* @return the children
*/
private String getPropertiesKey() {
if (parent != null) {
return parent.getPropertiesKey() + "." + getId();
}
return getId();
public Map<String, CalendarHierarchy> getChildren() {
return children;
}

/**
* {@inheritDoc}
* <p>
* 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);
}

/**
* <p>
* Setter for the field <code>children</code>.
* </p>
*
* @param children the children to set
*/
public void setChildren(final Map<String, CalendarHierarchy> children) {
this.children = children;
}

/**
* <p>
* Getter for the field <code>children</code>.
* </p>
*
* @return the children
*/
public Map<String, CalendarHierarchy> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public Set<Holiday> getHolidays(final LocalDate startDateInclusive, final LocalD
*/
@Override
public CalendarHierarchy getCalendarHierarchy() {
return createConfigurationHierarchy(configuration, null);
return createConfigurationHierarchy(configuration, null, null);
}

/**
Expand Down Expand Up @@ -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;
}
}

0 comments on commit feff53b

Please sign in to comment.