Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Immutable calendar hierarchy #719

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package de.focus_shift.jollyday.tests;

import de.focus_shift.jollyday.core.HolidayCalendar;
import org.junit.jupiter.api.Test;

import java.util.Arrays;

import static de.focus_shift.jollyday.core.HolidayManager.getInstance;
import static de.focus_shift.jollyday.core.ManagerParameters.create;

class CalendarHierarchyTest {

@Test
void name() {
Arrays.stream(HolidayCalendar.values()).forEach(holidayCalendar -> {
System.out.println(getInstance(create(holidayCalendar)).getCalendarHierarchy());
});
}
}
Loading