java.lang.Object
info.ejava.assignments.api.houserenters.dto.rentals.TimePeriod
All Implemented Interfaces:
Comparable<TimePeriod>

public class TimePeriod extends Object implements 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 Details

  • Constructor Details

    • TimePeriod

      public TimePeriod(@NotNull @NotNull LocalDate startDate, @NotNull @NotNull LocalDate endDate)
    • TimePeriod

      public TimePeriod(@NotNull @NotNull LocalDate startDate)
    • 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

      @NotNull public @NotNull Period getPeriod()
    • isBetween

      public boolean isBetween(LocalDate date)
      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

      public boolean isOverlap(TimePeriod other)
      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

      public TimePeriod slide(int days)
      Return a timeperiod like this one, just N days prior or after.
      Parameters:
      days - amount to shift the new TimePeriod
      Returns:
      timeperiod
    • slide

      public TimePeriod slide(TemporalAmount amount)
      Return a timeperiod like this one an amount before or after this one.
      Parameters:
      amount -
      Returns:
      timeperiod
    • prev

      public TimePeriod prev()
    • next

      public TimePeriod next()
      Return a timePeriod that starts the day after endDate
      Returns:
      next time period like this
    • create

      public static TimePeriod create(LocalDate startDate, LocalDate endDate)
      Creates a timeperiod according to what was provided for startDate and endDate
      Parameters:
      startDate - will be set to endDate if startDate not provided
      endDate - will be set to startDate if endDate not provided
      Returns:
      timeperiod
    • compareTo

      public int compareTo(@NotNull @NotNull TimePeriod rhs)
      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 interface Comparable<TimePeriod>
      Parameters:
      rhs - the object to be compared.
      Returns:
      0 if overlapped, -1 before, and +1 after