Class TimePeriod
java.lang.Object
info.ejava.assignments.api.houserenters.dto.rentals.TimePeriod
- All Implemented Interfaces:
Comparable<TimePeriod>
This class provides a convenience mechanism for tracking a start
and end date, determining their relationship to other time periods,
and anything else that can offload start/end data evaluations from
the service logic. Note: dates are inclusive. A startDate represents
the first day and the endDate represents the lastDay. That means they
can be the same date and represent 1 day.
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionTimePeriod
(@NotNull LocalDate startDate) TimePeriod
(@NotNull LocalDate startDate, @jakarta.validation.constraints.Positive int days) TimePeriod
(@NotNull LocalDate startDate, @NotNull LocalDate endDate) -
Method Summary
Modifier and TypeMethodDescriptionint
compareTo
(@NotNull TimePeriod rhs) Returns negative if this instance does not overlap and is before the provided instance.static TimePeriod
Creates a timeperiod according to what was provided for startDate and endDate@jakarta.validation.constraints.Positive int
getDays()
@NotNull Period
boolean
Evaluate whether the date provided is between the startDate and endDate, both inclusive.boolean
isOverlap
(TimePeriod other) Evaluate whether the time period provided has a start or end date that overlaps with this one.next()
Return a timePeriod that starts the day after endDateprev()
slide
(int days) Return a timeperiod like this one, just N days prior or after.slide
(TemporalAmount amount) Return a timeperiod like this one an amount before or after this one.
-
Field Details
-
startDate
-
endDate
-
-
Constructor Details
-
TimePeriod
-
TimePeriod
-
TimePeriod
public TimePeriod(@NotNull @NotNull LocalDate startDate, @Positive @jakarta.validation.constraints.Positive int days)
-
-
Method Details
-
getDays
@Positive public @jakarta.validation.constraints.Positive int getDays() -
getPeriod
-
isBetween
Evaluate whether the date provided is between the startDate and endDate, both inclusive.- Parameters:
date
- to test between- Returns:
- true if between the start and end dates, inclusive
-
isOverlap
Evaluate whether the time period provided has a start or end date that overlaps with this one.- Parameters:
other
- timePeriod to evaluate- Returns:
- true if timePeriods overlap
-
slide
Return a timeperiod like this one, just N days prior or after.- Parameters:
days
- amount to shift the new TimePeriod- Returns:
- timeperiod
-
slide
Return a timeperiod like this one an amount before or after this one.- Parameters:
amount
-- Returns:
- timeperiod
-
prev
-
next
Return a timePeriod that starts the day after endDate- Returns:
- next time period like this
-
create
Creates a timeperiod according to what was provided for startDate and endDate- Parameters:
startDate
- will be set to endDate if startDate not providedendDate
- will be set to startDate if endDate not provided- Returns:
- timeperiod
-
compareTo
Returns negative if this instance does not overlap and is before the provided instance. Returns positive if it does not overlap and after the other instance. Otherwise 0 is returned.- Specified by:
compareTo
in interfaceComparable<TimePeriod>
- Parameters:
rhs
- the object to be compared.- Returns:
- 0 if overlapped, -1 before, and +1 after
-