Class Instant
- All Implemented Interfaces:
Serializable
,Comparable<Instant>
Day
s and Year
s, that also start at 1.
This class is also used to handle the concept of "time of day". In the Lukashian Calendar, the time of day is simply a BigFraction
that expresses
the proportion of the day that has passed. This BigFraction
can then be represented as, for example,
Basis Points.
The fact that an Instant is used to represent a unique millisecond on the timeline, as well as the proportion of the day that has passed, means that we need to specify how to translate between those two:
-
From a proportion (as
BigFraction
) to a specific millisecond: The thing to consider here is that a specific proportion (for example 0.12345th of the day), may not actually lead to a whole number of milliseconds to have passed on that day. The millisecond that is chosen as the result of the translation is the millisecond that the proportion "points to". The best way to visualize this is a wheel of fortune: the arrow is the proportion and the segments on the wheel are the milliseconds. It does not matter at which part of a segment the arrow points. If a certain proportion means that, for example, 120.01 milliseconds have passed on a certain day, then millisecond 121 of that day is chosen.If a certain proportion points exactly at a boundary between milliseconds, then the millisecond after the boundary is chosen. For example, if a certain proportion means that exactly 120 milliseconds have fully passed (and hence we are at the start of millisecond 121), then millisecond 121 of that day is chosen.
This effectively means that an Instant represents a specific millisecond including its start, but excluding its end, or in mathematical notation:
[m>
. This also effectively means that the calendar does have a start, but does not have an end, which is as expected. -
From a specific millisecond to a proportion (as
BigFraction
): This is not a problem, because a BigFraction has exact precision for any rational number, so for a specific millisecond, we can always exactly calculate the proportion of the day that has passed. The only questions is: is the millisecond itself considered to have fully passed?The answer is "no", the millisecond for which we calculate the proportion of the day that has passed, is itself not considered to have fully passed. In fact, this millisecond is considered to not have passed at all, meaning that we calculate the proportion of the day from the start of the day until the start of this millisecond. This means that the first millisecond of the day results in a proportion of 0 and the final millisecond of the day results in a proportion smaller than 1. This corresponds to the fact that a
Day
runs from its start (inclusive) to its end (exclusive).
Day
and the BigFraction that represents the proportion of its day that has passed. It does *not* store the unique millisecond
on the timeline that this Instant represents.
When creating an Instant using one of the constructors or methods that specifies the proportion of the day, the Instant stores the proportion with which it was created.
When creating an Instant using one of the constructors or methods that specifies the unique millisecond on the timeline, the translation to the corresponding
proportion is done as outlined above, after which the resulting Day
and proportion (as BigFraction) will be stored.
The standard Java methods equals(Object)
, hashCode()
and compareTo(Instant)
look at the unique millisecond on the timeline that this Instant
represents, not the exact proportion of the day.
So, why not store the unique millisecond on the timeline that this Instant represents, instead of the Day
and the proportion as a BigFraction? Remember the
translations: if we create an Instant at, say, 5000 beeps of a certain day, this proportion will "point" to a certain millisecond. It is unlikely that this proportion
will point exactly to a boundary between milliseconds. If we would then store said millisecond as the representation of the Instant, then what would happen if we
then calculate the number of beeps that have passed?
First, we would have to translate back to the proportion of the day that has passed. The millisecond itself is not considered to have passed at all (see the explanation
of the translations above), meaning that we won't get 0.5, but something like 0.499999999....... If this then gets truncated to beeps, we get 4999 instead of the
original 5000 (rounding cannot solve the problem, see getBeeps()
for details).
By storing the Day
and the proportion with which the Instant was created, we do not have this problem.
The other way around is not a problem either. BigFraction has exact precision for any rational number, so translating a unique millisecond on the timeline to a proportion, and then back again, is guaranteed to yield the original value.
Instant
is an immutable object. New instances are always created when calling one of the mutation methods.
- See Also:
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionReturns a newInstant
that represents the passed proportion of this instant's day on the next day, for example, if this instant represents a point at one third of its day, then calling this method will return an instant that represents one third of the next day.Returns a newInstant
that represents the passed proportion of this instant's day on the previous day, for example, if this instant represents a point at one third of its day, then calling this method will return an instant that represents one third of the previous day.int
int
differenceInBeepsWith
(Instant other) Returns the amount of beeps between this instant and the given non-nullInstant
, directionally.long
differenceWith
(Instant other) Returns the amount of milliseconds between this instant and the given non-nullInstant
, directionally.boolean
int
getBeeps()
Gets thegetProportionOfDay()
, represented as beeps.getDay()
Returns theDay
of this instant.long
Gets the unique millisecond on the timeline that this Instant represents.long
Gets the unique millisecond on the timeline that this Instant represents.org.apache.commons.numbers.fraction.BigFraction
Returns the proportion of theDay
of this instant that has passed.long
Gets the total number of milliseconds from the UNIX Epoch, up to the final point of the millisecond represented by this instant.getYear()
Returns the year that this instant is in.int
hashCode()
boolean
Returns whether this instant is after the given non-nullInstant
.boolean
Returns whether this instant is before the given non-nullInstant
.boolean
Returns whether this instant is in the given non-nullDay
.boolean
Returns whether this instant is in the given non-nullYear
.boolean
Returns whether this instant is not in the given non-nullDay
.boolean
Returns whether this instant is not in the given non-nullYear
.boolean
isSameOrAfter
(Instant other) Returns whether this instant is the same or after the given non-nullInstant
.boolean
isSameOrBefore
(Instant other) Returns whether this instant is the same or before the given non-nullInstant
.minusBeeps
(int beepsToSubtract) Returns a newInstant
that represents this instant minus the given amount of beeps.minusDays
(int daysToSubtract) Returns a newInstant
that represents the passed proportion of this instant's day on this instant's day minus the given amount of days, for example, if this instant represents a point at one third of its day, then calling this method will return an instant that represents one third of the resulting day.minusMilliseconds
(long millisecondsToSubtract) Returns a newInstant
that represents this instant minus the given amount of milliseconds.minusProportionOfDay
(org.apache.commons.numbers.fraction.BigFraction proportionToSubtract) minusSeconds
(long secondsToSubtract) Returns a newInstant
that represents this instant minus the given amount of seconds.minusYears
(int yearsToSubtract) Returns a newInstant
that represents the passed proportion of this instant's day on this instant's day minus the given amount of years, for example, if this instant represents a point at one third of its day, then calling this method will return an instant that represents one third of the resulting day.static Instant
now()
Returns the currentInstant
.static Instant
of
(int year, int day, int beeps) Creates a newInstant
that represents the millisecond at the point in time when the given proportion of the given day has passed.static Instant
of
(int year, int day, org.apache.commons.numbers.fraction.BigFraction proportionOfDay) Creates a newInstant
that represents the millisecond at the point in time when the given proportion of the given day has passed.static Instant
of
(long epochMilliseconds) Creates a newInstant
representing the given number of milliseconds since the start of the Lukashian Calendar.static Instant
Creates a newInstant
that represents the millisecond at the point in time when the given proportion of the given day has passed.static Instant
Creates a newInstant
that represents the millisecond at the point in time when the given proportion of the given day has passed.static Instant
Creates a newInstant
that represents the millisecond at the point in time when the given proportion of the given day has passed.static Instant
Creates a newInstant
that represents the millisecond at the point in time when the given proportion of the given day has passed.static Instant
ofJavaInstant
(Instant javaInstant) static Instant
ofUnixEpochMilliseconds
(long unixEpochMilliseconds) Creates a newInstant
representing the amount of milliseconds since the UNIX Epoch.plusBeeps
(int beepsToAdd) Returns a newInstant
that represents this instant plus the given amount of beeps.plusDays
(int daysToAdd) Returns a newInstant
that represents the passed proportion of this instant's day on this instant's day plus the given amount of days, for example, if this instant represents a point at one third of its day, then calling this method will return an instant that represents one third of the resulting day.plusMilliseconds
(long millisecondsToAdd) Returns a newInstant
that represents this instant plus the given amount of milliseconds.plusProportionOfDay
(org.apache.commons.numbers.fraction.BigFraction proportionToAdd) plusSeconds
(long secondsToAdd) Returns a newInstant
that represents this instant plus the given amount of seconds.plusYears
(int yearsToAdd) Returns a newInstant
that represents the passed proportion of this instant's day on this instant's day plus the given amount of years, for example, if this instant represents a point at one third of its day, then calling this method will return an instant that represents one third of the resulting day.toString()
-
Field Details
-
BEEPS_PER_DAY
public static final int BEEPS_PER_DAYThe amount of beeps per day, see Basis Points.- See Also:
-
-
Method Details
-
minusYears
Returns a newInstant
that represents the passed proportion of this instant's day on this instant's day minus the given amount of years, for example, if this instant represents a point at one third of its day, then calling this method will return an instant that represents one third of the resulting day.To see what the resulting day will be, see
Day.minusYears(int)
, which will be applied to this instant's day.- Throws:
LukashianException
- when the result would be before the start of the Lukashian Calendar or when the resulting year does not have a day with this instant's day's number
-
plusYears
Returns a newInstant
that represents the passed proportion of this instant's day on this instant's day plus the given amount of years, for example, if this instant represents a point at one third of its day, then calling this method will return an instant that represents one third of the resulting day.To see what the resulting day will be, see
Day.plusYears(int)
, which will be applied to this instant's day.- Throws:
LukashianException
- when the resulting year does not have a day with this instant's day's number
-
minusDays
Returns a newInstant
that represents the passed proportion of this instant's day on this instant's day minus the given amount of days, for example, if this instant represents a point at one third of its day, then calling this method will return an instant that represents one third of the resulting day.Calling this method might result in a
Instant
that is in a different year.- Throws:
LukashianException
- when the result would be before the start of the Lukashian Calendar
-
plusDays
Returns a newInstant
that represents the passed proportion of this instant's day on this instant's day plus the given amount of days, for example, if this instant represents a point at one third of its day, then calling this method will return an instant that represents one third of the resulting day.Calling this method might result in a
Instant
that is in a different year. -
atPreviousDay
Returns a newInstant
that represents the passed proportion of this instant's day on the previous day, for example, if this instant represents a point at one third of its day, then calling this method will return an instant that represents one third of the previous day.Calling this method might result in a
Instant
that is in a different year.- Throws:
LukashianException
- when the result would be before the start of the Lukashian Calendar
-
atNextDay
Returns a newInstant
that represents the passed proportion of this instant's day on the next day, for example, if this instant represents a point at one third of its day, then calling this method will return an instant that represents one third of the next day.Calling this method might result in a
Instant
that is in a different year.- Throws:
LukashianException
- when the result would be before the start of the Lukashian Calendar
-
minusMilliseconds
Returns a newInstant
that represents this instant minus the given amount of milliseconds. This might result in anInstant
that is in a different day or year.Please note that if the resulting
Instant
is formatted with the default options inFormatter
, it may look the same as the original if the change is less than 1 beep.- Throws:
LukashianException
- when the result would be before the start of the Lukashian Calendar
-
minusSeconds
Returns a newInstant
that represents this instant minus the given amount of seconds. This might result in anInstant
that is in a different day or year.Please note that if the resulting
Instant
is formatted with the default options inFormatter
, it may look the same as the original if the change is less than 1 beep.- Throws:
LukashianException
- when the result would be before the start of the Lukashian Calendar
-
plusMilliseconds
Returns a newInstant
that represents this instant plus the given amount of milliseconds. This might result in anInstant
that is in a different day or year.Please note that if the resulting
Instant
is formatted with the default options inFormatter
, it may look the same as the original if the change is less than 1 beep. -
plusSeconds
Returns a newInstant
that represents this instant plus the given amount of seconds. This might result in anInstant
that is in a different day or year.Please note that if the resulting
Instant
is formatted with the default options inFormatter
, it may look the same as the original if the change is less than 1 beep. -
minusProportionOfDay
public Instant minusProportionOfDay(org.apache.commons.numbers.fraction.BigFraction proportionToSubtract) Returns a newInstant
that represents this instant minus the given proportion of aDay
. This might result in anInstant
that is in a different day or year. The value of the passedBigFraction
will be interpreted in such a way that 1 is a full day. So 0.5 will represent half a day and 2.5 will represent two and a half days.Please note that since the duration of a day is not constant, the durations of the various parts of the proportion that are subtracted may vary if subtracting that proportion will lead to an
Instant
that is on a different day. For example, if thisInstant
is at 0.2 of the current day and 0.4 is subtracted, this will lead to anInstant
that is at 0.8 of the previous day. This means that the first 0.2 day that was subtracted has the duration of 20% of the current day and the last 0.2 day that was subtracted has the duration of 20% of the previous day.The same principle applies if more than 1 day is subtracted.
Please note that if the resulting
Instant
is formatted with the default options inFormatter
, it may look the same as the original if the change is less than 1 beep.- Throws:
LukashianException
- when the result would be before the start of the Lukashian Calendar
-
minusBeeps
Returns a newInstant
that represents this instant minus the given amount of beeps. This might result in anInstant
that is in a different day or year.Please note that the duration of a beep is 1/10000th of a day. Since the duration of a day is not constant, the duration of the beeps that are subtracted may vary if subtracting those beeps will lead to an
Instant
that is on a different day. For example, if thisInstant
is at 2000 beeps of the current day and 4000 beeps are subtracted, this will lead to anInstant
that is at 8000 beeps of the previous day. This means that the first 2000 beeps that were subtracted have the duration of 2000/10000th of the current day and the last 2000 beeps that were subtracted have the duration of 2000/10000th of the previous day.The same principle applies if more than 1 day's worth of beeps are subtracted.
Please note that if the resulting
Instant
is formatted with the default options inFormatter
, it may look the same as the original if the change is less than 1 beep.- Throws:
LukashianException
- when the result would be before the start of the Lukashian Calendar
-
plusProportionOfDay
Returns a newInstant
that represents this instant plus the given proportion of aDay
. This might result in anInstant
that is in a different day or year. The value of the passedBigFraction
will be interpreted in such a way that 1 is a full day. So 0.5 will represent half a day and 2.5 will represent two and a half days.Please note that since the duration of a day is not constant, the durations of the various parts of the proportion that are added may vary if adding that proportion will lead to an
Instant
that is on a different day. For example, if thisInstant
is at 0.8 of the current day and 0.4 is added, this will lead to anInstant
that is at 0.2 of the next day. This means that the first 0.2 day that was added has the duration of 20% of the current day and the last 0.2 day that was added has the duration of 20% of the next day.The same principle applies if more than 1 day is added.
Please note that if the resulting
Instant
is formatted with the default options inFormatter
, it may look the same as the original if the change is less than 1 beep. -
plusBeeps
Returns a newInstant
that represents this instant plus the given amount of beeps. This might result in anInstant
that is in a different day or year.Please note that the duration of a beep is 1/10000th of a day. Since the duration of a day is not constant, the duration of the beeps that are added may vary if adding those beeps will lead to an
Instant
that is on a different day. For example, if thisInstant
is at 8000 beeps of the current day and 4000 beeps are added, this will lead to anInstant
that is at 2000 beeps of the next day. This means that the first 2000 beeps that were added have the duration of 2000/10000th of the current day and the last 2000 beeps that were added have the duration of 2000/10000th of the next day.The same principle applies if more than 1 day's worth of beeps are added.
Please note that if the resulting
Instant
is formatted with the default options inFormatter
, it may look the same as the original if the change is less than 1 beep. -
isBefore
Returns whether this instant is before the given non-nullInstant
. This will compare the unique milliseconds on the timeline that the Instants represent. It will not compare the proportions of the respective days that the Instants represent. -
isSameOrBefore
Returns whether this instant is the same or before the given non-nullInstant
. This will compare the unique milliseconds on the timeline that the Instants represent. It will not compare the proportions of the respective days that the Instants represent. -
isAfter
Returns whether this instant is after the given non-nullInstant
. This will compare the unique milliseconds on the timeline that the Instants represent. It will not compare the proportions of the respective days that the Instants represent. -
isSameOrAfter
Returns whether this instant is the same or after the given non-nullInstant
. This will compare the unique milliseconds on the timeline that the Instants represent. It will not compare the proportions of the respective days that the Instants represent. -
isIn
Returns whether this instant is in the given non-nullYear
. -
isNotIn
Returns whether this instant is not in the given non-nullYear
. -
isIn
Returns whether this instant is in the given non-nullDay
. -
isNotIn
Returns whether this instant is not in the given non-nullDay
. -
getEpochMilliseconds
public long getEpochMilliseconds()Gets the unique millisecond on the timeline that this Instant represents. This is essentially the how-manieth millisecond on the entire calendar this Instant represents. See the javadoc ofInstant
for an explanation of how a proportion of a day is translated to a specific millisecond.This method returns the same result as
getMillisecond()
. -
getMillisecond
public long getMillisecond()Gets the unique millisecond on the timeline that this Instant represents. This is essentially the how-manieth millisecond on the entire calendar this Instant represents. See the javadoc ofInstant
for an explanation of how a proportion of a day is translated to a specific millisecond.This method returns the same result as
getEpochMilliseconds()
. -
getProportionOfDay
public org.apache.commons.numbers.fraction.BigFraction getProportionOfDay()Returns the proportion of theDay
of this instant that has passed. -
getDay
Returns theDay
of this instant. -
getYear
Returns the year that this instant is in. Note that this might be different from the year that theDay
of this instant is in, because aDay
is part of theYear
in which it starts, but it does not necessarily end in that year. If the year of the day of this instant is needed, please call getDay().getYear(). -
getBeeps
public int getBeeps()Gets thegetProportionOfDay()
, represented as beeps. This will result in an int between 0 (inclusive) and 9999 (inclusive). Please see the javadoc ofInstant
for an explanation.Rounding is not performed here, because that would require a correction to prevent overflowing into the next day, which in turn will lead to the first beep of a day lasting for only half a beep and the last one lasting for one and a half (because the last half would otherwise be rounded to the first beep of the following day).
As a result, the amount of beeps that is returned by this method is the amount of beeps that have fully passed at the time of the start of this instant. This means that everything after the first 4 significant digits of the proportion of the day that this instant represents, will be truncated.
-
getUnixEpochMilliseconds
public long getUnixEpochMilliseconds()Gets the total number of milliseconds from the UNIX Epoch, up to the final point of the millisecond represented by this instant. For instants that occurred before the UNIX Epoch, a negative number is returned. -
toJavaInstant
-
differenceWith
Returns the amount of milliseconds between this instant and the given non-nullInstant
, directionally. Therefore, if this instant is after the other instant, the result will be a positive number. If this instant is before the other instant, the result will be a negative number. If they represent the sameInstant
on the timeline, the result will be 0. -
differenceInBeepsWith
Returns the amount of beeps between this instant and the given non-nullInstant
, directionally. Therefore, if this instant is after the other instant, the result will be a positive number. If this instant is before the other instant, the result will be a negative number. If they represent the sameInstant
on the timeline, the result will be 0.Please note that the duration of a beep is 1/10000th of a day. Since the duration of a day is not constant, the duration of the beeps that constitute the returned difference may vary if this instant is compared to an
Instant
that is on a different day. For example, if thisInstant
is at 2000 beeps of the current day and it is compared to anInstant
that is at 8000 beeps of the previous day, the result will be 4000. 2000 of these beeps have the duration of 2000/10000th of the current day and the other 2000 beeps have the duration of 2000/10000th of the previous day.The same principle applies if there is more than 1 day between the compared
Instant
s.If the difference in beeps between the the compared
Instant
s is not a whole number, then the decimal part of the result will be truncated.If the exact difference between 2
Instant
s is needed, please usedifferenceWith(Instant)
. -
of
Creates a newInstant
representing the given number of milliseconds since the start of the Lukashian Calendar. See the javadoc ofInstant
for an explanation of how a millisecond is translated to a proportion of a day.- Throws:
LukashianException
- when the given number of milliseconds is lower than 0
-
of
Creates a newInstant
that represents the millisecond at the point in time when the given proportion of the given day has passed. For more information on how this mechanism works, see the javadoc ofInstant
.- Throws:
LukashianException
- when the given proportion is not between 0 (inclusive) and 1 (exclusive)
-
of
public static Instant of(Year year, int day, org.apache.commons.numbers.fraction.BigFraction proportionOfDay) Creates a newInstant
that represents the millisecond at the point in time when the given proportion of the given day has passed.- Throws:
LukashianException
- when the given day does not exist for the given year or when the given proportion is not between 0 (inclusive) and 1 (exclusive)
-
of
public static Instant of(int year, int day, org.apache.commons.numbers.fraction.BigFraction proportionOfDay) Creates a newInstant
that represents the millisecond at the point in time when the given proportion of the given day has passed.- Throws:
LukashianException
- when the given year is 0 or lower or when the given day does not exist for the given year or when the given proportion is not between 0 (inclusive) and 1 (exclusive)
-
of
Creates a newInstant
that represents the millisecond at the point in time when the given proportion of the given day has passed.- Throws:
LukashianException
- when the given proportion is not between 0 (inclusive) and 9999 (inclusive)
-
of
Creates a newInstant
that represents the millisecond at the point in time when the given proportion of the given day has passed.- Throws:
LukashianException
- when the given day does not exist for the given year or when the given proportion is not between 0 (inclusive) and 9999 (inclusive)
-
of
Creates a newInstant
that represents the millisecond at the point in time when the given proportion of the given day has passed.- Throws:
LukashianException
- when the given year is 0 or lower or when the given day does not exist for the given year or when the given proportion is not between 0 (inclusive) and 9999 (inclusive)
-
ofUnixEpochMilliseconds
Creates a newInstant
representing the amount of milliseconds since the UNIX Epoch. Negative numbers are allowed.- Throws:
LukashianException
- when the given value would result in a point before the start of the Lukashian Calendar
-
ofJavaInstant
- Throws:
LukashianException
- when the given value would result in a point before the start of the Lukashian Calendar
-
now
Returns the currentInstant
. -
compareTo
- Specified by:
compareTo
in interfaceComparable<Instant>
-
hashCode
public int hashCode() -
equals
-
toString
-