public final class DayRange extends java.lang.Object implements java.lang.Iterable<Day>, java.io.Serializable
Instances of this class are immutable. You can safely reuse them and call them from multiple threads at once.
Constructor and Description |
---|
DayRange(Day first,
Day last)
Constructs a
DayRange object that starts on the specified
first day and ends on the specified last day. |
Modifier and Type | Method and Description |
---|---|
boolean |
contains(Day testDay)
Returns
true if the specified Day is
contained within this DayRange ; false
otherwise. |
boolean |
contains(DayRange testRange)
Returns
true if the specified DayRange is fully
contained within this DayRange ; false
otherwise. |
Day |
dayAt(int zeroBasedIndex)
Returns the non-null
Day at the specified zero-based index
within this DayRange . |
int |
dayCount()
Returns the number of days covered by this
DayRange (always
1 or more). |
boolean |
equals(java.lang.Object o)
Returns
true if o is a DayRange
covering exactly the same days as this DayRange ;
false otherwise. |
Day |
first()
Returns the non-null first
Day in this
DayRange . |
MaybeEmptyDayRanges |
fullCalendarMonthsWithin()
Returns a non-null possibly-empty set of
DayRange objects
representing the full calendar months that are fully contained within
this DayRange . |
MaybeEmptyDayRanges |
fullCalendarYearsWithin()
Returns a non-null possibly-empty set of
DayRange objects
representing the full calendar years that are fully contained within this
DayRange . |
MaybeEmptyDayRanges |
fullMonthsWithin(StartOfMonth startOfMonth)
Returns a non-null possibly-empty set of
DayRange objects
representing the full months (starting on the specified
StartOfMonth ) that are fully contained within this
DayRange . |
DayRanges |
fullOrPartialCalendarMonthsWithin()
Returns a non-null set of
DayRange objects representing the
full-or-partial calendar months that are contained within this
DayRange . |
DayRanges |
fullOrPartialCalendarYearsWithin()
Returns a non-null set of
DayRange objects representing the
full-or-partial calendar years that are contained within this
DayRange . |
DayRanges |
fullOrPartialMonthsWithin(StartOfMonth startOfMonth)
Returns a non-null set of
DayRange objects representing the
full-or-partial months (starting on the specified
StartOfMonth ) that are contained within this
DayRange . |
DayRanges |
fullOrPartialWeeksWithin(DayOfWeek firstDayOfWeek)
Returns a non-null set of
DayRange objects representing the
full-or-partial weeks (starting on the specified
firstDayOfWeek ) that are contained within this
DayRange . |
DayRanges |
fullOrPartialYearsWithin(StartOfYear startOfYear)
Returns a non-null set of
DayRange objects representing the
full-or-partial years (starting on the specified StartOfYear )
that are contained within this DayRange . |
MaybeEmptyDayRanges |
fullWeeksWithin(DayOfWeek firstDayOfWeek)
Returns a non-null possibly-empty set of
DayRange objects
representing the full weeks (starting on the specified
firstDayOfWeek ) that are fully contained within this
DayRange . |
MaybeEmptyDayRanges |
fullYearsWithin(StartOfYear startOfYear)
Returns a non-null possibly-empty set of
DayRange objects
representing the full years (starting on the specified
StartOfYear ) that are fully contained within this
DayRange . |
DayRange |
greedyUnion(Day dayToIncludeInUnion)
Returns the non-null
DayRange that includes all the days
within this DayRange , the specified Day
(dayToIncludeInUnion ), and any days between. |
DayRange |
greedyUnion(DayRange rangeToIncludeInUnion)
Returns the non-null
DayRange that includes all the days
within this DayRange , the specified DayRange
(rangeToIncludeInUnion ), and any days between. |
int |
hashCode()
Overridden to ensure consistency with
equals . |
int |
indexOf(Day day)
Returns the index of the specified
Day within this
DayRange , or -1 if it is not contained within this
DayRange . |
DayRange |
intersectionOrNull(DayRange intersectWith)
Returns a
DayRange covering the days that are contained
within both this DayRange and the specified
DayRange , or null if there is no such
intersection. |
java.util.Iterator<Day> |
iterator() |
Day |
last()
Returns the non-null last
Day in this DayRange . |
DayRanges |
toDailyDayRanges()
Returns a non-null
DayRanges object representing each
Day within this DayRange as its own
DayRange (covering just one day). |
java.lang.String |
toString()
Returns a non-null, non-empty string representation of this object for logging and debugging purposes.
|
public DayRange(Day first, Day last)
DayRange
object that starts on the specified
first
day and ends on the specified last
day.
If first
and last
represent the same day, the
range will cover that day only.
first
- the first day that the DayRange
should include.
Cannot be null
.last
- the last day that the DayRange
should include.
Cannot be null
, and must be equal to or later than
first
.java.lang.NullPointerException
- if first
or
last
is null
.java.lang.IllegalArgumentException
- if first
is after
last
.Day.to(Day)
public Day first()
Day
in this
DayRange
.public Day last()
Day
in this DayRange
.public int dayCount()
DayRange
(always
1 or more).public Day dayAt(int zeroBasedIndex)
Day
at the specified zero-based index
within this DayRange
.zeroBasedIndex
- the index of the Day
to return. Must
be greater than or equal to zero and less than dayCount()
.java.lang.IndexOutOfBoundsException
- if zeroBasedIndex
is less
than zero or greater than or equal to dayCount()
.indexOf(Day)
public java.util.Iterator<Day> iterator()
iterator
in interface java.lang.Iterable<Day>
public boolean contains(Day testDay)
true
if the specified Day
is
contained within this DayRange
; false
otherwise.public boolean contains(DayRange testRange)
true
if the specified DayRange
is fully
contained within this DayRange
; false
otherwise.testRange
- the DayRange
to test for containment within
this DayRange
.true
if the first day of testRange
is
equal to or after the first day of this DayRange
,
and the last day of testRange
is equal to or before
the last day of this DayRange
; false
otherwise.java.lang.NullPointerException
- if testRange
is
null
.public int indexOf(Day day)
Day
within this
DayRange
, or -1 if it is not contained within this
DayRange
.day
- the Day
to return the index of.java.lang.NullPointerException
- if day
is
null
.dayAt(int)
public DayRange intersectionOrNull(DayRange intersectWith)
DayRange
covering the days that are contained
within both this DayRange
and the specified
DayRange
, or null
if there is no such
intersection.
By definition, a.intersectionOrNull(b)
will always give the
same result as b.intersectionOrNull(a)
, provided neither
a
nor b
are null
.
intersectWith
- the other DayRange
that the
intersection should be calculated from (together with this
DayRange
). Cannot be null
.java.lang.NullPointerException
- if intersectWith
is
null
.public DayRange greedyUnion(DayRange rangeToIncludeInUnion)
DayRange
that includes all the days
within this DayRange
, the specified DayRange
(rangeToIncludeInUnion
), and any days between.
This method is "greedy" because, if there is a gap between this
DayRange
and rangeToIncludeInUnion
, that gap
will be included in the returned DayRange
.
rangeToIncludeInUnion
- the DayRange
that the returned
DayRange
should include (together with this
DayRange
and any days between). Cannot be
null
.java.lang.NullPointerException
- if rangeToIncludeInUnion
is
null
.public DayRange greedyUnion(Day dayToIncludeInUnion)
DayRange
that includes all the days
within this DayRange
, the specified Day
(dayToIncludeInUnion
), and any days between.
This method is "greedy" because, if there is a gap between this
DayRange
and dayToIncludeInUnion
, that gap will
be included in the returned DayRange
.
dayToIncludeInUnion
- the Day
that the returned
DayRange
should include (together with this
DayRange
and any days between). Cannot be
null
.java.lang.NullPointerException
- if dayToIncludeInUnion
is
null
.public MaybeEmptyDayRanges fullWeeksWithin(DayOfWeek firstDayOfWeek)
DayRange
objects
representing the full weeks (starting on the specified
firstDayOfWeek
) that are fully contained within this
DayRange
.
If you know that this DayRange
will contain at least one
full week starting on your specified firstDayOfWeek
you can
call MaybeEmptyDayRanges.notEmpty()
on the returned object to get
it as a DayRanges
. For example:
DayRanges result = dayRange.fullWeeksWithin(DayOfWeek.MONDAY).notEmpty();
firstDayOfWeek
- indicating the first day of a "week" e.g.
DayOfWeek.MONDAY
for weeks running from Monday to
Sunday (inclusive). Cannot be null
.java.lang.NullPointerException
- if firstDayOfWeek
is
null
.public MaybeEmptyDayRanges fullMonthsWithin(StartOfMonth startOfMonth)
DayRange
objects
representing the full months (starting on the specified
StartOfMonth
) that are fully contained within this
DayRange
.
If you know that this DayRange
will contain at least one
full month starting on your specified startOfMonth
you can
call MaybeEmptyDayRanges.notEmpty()
on the returned object to get
it as a DayRanges
. For example:
DayRanges result = dayRange.fullMonthsWithin(StartOfMonth.of(6)).notEmpty();
startOfMonth
- indicating the first day of a "month" e.g.
StartOfMonth.of(1)
for calendar months starting on
the 1st. Cannot be null
.java.lang.NullPointerException
- if startOfMonth
is
null
.public MaybeEmptyDayRanges fullCalendarMonthsWithin()
DayRange
objects
representing the full calendar months that are fully contained within
this DayRange
.
If you know that this DayRange
will contain at least one
full calendar month you can call MaybeEmptyDayRanges.notEmpty()
on the returned object to get it as a DayRanges
. For example:
DayRanges result = dayRange.fullCalendarMonthsWithin().notEmpty();
public MaybeEmptyDayRanges fullYearsWithin(StartOfYear startOfYear)
DayRange
objects
representing the full years (starting on the specified
StartOfYear
) that are fully contained within this
DayRange
.
If you know that this DayRange
will contain at least one
full year starting on your specified startOfYear
you can
call MaybeEmptyDayRanges.notEmpty()
on the returned object to get
it as a DayRanges
. For example:
DayRanges result = dayRange.fullYearsWithin(StartOfYear.of(4, 6)).notEmpty();
startOfYear
- indicating the first day of a "year" e.g.
StartOfYear.of(1, 1)
for calendar years starting on
January 1st. Cannot be null
.java.lang.NullPointerException
- if startOfYear
is
null
.public MaybeEmptyDayRanges fullCalendarYearsWithin()
DayRange
objects
representing the full calendar years that are fully contained within this
DayRange
.
If you know that this DayRange
will contain at least one
full calendar year you can call MaybeEmptyDayRanges.notEmpty()
on
the returned object to get it as a DayRanges
. For example:
DayRanges result = dayRange.fullCalendarYearsWithin().notEmpty();
public DayRanges fullOrPartialWeeksWithin(DayOfWeek firstDayOfWeek)
DayRange
objects representing the
full-or-partial weeks (starting on the specified
firstDayOfWeek
) that are contained within this
DayRange
.
For example, consider calling this method on a DayRange
covering Monday 2020-07-20 to Thursday 2020-07-30 (inclusive), and
specifying the first day of the week as Monday:
DayRange dayRange = Day.of(2020, 7, 20).to(2020, 7, 30);
DayRanges dayRanges = dayRange.fullOrPartialWeeksWithin(DayOfWeek.MONDAY);
The returned DayRanges
object would contain:
firstDayOfWeek
- indicating the first day of a "week" e.g.
DayOfWeek.MONDAY
for weeks running from Monday to
Sunday (inclusive). Cannot be null
.java.lang.NullPointerException
- if firstDayOfWeek
is
null
.public DayRanges fullOrPartialMonthsWithin(StartOfMonth startOfMonth)
DayRange
objects representing the
full-or-partial months (starting on the specified
StartOfMonth
) that are contained within this
DayRange
.
For example, consider calling this method on a DayRange
covering 2020-06-15 to 2020-07-26 (inclusive), and specifying the start
of the month as the 6th:
DayRange dayRange = Day.of(2020, 6, 15).to(2020, 7, 26);
DayRanges dayRanges = dayRange.fullOrPartialMonthsWithin(StartOfMonth.of(6));
The returned DayRanges
object would contain:
startOfMonth
- indicating the first day of a "month" e.g.
StartOfMonth.of(1)
for calendar months starting on
the 1st. Cannot be null
.java.lang.NullPointerException
- if startOfMonth
is
null
.public DayRanges fullOrPartialCalendarMonthsWithin()
DayRange
objects representing the
full-or-partial calendar months that are contained within this
DayRange
.
For example, consider calling this method on a DayRange
covering 2020-05-15 to 2020-07-26 (inclusive):
DayRange dayRange = Day.of(2020, 5, 15).to(2020, 7, 26);
DayRanges dayRanges = dayRange.fullOrPartialCalendarMonthsWithin();
The returned DayRanges
object would contain:
public DayRanges fullOrPartialYearsWithin(StartOfYear startOfYear)
DayRange
objects representing the
full-or-partial years (starting on the specified StartOfYear
)
that are contained within this DayRange
.
For example, consider calling this method on a DayRange
covering 2019-02-15 to 2020-07-26 (inclusive), and specifying the start
of the year as April 6th:
DayRange dayRange = Day.of(2019, 2, 15).to(2020, 7, 26);
DayRanges dayRanges = dayRange.fullOrPartialYearsWithin(StartOfYear.of(4, 6));
The returned DayRanges
object would contain:
startOfYear
- indicating the first day of a "year" e.g.
StartOfYear.of(1, 1)
for calendar years starting on
January 1st. Cannot be null
.java.lang.NullPointerException
- if startOfYear
is
null
.public DayRanges fullOrPartialCalendarYearsWithin()
DayRange
objects representing the
full-or-partial calendar years that are contained within this
DayRange
.
For example, consider calling this method on a DayRange
covering 2018-05-15 to 2020-07-26 (inclusive):
DayRange dayRange = Day.of(2018, 5, 15).to(2020, 7, 26);
DayRanges dayRanges = dayRange.fullOrPartialCalendarYearsWithin();
The returned DayRanges
object would contain:
public DayRanges toDailyDayRanges()
DayRanges
object representing each
Day
within this DayRange
as its own
DayRange
(covering just one day).
This is not as inefficient as it sounds for a large DayRange
(e.g. one covering many years), as the returned DayRanges
object will not actually create the individual single-day
DayRange
objects within it unless/until they are accessed.
public boolean equals(java.lang.Object o)
true
if o
is a DayRange
covering exactly the same days as this DayRange
;
false
otherwise.equals
in class java.lang.Object
public int hashCode()
equals
.hashCode
in class java.lang.Object
public java.lang.String toString()
The exact details of the representation are unspecified and subject to change.
toString
in class java.lang.Object
See www.degreedays.net/api/ for more about the Degree Days.net API.
You can sign up for a Degree Days.net API account and read the integration guide that is useful and applicable whether you are using Java or not.