Skip to content

Commit 19af4eb

Browse files
committed
java: meetup tests and example implementation
A straight port of the Ruby tests. The example implementation uses Joda Time which is generally considered a saner date/time library than what is built in to Java.
1 parent 5fc1a1b commit 19af4eb

5 files changed

Lines changed: 711 additions & 2 deletions

File tree

‎assignments/java/meetup/build.gradle‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,7 @@ repositories {
77
}
88

99
dependencies {
10+
compile "joda-time:joda-time:2.3+"
1011
testCompile "junit:junit:4.10"
12+
testCompile "org.easytesting:fest-assert-core:2.0M10"
1113
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import org.joda.time.DateTime;
2+
3+
public class Meetup {
4+
private final DateTime startOfMonth;
5+
6+
public Meetup(int monthOfYear, int year) {
7+
startOfMonth = new DateTime(year, monthOfYear, 1, 0, 0);
8+
}
9+
10+
DateTime day(int dayOfWeek, MeetupSchedule schedule) {
11+
DateTime current = cycleToNext(dayOfWeek, startOfMonth);
12+
switch (schedule) {
13+
case FIRST:
14+
break;
15+
case SECOND:
16+
current = current.plusWeeks(1);
17+
break;
18+
case THIRD:
19+
current = current.plusWeeks(2);
20+
break;
21+
case FOURTH:
22+
current = current.plusWeeks(3);
23+
break;
24+
case TEENTH:
25+
while (current.getDayOfMonth() < 13) {
26+
current = current.plusWeeks(1);
27+
}
28+
break;
29+
case LAST:
30+
current = cycleToPrev(dayOfWeek, startOfMonth.plusMonths(1).minusDays(1));
31+
break;
32+
default:
33+
return null;
34+
}
35+
return current;
36+
}
37+
38+
private DateTime cycleToPrev(int dayOfWeek, DateTime current) {
39+
while (current.getDayOfWeek() != dayOfWeek) {
40+
current = current.minusDays(1);
41+
}
42+
return current;
43+
}
44+
45+
private DateTime cycleToNext(int dayOfWeek, DateTime current) {
46+
while (current.getDayOfWeek() != dayOfWeek) {
47+
current = current.plusDays(1);
48+
}
49+
return current;
50+
}
51+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#Mon Dec 02 00:17:20 EST 2013
1+
#Tue Feb 04 23:44:51 EST 2014
22
distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6-
distributionUrl=http\://services.gradle.org/distributions/gradle-1.8-bin.zip
6+
distributionUrl=http\://services.gradle.org/distributions/gradle-1.8-all.zip
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
public enum MeetupSchedule {
2+
FIRST,
3+
SECOND,
4+
THIRD,
5+
FOURTH,
6+
LAST,
7+
TEENTH
8+
}

0 commit comments

Comments
 (0)