Class Tenor

  • All Implemented Interfaces:
    java.io.Serializable, java.lang.Comparable<Tenor>, java.time.temporal.TemporalAmount

    public final class Tenor
    extends java.lang.Object
    implements java.lang.Comparable<Tenor>, java.time.temporal.TemporalAmount, java.io.Serializable
    A tenor indicating how long it will take for a financial instrument to reach maturity.

    A tenor is allowed to be any non-negative non-zero period of days, weeks, month or years. This class provides constants for common tenors which are best used by static import.

    Each tenor is based on a Period. The months and years of the period are not normalized, thus it is possible to have a tenor of 12 months and a different one of 1 year. When used, standard date addition rules apply, thus there is no difference between them. Call normalized() to apply normalization.

    Usage

    Tenor implements TemporalAmount allowing it to be directly added to a date:
      LocalDate later = baseDate.plus(tenor);
     
    See Also:
    Serialized Form
    • Field Detail

      • TENOR_1D

        public static final Tenor TENOR_1D
        A tenor of one day.
      • TENOR_2D

        public static final Tenor TENOR_2D
        A tenor of two days.
      • TENOR_3D

        public static final Tenor TENOR_3D
        A tenor of three days.
      • TENOR_1W

        public static final Tenor TENOR_1W
        A tenor of 1 week.
      • TENOR_2W

        public static final Tenor TENOR_2W
        A tenor of 2 weeks.
      • TENOR_3W

        public static final Tenor TENOR_3W
        A tenor of 3 weeks.
      • TENOR_4W

        public static final Tenor TENOR_4W
        A tenor of 4 weeks.
      • TENOR_6W

        public static final Tenor TENOR_6W
        A tenor of 6 weeks.
      • TENOR_13W

        public static final Tenor TENOR_13W
        A tenor of 13 weeks.
      • TENOR_26W

        public static final Tenor TENOR_26W
        A tenor of 26 weeks.
      • TENOR_52W

        public static final Tenor TENOR_52W
        A tenor of 52 weeks.
      • TENOR_1M

        public static final Tenor TENOR_1M
        A tenor of 1 month.
      • TENOR_2M

        public static final Tenor TENOR_2M
        A tenor of 2 months.
      • TENOR_3M

        public static final Tenor TENOR_3M
        A tenor of 3 months.
      • TENOR_4M

        public static final Tenor TENOR_4M
        A tenor of 4 months.
      • TENOR_5M

        public static final Tenor TENOR_5M
        A tenor of 5 months.
      • TENOR_6M

        public static final Tenor TENOR_6M
        A tenor of 6 months.
      • TENOR_7M

        public static final Tenor TENOR_7M
        A tenor of 7 months.
      • TENOR_8M

        public static final Tenor TENOR_8M
        A tenor of 8 months.
      • TENOR_9M

        public static final Tenor TENOR_9M
        A tenor of 9 months.
      • TENOR_10M

        public static final Tenor TENOR_10M
        A tenor of 10 months.
      • TENOR_11M

        public static final Tenor TENOR_11M
        A tenor of 11 months.
      • TENOR_12M

        public static final Tenor TENOR_12M
        A tenor of 12 months.
      • TENOR_15M

        public static final Tenor TENOR_15M
        A tenor of 15 months.
      • TENOR_18M

        public static final Tenor TENOR_18M
        A tenor of 18 months.
      • TENOR_21M

        public static final Tenor TENOR_21M
        A tenor of 21 months.
      • TENOR_1Y

        public static final Tenor TENOR_1Y
        A tenor of 1 year.
      • TENOR_2Y

        public static final Tenor TENOR_2Y
        A tenor of 2 years.
      • TENOR_3Y

        public static final Tenor TENOR_3Y
        A tenor of 3 years.
      • TENOR_4Y

        public static final Tenor TENOR_4Y
        A tenor of 4 years.
      • TENOR_5Y

        public static final Tenor TENOR_5Y
        A tenor of 5 years.
      • TENOR_6Y

        public static final Tenor TENOR_6Y
        A tenor of 6 years.
      • TENOR_7Y

        public static final Tenor TENOR_7Y
        A tenor of 7 years.
      • TENOR_8Y

        public static final Tenor TENOR_8Y
        A tenor of 8 years.
      • TENOR_9Y

        public static final Tenor TENOR_9Y
        A tenor of 9 years.
      • TENOR_10Y

        public static final Tenor TENOR_10Y
        A tenor of 10 years.
      • TENOR_11Y

        public static final Tenor TENOR_11Y
        A tenor of 11 years.
      • TENOR_12Y

        public static final Tenor TENOR_12Y
        A tenor of 12 years.
      • TENOR_13Y

        public static final Tenor TENOR_13Y
        A tenor of 13 years.
      • TENOR_14Y

        public static final Tenor TENOR_14Y
        A tenor of 14 years.
      • TENOR_15Y

        public static final Tenor TENOR_15Y
        A tenor of 15 years.
      • TENOR_20Y

        public static final Tenor TENOR_20Y
        A tenor of 20 years.
      • TENOR_25Y

        public static final Tenor TENOR_25Y
        A tenor of 25 years.
      • TENOR_30Y

        public static final Tenor TENOR_30Y
        A tenor of 30 years.
      • TENOR_35Y

        public static final Tenor TENOR_35Y
        A tenor of 35 years.
      • TENOR_40Y

        public static final Tenor TENOR_40Y
        A tenor of 40 years.
      • TENOR_45Y

        public static final Tenor TENOR_45Y
        A tenor of 45 years.
      • TENOR_50Y

        public static final Tenor TENOR_50Y
        A tenor of 50 years.
    • Method Detail

      • of

        public static Tenor of​(java.time.Period period)
        Obtains an instance from a Period.

        The period normally consists of either days and weeks, or months and years. It must also be positive and non-zero.

        If the number of days is an exact multiple of 7 it will be converted to weeks. Months are not normalized into years.

        Parameters:
        period - the period to convert to a tenor
        Returns:
        the tenor
        Throws:
        java.lang.IllegalArgumentException - if the period is negative or zero
      • ofDays

        public static Tenor ofDays​(int days)
        Obtains an instance backed by a period of days.

        If the number of days is an exact multiple of 7 it will be converted to weeks.

        Parameters:
        days - the number of days
        Returns:
        the tenor
        Throws:
        java.lang.IllegalArgumentException - if days is negative or zero
      • ofWeeks

        public static Tenor ofWeeks​(int weeks)
        Obtains an instance backed by a period of weeks.
        Parameters:
        weeks - the number of weeks
        Returns:
        the tenor
        Throws:
        java.lang.IllegalArgumentException - if weeks is negative or zero
      • ofMonths

        public static Tenor ofMonths​(int months)
        Obtains an instance backed by a period of months.

        Months are not normalized into years.

        Parameters:
        months - the number of months
        Returns:
        the tenor
        Throws:
        java.lang.IllegalArgumentException - if months is negative or zero
      • ofYears

        public static Tenor ofYears​(int years)
        Obtains an instance backed by a period of years.
        Parameters:
        years - the number of years
        Returns:
        the tenor
        Throws:
        java.lang.IllegalArgumentException - if years is negative or zero
      • parse

        public static Tenor parse​(java.lang.String toParse)
        Parses a formatted string representing the tenor.

        The format can either be based on ISO-8601, such as 'P3M' or without the 'P' prefix e.g. '2W'.

        Parameters:
        toParse - the string representing the tenor
        Returns:
        the tenor
        Throws:
        java.lang.IllegalArgumentException - if the tenor cannot be parsed
      • getPeriod

        public java.time.Period getPeriod()
        Gets the underlying period of the tenor.
        Returns:
        the period
      • normalized

        public Tenor normalized()
        Normalizes the months and years of this tenor.

        This method returns a normalized tenor of an equivalent length. If the period is exactly 1 year then the result will be expressed as 12 months. Otherwise, the result will be expressed using Period.normalized().

        Returns:
        the normalized tenor
      • isWeekBased

        public boolean isWeekBased()
        Checks if the tenor is week-based.

        A week-based tenor consists of an integral number of weeks. There must be no day, month or year element.

        Returns:
        true if this is week-based
      • isMonthBased

        public boolean isMonthBased()
        Checks if the tenor is month-based.

        A month-based tenor consists of an integral number of months. Any year-based tenor is also counted as month-based. There must be no day or week element.

        Returns:
        true if this is month-based
      • get

        public long get​(java.time.temporal.TemporalUnit unit)
        Gets the value of the specified unit.

        This will return a value for the years, months and days units. Note that weeks are not included. All other units throw an exception.

        This method implements TemporalAmount. It is not intended to be called directly.

        Specified by:
        get in interface java.time.temporal.TemporalAmount
        Parameters:
        unit - the unit to query
        Returns:
        the value of the unit
        Throws:
        java.time.temporal.UnsupportedTemporalTypeException - if the unit is not supported
      • getUnits

        public java.util.List<java.time.temporal.TemporalUnit> getUnits()
        Gets the units supported by a tenor.

        This returns a list containing years, months and days. Note that weeks are not included.

        This method implements TemporalAmount. It is not intended to be called directly.

        Specified by:
        getUnits in interface java.time.temporal.TemporalAmount
        Returns:
        a list containing the years, months and days units
      • addTo

        public java.time.temporal.Temporal addTo​(java.time.temporal.Temporal temporal)
        Adds this tenor to the specified date.

        This method implements TemporalAmount. It is not intended to be called directly. Use LocalDate.plus(TemporalAmount) instead.

        Specified by:
        addTo in interface java.time.temporal.TemporalAmount
        Parameters:
        temporal - the temporal object to add to
        Returns:
        the result with this tenor added
        Throws:
        java.time.DateTimeException - if unable to add
        java.lang.ArithmeticException - if numeric overflow occurs
      • subtractFrom

        public java.time.temporal.Temporal subtractFrom​(java.time.temporal.Temporal temporal)
        Subtracts this tenor from the specified date.

        This method implements TemporalAmount. It is not intended to be called directly. Use LocalDate.minus(TemporalAmount) instead.

        Specified by:
        subtractFrom in interface java.time.temporal.TemporalAmount
        Parameters:
        temporal - the temporal object to subtract from
        Returns:
        the result with this tenor subtracted
        Throws:
        java.time.DateTimeException - if unable to subtract
        java.lang.ArithmeticException - if numeric overflow occurs
      • compareTo

        public int compareTo​(Tenor other)
        Compares this tenor to another tenor.

        Comparing tenors is a hard problem in general, but for commonly used tenors the outcome is as expected. If the two tenors are both based on days, then comparison is easy. If the two tenors are both based on months/years, then comparison is easy. Otherwise, months are converted to days to form an estimated length in days which is compared. The conversion from months to days divides by 12 and then multiplies by 365.25.

        The resulting order places:

        • a 1 month tenor between 30 and 31 days
        • a 2 month tenor between 60 and 61 days
        • a 3 month tenor between 91 and 92 days
        • a 6 month tenor between 182 and 183 days
        • a 1 year tenor between 365 and 366 days
        Specified by:
        compareTo in interface java.lang.Comparable<Tenor>
        Parameters:
        other - the other tenor
        Returns:
        negative if this is less than the other, zero if equal and positive if greater
      • equals

        public boolean equals​(java.lang.Object obj)
        Checks if this tenor equals another tenor.

        The comparison checks the tenor period.

        Overrides:
        equals in class java.lang.Object
        Parameters:
        obj - the other tenor, null returns false
        Returns:
        true if equal
      • hashCode

        public int hashCode()
        Returns a suitable hash code for the tenor.
        Overrides:
        hashCode in class java.lang.Object
        Returns:
        the hash code
      • toString

        public java.lang.String toString()
        Returns a formatted string representing the tenor.

        The format is a combination of the quantity and unit, such as 1D, 2W, 3M, 4Y.

        Overrides:
        toString in class java.lang.Object
        Returns:
        the formatted tenor