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

Add moving conditions to islamic holiday #324

Merged
merged 1 commit into from
Sep 22, 2023
Merged
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 @@ -4,6 +4,7 @@
import de.focus_shift.jollyday.core.parser.HolidayParser;
import de.focus_shift.jollyday.core.parser.functions.CalculateRelativeDatesFromChronologyWithinGregorianYear;
import de.focus_shift.jollyday.core.parser.functions.CreateHoliday;
import de.focus_shift.jollyday.core.parser.functions.MoveDateRelative;
import de.focus_shift.jollyday.core.parser.predicates.ValidLimitation;
import de.focus_shift.jollyday.core.spi.Holidays;

Expand All @@ -16,19 +17,18 @@

/**
* This parser calculates gregorian dates for the different islamic holidays.
*
* @author Sven Diedrichsen
* @version $Id: $
*/
public class IslamicHolidayParser implements HolidayParser {

@Override
public List<Holiday> parse(int year, Holidays holidays) {
return holidays.islamicHolidays().stream()
.filter(new ValidLimitation(year))
.flatMap(ih -> {
Stream<LocalDate> islamicHolidays;
switch (ih.type()) {
.flatMap(islamicHoliday -> {

final Stream<LocalDate> islamicHolidays;

switch (islamicHoliday.type()) {
case NEWYEAR:
islamicHolidays = new CalculateRelativeDatesFromChronologyWithinGregorianYear(1, 1, HijrahChronology.INSTANCE, 0).apply(year);
break;
Expand Down Expand Up @@ -75,9 +75,13 @@ public List<Holiday> parse(int year, Holidays holidays) {
islamicHolidays = new CalculateRelativeDatesFromChronologyWithinGregorianYear(9, 1, HijrahChronology.INSTANCE, 0).apply(year);
break;
default:
throw new IllegalArgumentException("Unknown islamic holiday " + ih.type());
throw new IllegalArgumentException("Unknown islamic holiday " + islamicHoliday.type());
}
return islamicHolidays.map(date -> new CreateHoliday(date).apply(ih));

return islamicHolidays
.map(date -> new MoveDateRelative(date).apply(islamicHoliday))
.map(date -> new CreateHoliday(date).apply(islamicHoliday));

})
.collect(toList());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package de.focus_shift.jollyday.core.spi;

public interface IslamicHoliday extends Described, Limited {
public interface IslamicHoliday extends Described, Limited, Movable {
IslamicHolidayType type();
}
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@

<xsd:complexType name="IslamicHoliday">
<xsd:complexContent>
<xsd:extension base="Holiday">
<xsd:extension base="MoveableHoliday">
<xsd:attribute name="type" type="IslamicHolidayType"/>
</xsd:extension>
</xsd:complexContent>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package de.focus_shift.jollyday.core.parser.impl;

import de.focus_shift.jollyday.core.Holiday;
import de.focus_shift.jollyday.core.spi.Holidays;
import de.focus_shift.jollyday.core.spi.IslamicHoliday;
import de.focus_shift.jollyday.core.spi.IslamicHolidayType;
import de.focus_shift.jollyday.core.spi.MovingCondition;
import de.focus_shift.jollyday.core.spi.With;
import de.focus_shift.jollyday.core.spi.YearCycle;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import java.time.DayOfWeek;
import java.util.List;

import static de.focus_shift.jollyday.core.spi.IslamicHolidayType.ARAFAAT;
import static de.focus_shift.jollyday.core.spi.YearCycle.EVERY_YEAR;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
class IslamicHolidayParserTest {

@Mock
private Holidays holidays;
@Mock
private IslamicHoliday islamicHoliday;

@Test
void ensureThatIslamicHolidaysAreMovable() {

final MovingCondition movingCondition = new MovingCondition() {
@Override
public DayOfWeek substitute() {
return DayOfWeek.SUNDAY;
}

@Override
public With with() {
return With.NEXT;
}

@Override
public DayOfWeek weekday() {
return DayOfWeek.MONDAY;
}
};

final IslamicHolidayParser sut = new IslamicHolidayParser();
when(holidays.islamicHolidays()).thenReturn(List.of(islamicHoliday));
when(islamicHoliday.type()).thenReturn(IslamicHolidayType.ID_UL_ADHA_2);
when(islamicHoliday.conditions()).thenReturn(List.of(movingCondition));
when(islamicHoliday.cycle()).thenReturn(EVERY_YEAR);

// ID_UL_ADHA_2 will be on a sunday in 2022
final List<Holiday> calculatedHoliday = sut.parse(2022, holidays);
assertThat(calculatedHoliday.get(0).getDate().getDayOfWeek()).isEqualTo(DayOfWeek.MONDAY);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
import de.focus_shift.jollyday.core.HolidayType;
import de.focus_shift.jollyday.core.spi.IslamicHoliday;
import de.focus_shift.jollyday.core.spi.IslamicHolidayType;
import de.focus_shift.jollyday.core.spi.MovingCondition;
import de.focus_shift.jollyday.core.spi.YearCycle;

import java.time.Year;
import java.util.List;

import static java.util.stream.Collectors.toList;


public class JaxbIslamicHoliday implements IslamicHoliday {
Expand Down Expand Up @@ -53,4 +57,11 @@ public YearCycle cycle() {
? YearCycle.EVERY_YEAR
: YearCycle.valueOf(islamicHoliday.getEvery().name());
}

@Override
public List<MovingCondition> conditions() {
return islamicHoliday.getMovingCondition().stream()
.map(JaxbMovingCondition::new)
.collect(toList());
}
}