org.exparity.hamcrest.date.LocalTimeMatchers Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hamcrest-date Show documentation
Show all versions of hamcrest-date Show documentation
Hamcrest Date matchers for Java
The newest version!
package org.exparity.hamcrest.date;
import static org.exparity.hamcrest.date.core.TemporalConverters.LOCALTIME_AS_HOUR;
import static org.exparity.hamcrest.date.core.TemporalConverters.LOCALTIME_AS_LOCALTIME;
import static org.exparity.hamcrest.date.core.TemporalConverters.LOCALTIME_AS_MINUTE;
import static org.exparity.hamcrest.date.core.TemporalConverters.LOCALTIME_AS_SECOND;
import static org.exparity.hamcrest.date.core.TemporalFunctions.LOCALTIME;
import static org.exparity.hamcrest.date.core.TemporalProviders.hour;
import static org.exparity.hamcrest.date.core.TemporalProviders.localTime;
import static org.exparity.hamcrest.date.core.TemporalProviders.minute;
import static org.exparity.hamcrest.date.core.TemporalProviders.second;
import java.time.LocalTime;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;
import org.exparity.hamcrest.date.core.IsAfter;
import org.exparity.hamcrest.date.core.IsBefore;
import org.exparity.hamcrest.date.core.IsHour;
import org.exparity.hamcrest.date.core.IsMaximum;
import org.exparity.hamcrest.date.core.IsMinimum;
import org.exparity.hamcrest.date.core.IsMinute;
import org.exparity.hamcrest.date.core.IsSameOrAfter;
import org.exparity.hamcrest.date.core.IsSameOrBefore;
import org.exparity.hamcrest.date.core.IsSecond;
import org.exparity.hamcrest.date.core.IsWithin;
import org.exparity.hamcrest.date.core.types.Interval;
import org.hamcrest.Matcher;
/**
* Static factory for creating {@link org.hamcrest.Matcher} instances for comparing {@link LocalTime} instances
*
* @author Stewart Bissett
*/
public abstract class LocalTimeMatchers {
/**
* Creates a matcher that matches when the examined time is after the reference time
*
* For example:
*
*
* MatcherAssert.assertThat(myTime, LocalTimeMatchers.after(LocalTime.now()));
*
*
* @param time the reference time against which the examined time is checked
*/
public static Matcher after(final LocalTime time) {
return new IsAfter<>(LOCALTIME_AS_LOCALTIME, localTime(time), LOCALTIME);
}
/**
* Creates a matcher that matches when the examined time is after the end of the reference year
*
* For example:
*
*
* MatcherAssert.assertThat(myTime, LocalTimeMatchers.after(23,59,59);
*
*
* @param hour the hour of the day
* @param minute the minute of the hour
* @param second the second of the minute
*/
public static Matcher after(final int hour, final int minute, final int second) {
return after(LocalTime.of(hour, minute, second));
}
/**
* Creates a matcher that matches when the examined time is before the reference time
*
* For example:
*
*
* MatcherAssert.assertThat(myTime, LocalTimeMatchers.before(LocalTime.now()));
*
*
* @param time the reference time against which the examined time is checked
*/
public static Matcher before(final LocalTime time) {
return new IsBefore<>(LOCALTIME_AS_LOCALTIME, localTime(time), LOCALTIME);
}
/**
* Creates a matcher that matches when the examined time is before the end of the reference year
*
* For example:
*
*
* MatcherAssert.assertThat(myTime, LocalTimeMatchers.before(23,59,59);
*
*
* @param hour the hour of the day
* @param minute the minute of the hour
* @param second the second of the minute
*/
public static Matcher before(final int hour, final int minute, final int second) {
return before(LocalTime.of(hour, minute, second));
}
/**
* Creates a matcher that matches when the examined time is at the same instant or before the reference time
*
* For example:
*
*
* assertThat(myTime, isSameOrBefore(LocalTime.now()))
*
*
* @param time the reference time against which the examined time is checked
*/
public static Matcher sameOrBefore(final LocalTime time) {
return new IsSameOrBefore<>(LOCALTIME_AS_LOCALTIME, localTime(time), LOCALTIME);
}
/**
* Creates a matcher that matches when the examined time is on the same day or before the start of the reference
* time
*
* For example:
*
*
* assertThat(myTime, isSameOrBefore(23, 59, 59));
*
*
* @param hour the hour of the day
* @param minute the minute of the hour
* @param second the second of the minute
*/
public static Matcher sameOrBefore(final int hour, final int minute, final int second) {
return sameOrBefore(LocalTime.of(hour, minute, second));
}
/**
* Creates a matcher that matches when the examined time is at the same instant or after the reference time
*
* For example:
*
*
* assertThat(myTime, isSameOrAfter(LocalTime.now()))
*
*
* @param time the reference time against which the examined time is checked
*/
public static Matcher sameOrAfter(final LocalTime time) {
return new IsSameOrAfter<>(LOCALTIME_AS_LOCALTIME, localTime(time), LOCALTIME);
}
/**
* Creates a matcher that matches when the examined time is on the same day or after the start of the reference time
*
* For example:
*
*
* assertThat(myTime, isSameOrAfter(23, 59, 59));
*
*
* @param hour the hour of the day
* @param minute the minute of the hour
* @param second the second of the minute
*/
public static Matcher sameOrAfter(final int hour, final int minute, final int second) {
return sameOrAfter(LocalTime.of(hour, minute, second));
}
/**
* Creates a matcher that matches when the examined time is within a defined period the reference time
*
* For example:
*
*
* assertThat(myTime, within(10, TimeUnit.SECONDS, LocalTime.NOON))
*
*
* @param time the reference time against which the examined time is checked
*/
public static Matcher within(final long period, final ChronoUnit unit, final LocalTime time) {
return new IsWithin<>(Interval.of(period, unit), LOCALTIME_AS_LOCALTIME, localTime(time), LOCALTIME);
}
/**
* Creates a matcher that matches when the examined time is within a given period of the reference time
*
* For example:
*
*
* assertThat(myTime, within(10, TimeUnit.SECONDS, 23, 59, 59));
*
*
* @param period the timeunit interval the examined time should be with
* @param unit the timeunit to define the length of the period
* @param hour the hour of the day
* @param minute the minute of the hour
* @param second the second of the minute
*/
public static Matcher within(final long period,
final ChronoUnit unit,
final int hour,
final int minute,
final int second) {
return within(period, unit, LocalTime.of(hour, minute, second));
}
/**
* Creates a matcher that matches when the examined date is on the maximum value of the given date part in its
* period
*
* For example:
*
*
* assertThat(myDate, isMaximumDayOfMonth(ChronoField.DAY_OF_MONTH));
*
*
* @param field the temporal field to check
*/
public static Matcher isMinimum(final ChronoField field) {
return new IsMinimum<>(LOCALTIME_AS_LOCALTIME, field);
}
/**
* Creates a matcher that matches when the examined date is on the maximum value of the given date part in its
* period
*
* For example:
*
*
* assertThat(myDate, isMaximum(ChronoField.DAY_OF_MONTH));
*
*
* @param field the temporal field to check
*/
public static Matcher isMaximum(final ChronoField field) {
return new IsMaximum<>(LOCALTIME_AS_LOCALTIME, field);
}
/**
* Creates a matcher that matches when the examined time is on the expected hour (0-23)
*
* For example:
*
*
* assertThat(myTime, isHour(12));
*
*
* @param hour the hour of the day (0-23)
*/
public static Matcher isHour(final int hour) {
return new IsHour<>(LOCALTIME_AS_HOUR, hour(hour));
}
/**
* Creates a matcher that matches when the examined time is on the same hour as the reference time
*
* For example:
*
*
* assertThat(myTime, sameHourOfDay(LocalTime.now()))
*
*
* @param time the reference time against which the examined time is checked
*/
public static Matcher sameHourOfDay(final LocalTime time) {
return new IsHour<>(LOCALTIME_AS_HOUR, hour(time));
}
/**
* Creates a matcher that matches when the examined time is on the expected minute (0-59)
*
* For example:
*
*
* assertThat(myTime, isMinute(12));
*
*
* @param minute the minute of the day (0-59)
*/
public static Matcher isMinute(final int minute) {
return new IsMinute<>(LOCALTIME_AS_MINUTE, minute(minute));
}
/**
* Creates a matcher that matches when the examined time is on the same minute as the reference time
*
* For example:
*
*
* assertThat(myTime, sameMinuteOfHour(LocalTime.now()))
*
*
* @param time the reference time against which the examined time is checked
*/
public static Matcher sameMinuteOfHour(final LocalTime time) {
return new IsMinute<>(LOCALTIME_AS_MINUTE, minute(time));
}
/**
* Creates a matcher that matches when the examined time is on the expected second (0-59)
*
* For example:
*
*
* assertThat(myTime, isSecond(12));
*
*
* @param second the second of the day (0-59)
*/
public static Matcher isSecond(final int second) {
return new IsSecond<>(LOCALTIME_AS_SECOND, second(second));
}
/**
* Creates a matcher that matches when the examined time is on the same second as the reference time
*
* For example:
*
*
* assertThat(myTime, sameSecondOfMinute(LocalTime.now()))
*
*
* @param time the reference time against which the examined time is checked
*/
public static Matcher sameSecondOfMinute(final LocalTime time) {
return new IsSecond<>(LOCALTIME_AS_SECOND, second(time));
}
}