Skip to content
This repository was archived by the owner on Mar 4, 2021. It is now read-only.

Commit 0866a9a

Browse files
authored
Merge pull request #266 from AutoScout24/bavarian-holidays
Add possibility to load custom calendar, add bavarian holidays.
2 parents a21ec60 + 5ea5989 commit 0866a9a

File tree

5 files changed

+334
-13
lines changed

5 files changed

+334
-13
lines changed

src/main/java/com/netflix/simianarmy/basic/BasicCalendar.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public class BasicCalendar implements MonkeyCalendar {
5050
private final TimeZone tz;
5151

5252
/** The holidays. */
53-
private final Set<Integer> holidays = new TreeSet<Integer>();
53+
protected final Set<Integer> holidays = new TreeSet<Integer>();
5454

5555
/** The cfg. */
5656
private MonkeyConfiguration cfg;
@@ -83,7 +83,7 @@ public BasicCalendar(int open, int close, TimeZone timezone) {
8383
closeHour = close;
8484
tz = timezone;
8585
}
86-
86+
8787
/**
8888
* Instantiates a new basic calendar.
8989
*
@@ -235,7 +235,7 @@ protected void loadHolidays(int year) {
235235
* the day
236236
* @return the day of the year
237237
*/
238-
private int dayOfYear(int year, int month, int day) {
238+
protected int dayOfYear(int year, int month, int day) {
239239
Calendar holiday = now();
240240
holiday.set(Calendar.YEAR, year);
241241
holiday.set(Calendar.MONTH, month);
@@ -256,7 +256,7 @@ private int dayOfYear(int year, int month, int day) {
256256
* the week in month
257257
* @return the day of the year
258258
*/
259-
private int dayOfYear(int year, int month, int dayOfWeek, int weekInMonth) {
259+
protected int dayOfYear(int year, int month, int dayOfWeek, int weekInMonth) {
260260
Calendar holiday = now();
261261
holiday.set(Calendar.YEAR, year);
262262
holiday.set(Calendar.MONTH, month);
@@ -276,7 +276,7 @@ private int dayOfYear(int year, int month, int dayOfWeek, int weekInMonth) {
276276
* the day
277277
* @return the day of the year adjusted to the closest workday
278278
*/
279-
private int workDayInYear(int year, int month, int day) {
279+
protected int workDayInYear(int year, int month, int day) {
280280
Calendar holiday = now();
281281
holiday.set(Calendar.YEAR, year);
282282
holiday.set(Calendar.MONTH, month);
@@ -308,8 +308,7 @@ public Date getBusinessDay(Date date, int n) {
308308

309309
private boolean isWeekend(Calendar calendar) {
310310
int dow = calendar.get(Calendar.DAY_OF_WEEK);
311-
return dow == Calendar.SATURDAY
312-
|| dow == Calendar.SUNDAY;
311+
return dow == Calendar.SATURDAY || dow == Calendar.SUNDAY;
313312
}
314313

315314
}

src/main/java/com/netflix/simianarmy/basic/BasicSimianArmyContext.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,19 +123,18 @@ protected BasicSimianArmyContext(String... configFiles) {
123123
}
124124

125125
config = new BasicConfiguration(properties);
126-
calendar = new BasicCalendar(config);
127126

128127
account = config.getStr("simianarmy.client.aws.accountKey");
129128
secret = config.getStr("simianarmy.client.aws.secretKey");
130129
accountName = config.getStrOrElse("simianarmy.client.aws.accountName", "Default");
131-
130+
132131
String defaultRegion = "us-east-1";
133132
Region currentRegion = Regions.getCurrentRegion();
134-
133+
135134
if (currentRegion != null) {
136135
defaultRegion = currentRegion.getName();
137136
}
138-
137+
139138
region = config.getStrOrElse("simianarmy.client.aws.region", defaultRegion);
140139
GLOBAL_OWNER_TAGKEY = config.getStrOrElse("simianarmy.tags.owner", "owner");
141140

@@ -165,6 +164,8 @@ protected BasicSimianArmyContext(String... configFiles) {
165164

166165
createClient();
167166

167+
createCalendar();
168+
168169
createScheduler();
169170

170171
createRecorder();
@@ -224,6 +225,17 @@ private void createRecorder() {
224225
}
225226
}
226227

228+
@SuppressWarnings("unchecked")
229+
private void createCalendar() {
230+
@SuppressWarnings("rawtypes")
231+
Class calendarClass = loadClientClass("simianarmy.calendar.class");
232+
if (calendarClass == null || calendarClass.equals(BasicCalendar.class)) {
233+
setCalendar(new BasicCalendar(config));
234+
} else {
235+
setCalendar((MonkeyCalendar) factory(calendarClass));
236+
}
237+
}
238+
227239
/**
228240
* Create the specific client with region taken from properties.
229241
* Override to provide your own client.
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
/*
2+
*
3+
* Copyright 2012 Netflix, Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
package com.netflix.simianarmy.basic.calendars;
19+
20+
import com.netflix.simianarmy.MonkeyConfiguration;
21+
import com.netflix.simianarmy.basic.BasicCalendar;
22+
23+
import java.util.Arrays;
24+
import java.util.Calendar;
25+
import java.util.Collection;
26+
27+
// CHECKSTYLE IGNORE MagicNumberCheck
28+
/**
29+
* The Class BavarianCalendar.
30+
*/
31+
public class BavarianCalendar extends BasicCalendar
32+
{
33+
/**
34+
* Instantiates a new basic calendar.
35+
*
36+
* @param cfg the monkey configuration
37+
*/
38+
public BavarianCalendar(MonkeyConfiguration cfg)
39+
{
40+
super(cfg);
41+
}
42+
43+
/** {@inheritDoc} */
44+
@Override
45+
protected void loadHolidays(int year) {
46+
holidays.clear();
47+
48+
// these aren't all strictly holidays, but days when engineers will likely
49+
// not be in the office to respond to rampaging monkeys
50+
51+
// first of all, we need easter sunday doy,
52+
// because ome other holidays calculated from it
53+
int easter = westernEasterDayOfYear(year);
54+
55+
// new year
56+
holidays.addAll(getHolidayWithBridgeDays(year, dayOfYear(year, Calendar.JANUARY, 1)));
57+
58+
// epiphanie
59+
holidays.addAll(getHolidayWithBridgeDays(year, dayOfYear(year, Calendar.JANUARY, 6)));
60+
61+
// good friday, always friday, don't need to check if it's bridge day
62+
holidays.add(easter - 2);
63+
64+
// easter monday, always monday, don't need to check if it's bridge day
65+
holidays.add(easter + 1);
66+
67+
// labor day
68+
holidays.addAll(getHolidayWithBridgeDays(year, dayOfYear(year, Calendar.MAY, 1)));
69+
70+
// ascension day
71+
holidays.addAll(getHolidayWithBridgeDays(year, easter + 39));
72+
73+
// whit monday, always monday, don't need to check if it's bridge day
74+
holidays.add(easter + 50);
75+
76+
// corpus christi
77+
holidays.add(westernEasterDayOfYear(year) + 60);
78+
79+
// assumption day
80+
holidays.addAll(getHolidayWithBridgeDays(year, dayOfYear(year, Calendar.AUGUST, 15)));
81+
82+
// german unity day
83+
holidays.addAll(getHolidayWithBridgeDays(year, dayOfYear(year, Calendar.OCTOBER, 3)));
84+
85+
// all saints
86+
holidays.addAll(getHolidayWithBridgeDays(year, dayOfYear(year, Calendar.NOVEMBER, 1)));
87+
88+
// monkey goes on christmas vacations between christmas and new year!
89+
holidays.addAll(getHolidayWithBridgeDays(year, dayOfYear(year, Calendar.DECEMBER, 24)));
90+
holidays.add(dayOfYear(year, Calendar.DECEMBER, 25));
91+
holidays.add(dayOfYear(year, Calendar.DECEMBER, 26));
92+
holidays.add(dayOfYear(year, Calendar.DECEMBER, 27));
93+
holidays.add(dayOfYear(year, Calendar.DECEMBER, 28));
94+
holidays.add(dayOfYear(year, Calendar.DECEMBER, 29));
95+
holidays.add(dayOfYear(year, Calendar.DECEMBER, 30));
96+
holidays.add(dayOfYear(year, Calendar.DECEMBER, 31));
97+
98+
// mark the holiday set with the year, so on Jan 1 it will automatically
99+
// recalculate the holidays for next year
100+
holidays.add(year);
101+
}
102+
103+
/**
104+
* Returns collection of holidays, including Monday or Friday
105+
* if given holiday is Thuesday or Thursday.
106+
*
107+
* The behaviour to take Monday as day off if official holiday is Thuesday
108+
* and to take Friday as day off if official holiday is Thursday
109+
* is specific to [at least] Germany.
110+
* We call it, literally, "bridge day".
111+
*
112+
* @param dayOfYear holiday day of year
113+
*/
114+
private Collection<Integer> getHolidayWithBridgeDays(int year, int dayOfYear) {
115+
Calendar holiday = now();
116+
holiday.set(Calendar.YEAR, year);
117+
holiday.set(Calendar.DAY_OF_YEAR, dayOfYear);
118+
int dow = holiday.get(Calendar.DAY_OF_WEEK);
119+
int mon = holiday.get(Calendar.MONTH);
120+
int dom = holiday.get(Calendar.DAY_OF_MONTH);
121+
122+
// We don't want to include Monday if Thuesday is January 1.
123+
if (dow == Calendar.TUESDAY && dayOfYear != 1)
124+
return Arrays.asList(dayOfYear, dayOfYear - 1);
125+
126+
// We don't want to include Friday if Thursday is December 31.
127+
if (dow == Calendar.THURSDAY && (mon != Calendar.DECEMBER || dom != 31))
128+
return Arrays.asList(dayOfYear, dayOfYear + 1);
129+
130+
return Arrays.asList(dayOfYear);
131+
}
132+
133+
/**
134+
* Western easter sunday in year.
135+
*
136+
* @param year
137+
* the year
138+
* @return the day of the year of western easter sunday
139+
*/
140+
protected int westernEasterDayOfYear(int year) {
141+
int a = year % 19,
142+
b = year / 100,
143+
c = year % 100,
144+
d = b / 4,
145+
e = b % 4,
146+
g = (8 * b + 13) / 25,
147+
h = (19 * a + b - d - g + 15) % 30,
148+
j = c / 4,
149+
k = c % 4,
150+
m = (a + 11 * h) / 319,
151+
r = (2 * e + 2 * j - k - h + m + 32) % 7;
152+
int oneBasedMonth = (h - m + r + 90) / 25;
153+
int dayOfYear = (h - m + r + oneBasedMonth + 19) % 32;
154+
return dayOfYear(year, oneBasedMonth - 1, dayOfYear);
155+
}
156+
157+
}

src/test/java/com/netflix/simianarmy/basic/TestBasicCalendar.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ public void testGetBusinessDayWihHolidayNextYear() {
204204
Assert.assertEquals(businessDay.get(Calendar.YEAR), 2013);
205205
Assert.assertEquals(businessDay.get(Calendar.MONTH), Calendar.JANUARY);
206206
Assert.assertEquals(businessDay.get(Calendar.DAY_OF_MONTH), 2);
207-
Assert.assertEquals(businessDay.get(Calendar.HOUR_OF_DAY),
208-
hour);
207+
Assert.assertEquals(businessDay.get(Calendar.HOUR_OF_DAY), hour);
209208
}
209+
210210
}

0 commit comments

Comments
 (0)