Skip to content

Commit

Permalink
Finished the year methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
shiver-me-timbers committed Nov 22, 2015
1 parent 6aeb6ad commit e3b6d67
Show file tree
Hide file tree
Showing 17 changed files with 476 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,15 @@ interface Calendar {

Calendar withDateOfMonth(int date);

Calendar withDayOfYear(int day);

Calendar minusMonths(int months);

Calendar addMonths(int months);

Calendar minusYears(int years);

Calendar addYears(int years);

long toTime();
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ interface Calendars {
Calendar midnightToday();

int daysThisMonth();

int daysThisYear();
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ public long someTimeInAMonth() {
return daysToMilliseconds(integers.someNumberBetween(1, calendars.daysThisMonth()));
}

@Override
public long someTimeInAYear() {
return daysToMilliseconds(integers.someNumberBetween(1, calendars.daysThisYear()));
}

@Override
public long midnightThisWeekOn(WeekDay weekDay) {
return calendars.midnightToday().withDayOfWeek(weekDay).toTime();
Expand All @@ -53,6 +58,11 @@ public long midnightThisMonthOnThe(int date) {
return calendars.midnightToday().withDateOfMonth(date).toTime();
}

@Override
public long midnightThisYearOnDay(int day) {
return calendars.midnightToday().withDayOfYear(day).toTime();
}

@Override
public long minusDays(long time, int days) {
return calendars.create(time).minusDays(days).toTime();
Expand Down Expand Up @@ -83,7 +93,17 @@ public long addMonths(long time, int months) {
return calendars.create(time).addMonths(months).toTime();
}

@Override
public long minusYears(long time, int years) {
return calendars.create(time).minusYears(years).toTime();
}

@Override
public long addYears(long time, int years) {
return calendars.create(time).addYears(years).toTime();
}

private long daysToMilliseconds(int days) {
return (long) days * 24L * 60L * 60L * 1000L;
return (long) days * MILLISECONDS_IN_ONE_DAY;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ public Calendar withDateOfMonth(int date) {
return this;
}

@Override
public Calendar withDayOfYear(int day) {
calendar.set(java.util.Calendar.DAY_OF_YEAR, day);
return this;
}

@Override
public Calendar minusMonths(int months) {
calendar.add(java.util.Calendar.MONTH, -months);
Expand All @@ -74,6 +80,18 @@ public Calendar addMonths(int months) {
return this;
}

@Override
public Calendar minusYears(int years) {
calendar.add(java.util.Calendar.YEAR, -years);
return this;
}

@Override
public Calendar addYears(int years) {
calendar.add(java.util.Calendar.YEAR, years);
return this;
}

@Override
public long toTime() {
return calendar.getTimeInMillis();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.GregorianCalendar;

import static java.util.Calendar.DAY_OF_MONTH;
import static java.util.Calendar.DAY_OF_YEAR;
import static java.util.Calendar.HOUR_OF_DAY;
import static java.util.Calendar.MILLISECOND;
import static java.util.Calendar.MINUTE;
Expand Down Expand Up @@ -36,4 +37,9 @@ public Calendar midnightToday() {
public int daysThisMonth() {
return new GregorianCalendar().getActualMaximum(DAY_OF_MONTH);
}

@Override
public int daysThisYear() {
return new GregorianCalendar().getActualMaximum(DAY_OF_YEAR);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@ public RandomTimeBuilder addMonths(int months) {
return this;
}

public RandomTimeBuilder minusYears(int years) {
setTime(timeStamps.minusYears(getTime(), years));
return this;
}

public RandomTimeBuilder addYears(int years) {
setTime(timeStamps.addYears(getTime(), years));
return this;
}

private RandomTimeBuilder someTimeBefore(long time) {
setTime(time + (longs.someNegativeNumber() - 1));
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,20 @@ public RandomTimeBuilder create(Date date) {
}

@Override
public RandomTimeBuilder random() {
return create(longs.someNumber());
public RandomTimeBuilder now() {
return create(timeStamps.now());
}

@Override
public RandomTimeBuilder between(Date min, Date max) {
final long minTime = min.getTime();
final long maxTime = max.getTime();
return create(longs.someNumberBetween(minTime, maxTime));
}

@Override
public RandomTimeBuilder today() {
return create(timeStamps.todayMidnight() + timeStamps.someTimeInADay());
}

@Override
Expand All @@ -48,20 +60,18 @@ public RandomTimeBuilder thisMonthOnThe(int date) {
}

@Override
public RandomTimeBuilder today() {
return create(timeStamps.todayMidnight() + timeStamps.someTimeInADay());
public RandomTimeBuilder thisYear() {
return create(timeStamps.midnightThisYearOnDay(1) + timeStamps.someTimeInAYear());
}

@Override
public RandomTimeBuilder between(Date min, Date max) {
final long minTime = min.getTime();
final long maxTime = max.getTime();
return create(longs.someNumberBetween(minTime, maxTime));
public RandomTimeBuilder thisYearOnDay(int day) {
return create(timeStamps.midnightThisYearOnDay(day) + timeStamps.someTimeInADay());
}

@Override
public RandomTimeBuilder now() {
return create(timeStamps.now());
public RandomTimeBuilder random() {
return create(longs.someNumber());
}

private RandomTimeBuilder create(long time) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ interface RandomTimeCreator<D extends Date> {

D create(Date date);

D random();
D now();

D between(Date min, Date max);

D today();

D thisWeek();

Expand All @@ -19,9 +23,9 @@ interface RandomTimeCreator<D extends Date> {

D thisMonthOnThe(int date);

D today();
D thisYear();

D between(Date min, Date max);
D thisYearOnDay(int day);

D now();
D random();
}
Original file line number Diff line number Diff line change
Expand Up @@ -120,32 +120,32 @@ public RandomTimeBuilder someTimeNextMonthOnThe(int date) {

@Override
public RandomTimeBuilder someTimeLastYear() {
throw new UnsupportedOperationException();
return thisYear().minusYears(1);
}

@Override
public RandomTimeBuilder someTimeThisYear() {
throw new UnsupportedOperationException();
return thisYear();
}

@Override
public RandomTimeBuilder someTimeNextYear() {
throw new UnsupportedOperationException();
return thisYear().addYears(1);
}

@Override
public RandomTimeBuilder someTimeLastYearOnThe(int day) {
throw new UnsupportedOperationException();
return thisYearOn(day).minusYears(1);
}

@Override
public RandomTimeBuilder someTimeThisYearOnThe(int day) {
throw new UnsupportedOperationException();
return thisYearOn(day);
}

@Override
public RandomTimeBuilder someTimeNextYearOnThe(int day) {
throw new UnsupportedOperationException();
return thisYearOn(day).addYears(1);
}

private RandomTimeBuilder now() {
Expand Down Expand Up @@ -175,4 +175,12 @@ private RandomTimeBuilder thisMonth() {
private RandomTimeBuilder thisMonthOnThe(int date) {
return randomTimeCreator.thisMonthOnThe(date);
}

private RandomTimeBuilder thisYear() {
return randomTimeCreator.thisYear();
}

private RandomTimeBuilder thisYearOn(int day) {
return randomTimeCreator.thisYearOnDay(day);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,14 @@ interface TimeStamps {

long someTimeInAMonth();

long someTimeInAYear();

long midnightThisWeekOn(WeekDay weekDay);

long midnightThisMonthOnThe(int date);

long midnightThisYearOnDay(int day);

long minusDays(long time, int days);

long addDays(long time, int days);
Expand All @@ -30,4 +34,8 @@ interface TimeStamps {
long minusMonths(long time, int months);

long addMonths(long time, int months);

long minusYears(long time, int years);

long addYears(long time, int years);
}
Loading

0 comments on commit e3b6d67

Please sign in to comment.