holidays(int year);
- /** factory */
+ /**
+ * factory
+ *
+ * TODO use ServiceProvider, consider @Locales
+ */
static HolidaysProvider defaultProvider() {
- return new GoogleICalHolidaysJaProvider();
+ for (HolidaysProvider p : ServiceLoader.load(HolidaysProvider.class)) {
+ Locales a = p.getClass().getAnnotation(Locales.class);
+ if (Arrays.asList(a.countries()).contains(Locale.getDefault().getCountry()) &&
+ Arrays.asList(a.languages()).contains(Locale.getDefault().getCountry())) {
+ return p;
+ }
+ }
+ for (HolidaysProvider p : ServiceLoader.load(HolidaysProvider.class)) {
+ Locales a = p.getClass().getAnnotation(Locales.class);
+ if (Arrays.asList(a.countries()).contains(Locale.getDefault().getCountry())) {
+ return p;
+ }
+ }
+ for (HolidaysProvider p : ServiceLoader.load(HolidaysProvider.class)) {
+ Locales a = p.getClass().getAnnotation(Locales.class);
+ if (Arrays.asList(a.languages()).contains(Locale.getDefault().getCountry())) {
+ return p;
+ }
+ }
+ return ServiceLoader.load(HolidaysProvider.class).iterator().next();
}
}
diff --git a/src/main/resources/META-INF/services/vavi.util.holiday.HolidaysProvider b/src/main/resources/META-INF/services/vavi.util.holiday.HolidaysProvider
new file mode 100644
index 0000000..43f62df
--- /dev/null
+++ b/src/main/resources/META-INF/services/vavi.util.holiday.HolidaysProvider
@@ -0,0 +1,2 @@
+vavi.util.holiday.CalculatedHolidaysJaProvider
+vavi.util.holiday.GoogleICalHolidaysJaProvider
\ No newline at end of file
diff --git a/src/test/java/vavi/util/holiday/CalculatedHolidaysJaProviderTest.java b/src/test/java/vavi/util/holiday/CalculatedHolidaysJaProviderTest.java
index cb8f65e..20e007a 100644
--- a/src/test/java/vavi/util/holiday/CalculatedHolidaysJaProviderTest.java
+++ b/src/test/java/vavi/util/holiday/CalculatedHolidaysJaProviderTest.java
@@ -27,11 +27,11 @@ class CalculatedHolidaysJaProviderTest {
@Test
void test() {
CalculatedHolidaysJaProvider provider = new CalculatedHolidaysJaProvider();
- List holidays = provider.holidays(2022);
+ List holidays = provider.holidays(2022);
Debug.println("holidays: " + holidays.size());
assertNotEquals(0, holidays.size());
holidays.forEach(System.err::println);
- assertArrayEquals(HolidaysProviderTest.expected, holidays.stream().map(HolidaysProvider.Holyday::toString).toArray());
+ assertArrayEquals(HolidaysProviderTest.expected, holidays.stream().map(HolidaysProvider.Holiday::toString).toArray());
}
}
diff --git a/src/test/java/vavi/util/holiday/GoogleCalendarHolidaysJaProvider.java b/src/test/java/vavi/util/holiday/GoogleCalendarHolidaysJaProvider.java
index fc9642c..e1146ce 100644
--- a/src/test/java/vavi/util/holiday/GoogleCalendarHolidaysJaProvider.java
+++ b/src/test/java/vavi/util/holiday/GoogleCalendarHolidaysJaProvider.java
@@ -60,7 +60,7 @@ public class GoogleCalendarHolidaysJaProvider implements HolidaysProvider {
}
@Override
- public List holidays(int year) {
+ public List holidays(int year) {
try {
Events events = service.events().list(calendarId)
.setTimeMin(new DateTime(String.format("%04d-01-01T00:00:00.000+09:00", year)))
@@ -70,7 +70,7 @@ public List holidays(int year) {
.execute();
Debug.println(Level.FINE, "items: " + events.getItems().size());
//events.getItems().forEach(System.err::println);
- return events.getItems().stream().map(e -> new Holyday(toLocalDate(e.getStart().getDate()), e.getSummary())).collect(Collectors.toList());
+ return events.getItems().stream().map(e -> new Holiday(toLocalDate(e.getStart().getDate()), e.getSummary())).collect(Collectors.toList());
} catch (IOException e) {
throw new UncheckedIOException(e);
}
diff --git a/src/test/java/vavi/util/holiday/HolidaysProviderTest.java b/src/test/java/vavi/util/holiday/HolidaysProviderTest.java
index c66a451..55a4cc5 100644
--- a/src/test/java/vavi/util/holiday/HolidaysProviderTest.java
+++ b/src/test/java/vavi/util/holiday/HolidaysProviderTest.java
@@ -8,12 +8,17 @@
import java.util.List;
+import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.DisabledIfEnvironmentVariable;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
import vavi.util.Debug;
+import vavi.util.Locales;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
/**
@@ -47,9 +52,9 @@ class HolidaysProviderTest {
void test() {
Debug.println("-------- Google Calendar ICal --------");
HolidaysProvider provider = new GoogleICalHolidaysJaProvider();
- List holidays = provider.holidays(2022);
+ List holidays = provider.holidays(2022);
holidays.stream().sorted().forEach(System.err::println);
- assertArrayEquals(expected, holidays.stream().map(HolidaysProvider.Holyday::toString).toArray());
+ assertArrayEquals(expected, holidays.stream().map(HolidaysProvider.Holiday::toString).toArray());
}
@Test
@@ -57,9 +62,19 @@ void test() {
void test2() {
Debug.println("-------- Google Calendar API --------");
HolidaysProvider provider = new GoogleCalendarHolidaysJaProvider();
- List holidays = provider.holidays(2022);
+ List holidays = provider.holidays(2022);
holidays.stream().sorted().forEach(System.err::println);
- assertArrayEquals(expected, holidays.stream().map(HolidaysProvider.Holyday::toString).toArray());
+ assertArrayEquals(expected, holidays.stream().map(HolidaysProvider.Holiday::toString).toArray());
+ }
+
+ /** @see "https://blog1.mammb.com/entry/2017/07/05/223914" */
+ @Disabled("wip")
+ @ParameterizedTest
+ @ValueSource(strings = {"ja", "en"})
+ void test3(String lang) throws Exception {
+ System.setProperty("user.language", lang);
+ HolidaysProvider p = HolidaysProvider.defaultProvider();
+ assertInstanceOf(GoogleICalHolidaysJaProvider.class, p);
}
}
diff --git a/src/test/resources/META-INF/services/vavi.util.holiday.HolidaysProvider b/src/test/resources/META-INF/services/vavi.util.holiday.HolidaysProvider
new file mode 100644
index 0000000..d8acf6c
--- /dev/null
+++ b/src/test/resources/META-INF/services/vavi.util.holiday.HolidaysProvider
@@ -0,0 +1 @@
+vavi.util.holiday.GoogleCalendarHolidaysJaProvider
\ No newline at end of file