Skip to content

Commit

Permalink
Merge pull request #7 from umjammer/0.4.2
Browse files Browse the repository at this point in the history
0.4.2
  • Loading branch information
umjammer authored Oct 22, 2022
2 parents 8a69669 + dd90f9f commit 4545a4a
Show file tree
Hide file tree
Showing 12 changed files with 94 additions and 58 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ on:
pull_request:
# The branches below must be a subset of the branches above
branches: [ master ]
schedule:
- cron: '20 7 * * 0'
#schedule:
# - cron: '20 7 * * 0'

jobs:
analyze:
Expand Down
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
[![Release](https://jitpack.io/v/umjammer/vavi-commons-sandbox.svg)](https://jitpack.io/#umjammer/vavi-commons-sandbox)
[![Java CI with Maven](https://github.com/umjammer/vavi-commons-sandbox/actions/workflows/maven.yml/badge.svg)](https://github.com/umjammer/vavi-commons-sandbox/actions/workflows/maven.yml)
[![CodeQL](https://github.com/umjammer/vavi-commons-sandbox/actions/workflows/codeql.yml/badge.svg)](https://github.com/umjammer/vavi-commons-sandbox/actions/workflows/codeql.yml)
![Java](https://img.shields.io/badge/Java-8-b07219)

# vavi-commons-sandbox

Expand All @@ -25,7 +28,7 @@ to be deprecated

### 🔧 Utility

* Japanese Holiday
* Holiday Provider
* iCal version
* google calendar cloud api version
* calculated version
Expand All @@ -44,6 +47,8 @@ to be deprecated

### 🔧 Dynamic Morphism

wip

### TODO

* regex find group iteration
21 changes: 14 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@

<groupId>vavi</groupId>
<artifactId>vavi-commons-sandbox</artifactId>
<version>0.4.1</version>
<version>0.4.2</version>

<name>Vavi Commons Sandbox API</name>
<organization>
<name></name>
<name>vavi</name>
</organization>
<url>https://github.com/umjammer/vavi-commons-sandbox</url>
<description>
Expand Down Expand Up @@ -81,7 +81,9 @@ TODO
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
Expand Down Expand Up @@ -159,7 +161,7 @@ TODO
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>5.8.1</version>
<version>5.9.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
Expand All @@ -168,9 +170,9 @@ TODO

<dependencies>
<dependency>
<groupId>com.github.umjammer</groupId>
<groupId>com.github.umjammer</groupId> <!-- com.github.umjammer -->
<artifactId>vavi-commons</artifactId>
<version>1.1.6</version>
<version>1.1.7</version>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
Expand All @@ -180,7 +182,7 @@ TODO
<dependency>
<groupId>org.apache.ant</groupId>
<artifactId>ant</artifactId>
<version>1.10.11</version>
<version>1.10.12</version>
</dependency>
<dependency>
<groupId>com.github.umjammer</groupId>
Expand Down Expand Up @@ -209,6 +211,11 @@ TODO
<artifactId>junit-platform-commons</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.github.umjammer.vavi-net-auth</groupId>
Expand All @@ -219,7 +226,7 @@ TODO
<dependency>
<groupId>com.google.apis</groupId>
<artifactId>google-api-services-calendar</artifactId>
<version>v3-rev409-1.25.0</version>
<version>v3-rev20220715-2.0.0</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
23 changes: 0 additions & 23 deletions src/main/java/vavi/util/Locales.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@


/**
* CalclatedHolidaysJaProvider.
* CalculatedHolidaysJaProvider.
*
* @author <a href="mailto:umjammer@gmail.com">Naohide Sano</a> (nsano)
* @version 0.00 021119 nsano port <br>
*/
@Locales(countries = "Japan", languages = "Japanese")
public class CalculatedHolidaysJaProvider implements HolidaysProvider {

/** this cause Y10K problem lol */
/** this causes the Y10K problem lol */
private static final int MoY = 9999;

enum Holidays {
Expand Down Expand Up @@ -101,14 +101,14 @@ private static int[] equinoxes(int year) {
}

@Override
public List<Holyday> holidays(int year) {
List<Holyday> holydays = new ArrayList<>();
public List<Holiday> holidays(int year) {
List<Holiday> holydays = new ArrayList<>();
Arrays.stream(Holidays.values()).forEach(e -> {
LocalDate date = e.getDate(year);
if (date != null) {
holydays.add(new Holyday(date, e.summary));
holydays.add(new Holiday(date, e.summary));
if (year >= 1973 && date.getDayOfWeek() == DayOfWeek.SUNDAY) {
holydays.add(new Holyday(date.withDayOfMonth(date.getDayOfMonth() + 1), "振替休日"));
holydays.add(new Holiday(date.withDayOfMonth(date.getDayOfMonth() + 1), "振替休日"));
}
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,16 @@
* @author <a href="mailto:umjammer@gmail.com">Naohide Sano</a> (umjammer)
* @version 0.00 2022/02/22 umjammer initial version <br>
*/
@Locales(countries = "Japan", languages = "Japanese, English")
@Locales(countries = "Japan", languages = {"Japanese", "English"})
public class GoogleICalHolidaysJaProvider implements HolidaysProvider {

private static final String url_jp = "https://www.google.com/calendar/ical/ja.japanese%23holiday%40group.v.calendar.google.com/public/basic.ics";
private static final String url_ja = "https://www.google.com/calendar/ical/ja.japanese%23holiday%40group.v.calendar.google.com/public/basic.ics";
private static final String url_en = "https://www.google.com/calendar/ical/japanese%40holiday.calendar.google.com/public/basic.ics";

@Override
public List<Holyday> holidays(int year) {
public List<Holiday> holidays(int year) {
try {
String url = Locale.getDefault().getLanguage().equals(Locale.JAPAN.getLanguage()) ? url_jp : url_en;
String url = Locale.getDefault().getLanguage().equals(Locale.JAPAN.getLanguage()) ? url_ja : url_en;
Debug.println("country: " + Locale.getDefault().getCountry());
String userDefinedUrl = System.getProperty("vavix.util.holiday.ICalProvider.url");
if (userDefinedUrl != null) {
Expand All @@ -54,7 +54,7 @@ public List<Holyday> holidays(int year) {

return calendar.events.stream()
.filter(e -> e.getDate().getYear() == year)
.map(e -> new Holyday(e.getDate(), e.summary))
.map(e -> new Holiday(e.getDate(), e.summary))
.sorted()
.collect(Collectors.toList());
} catch (IOException e) {
Expand Down
41 changes: 35 additions & 6 deletions src/main/java/vavi/util/holiday/HolidaysProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@
package vavi.util.holiday;

import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.NoSuchElementException;
import java.util.ServiceLoader;

import vavi.util.Locales;


/**
Expand All @@ -19,10 +25,10 @@
public interface HolidaysProvider {

/** common */
class Holyday implements Comparable<Holyday> {
class Holiday implements Comparable<Holiday> {
LocalDate date;
String desc;
public Holyday(LocalDate date, String desc) {
public Holiday(LocalDate date, String desc) {
this.date = date;
this.desc = desc;
}
Expand All @@ -31,17 +37,40 @@ public String toString() {
return date + " " + desc;
}
@Override
public int compareTo(Holyday o) {
public int compareTo(Holiday o) {
return this.date.compareTo(o.date);
}
}

/** sorted */
List<Holyday> holidays(int year);
List<Holiday> holidays(int year);

/** factory */
/**
* factory
* <p>
* 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();
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
vavi.util.holiday.CalculatedHolidaysJaProvider
vavi.util.holiday.GoogleICalHolidaysJaProvider
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ class CalculatedHolidaysJaProviderTest {
@Test
void test() {
CalculatedHolidaysJaProvider provider = new CalculatedHolidaysJaProvider();
List<HolidaysProvider.Holyday> holidays = provider.holidays(2022);
List<HolidaysProvider.Holiday> 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());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public class GoogleCalendarHolidaysJaProvider implements HolidaysProvider {
}

@Override
public List<Holyday> holidays(int year) {
public List<Holiday> holidays(int year) {
try {
Events events = service.events().list(calendarId)
.setTimeMin(new DateTime(String.format("%04d-01-01T00:00:00.000+09:00", year)))
Expand All @@ -70,7 +70,7 @@ public List<Holyday> 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);
}
Expand Down
23 changes: 19 additions & 4 deletions src/test/java/vavi/util/holiday/HolidaysProviderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;


/**
Expand Down Expand Up @@ -47,19 +52,29 @@ class HolidaysProviderTest {
void test() {
Debug.println("-------- Google Calendar ICal --------");
HolidaysProvider provider = new GoogleICalHolidaysJaProvider();
List<HolidaysProvider.Holyday> holidays = provider.holidays(2022);
List<HolidaysProvider.Holiday> 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
@DisabledIfEnvironmentVariable(named = "GITHUB_WORKFLOW", matches = ".*")
void test2() {
Debug.println("-------- Google Calendar API --------");
HolidaysProvider provider = new GoogleCalendarHolidaysJaProvider();
List<HolidaysProvider.Holyday> holidays = provider.holidays(2022);
List<HolidaysProvider.Holiday> 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);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
vavi.util.holiday.GoogleCalendarHolidaysJaProvider

0 comments on commit 4545a4a

Please sign in to comment.