
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
package org.exparity.hamcrest.date;
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.format.DatePartFormatter;
import org.exparity.hamcrest.date.core.format.LocalTimeFormatter;
import org.exparity.hamcrest.date.core.wrapper.LocalTimeWrapper;
import org.hamcrest.Factory;
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(new LocalTimeWrapper(time), new LocalTimeFormatter());
}
/**
* 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 new IsAfter(new LocalTimeWrapper(hour, minute, second), new LocalTimeFormatter());
}
/**
* 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(new LocalTimeWrapper(time), new LocalTimeFormatter());
}
/**
* 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 new IsBefore(new LocalTimeWrapper(hour, minute, second), new LocalTimeFormatter());
}
/**
* 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(new LocalTimeWrapper(time), new LocalTimeFormatter());
}
/**
* 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
*/
@Factory
public static Matcher sameOrBefore(final int hour, final int minute, final int second) {
return new IsSameOrBefore(new LocalTimeWrapper(hour, minute, second), new LocalTimeFormatter());
}
/**
* 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(new LocalTimeWrapper(time), new LocalTimeFormatter());
}
/**
* 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(period, unit, new LocalTimeWrapper(time), new LocalTimeFormatter());
}
/**
* 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 new IsWithin(period,
unit,
new LocalTimeWrapper(hour, minute, second),
new LocalTimeFormatter());
}
/**
* 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(field, t -> t, new DatePartFormatter());
}
/**
* 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(field, t -> t, new DatePartFormatter());
}
/**
* 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(hour, t -> t);
}
/**
* 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 isHour(time.getHour());
}
/**
* 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(minute, t -> t);
}
/**
* 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 isMinute(time.getMinute());
}
/**
* 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(Second, t -> t);
}
/**
* 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 isSecond(time.getSecond());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy