Class ChronoPeriodImpl

java.lang.Object
org.threeten.bp.chrono.ChronoPeriod
org.threeten.bp.chrono.ChronoPeriodImpl
All Implemented Interfaces:
Serializable, TemporalAmount

final class ChronoPeriodImpl extends ChronoPeriod implements Serializable
An implementation of ChronoPeriod.
  • Field Details

    • serialVersionUID

      private static final long serialVersionUID
      Serialization version.
      See Also:
    • chronology

      private final Chronology chronology
    • years

      private final int years
    • months

      private final int months
    • days

      private final int days
  • Constructor Details

    • ChronoPeriodImpl

      public ChronoPeriodImpl(Chronology chronology, int years, int months, int days)
  • Method Details

    • get

      public long get(TemporalUnit unit)
      Description copied from class: ChronoPeriod
      Gets the value of the requested unit.

      The supported units are chronology specific. They will typically be YEARS, MONTHS and DAYS. Requesting an unsupported unit will throw an exception.

      Specified by:
      get in interface TemporalAmount
      Specified by:
      get in class ChronoPeriod
      Parameters:
      unit - the TemporalUnit for which to return the value
      Returns:
      the long value of the unit
    • getUnits

      public List<TemporalUnit> getUnits()
      Description copied from class: ChronoPeriod
      Gets the set of units supported by this period.

      The supported units are chronology specific. They will typically be YEARS, MONTHS and DAYS. They are returned in order from largest to smallest.

      This set can be used in conjunction with ChronoPeriod.get(TemporalUnit) to access the entire state of the period.

      Specified by:
      getUnits in interface TemporalAmount
      Specified by:
      getUnits in class ChronoPeriod
      Returns:
      a list containing the supported units, not null
    • getChronology

      public Chronology getChronology()
      Description copied from class: ChronoPeriod
      Gets the chronology that defines the meaning of the supported units.

      The period is defined by the chronology. It controls the supported units and restricts addition/subtraction to ChronoLocalDate instances of the same chronology.

      Specified by:
      getChronology in class ChronoPeriod
      Returns:
      the chronology defining the period, not null
    • plus

      public ChronoPeriod plus(TemporalAmount amountToAdd)
      Description copied from class: ChronoPeriod
      Returns a copy of this period with the specified period added.

      If the specified amount is a ChronoPeriod then it must have the same chronology as this period. Implementations may choose to accept or reject other TemporalAmount implementations.

      This instance is immutable and unaffected by this method call.

      Specified by:
      plus in class ChronoPeriod
      Parameters:
      amountToAdd - the period to add, not null
      Returns:
      a ChronoPeriod based on this period with the requested period added, not null
    • minus

      public ChronoPeriod minus(TemporalAmount amountToSubtract)
      Description copied from class: ChronoPeriod
      Returns a copy of this period with the specified period subtracted.

      If the specified amount is a ChronoPeriod then it must have the same chronology as this period. Implementations may choose to accept or reject other TemporalAmount implementations.

      This instance is immutable and unaffected by this method call.

      Specified by:
      minus in class ChronoPeriod
      Parameters:
      amountToSubtract - the period to subtract, not null
      Returns:
      a ChronoPeriod based on this period with the requested period subtracted, not null
    • multipliedBy

      public ChronoPeriod multipliedBy(int scalar)
      Description copied from class: ChronoPeriod
      Returns a new instance with each amount in this period in this period multiplied by the specified scalar.

      This returns a period with each supported unit individually multiplied. For example, a period of "2 years, -3 months and 4 days" multiplied by 3 will return "6 years, -9 months and 12 days". No normalization is performed.

      Specified by:
      multipliedBy in class ChronoPeriod
      Parameters:
      scalar - the scalar to multiply by, not null
      Returns:
      a ChronoPeriod based on this period with the amounts multiplied by the scalar, not null
    • normalized

      public ChronoPeriod normalized()
      Description copied from class: ChronoPeriod
      Returns a copy of this period with the amounts of each unit normalized.

      The process of normalization is specific to each calendar system. For example, in the ISO calendar system, the years and months are normalized but the days are not, such that "15 months" would be normalized to "1 year and 3 months".

      This instance is immutable and unaffected by this method call.

      Specified by:
      normalized in class ChronoPeriod
      Returns:
      a ChronoPeriod based on this period with the amounts of each unit normalized, not null
    • addTo

      public Temporal addTo(Temporal temporal)
      Description copied from class: ChronoPeriod
      Adds this period to the specified temporal object.

      This returns a temporal object of the same observable type as the input with this period added.

      In most cases, it is clearer to reverse the calling pattern by using Temporal.plus(TemporalAmount).

         // these two lines are equivalent, but the second approach is recommended
         dateTime = thisPeriod.addTo(dateTime);
         dateTime = dateTime.plus(thisPeriod);
       

      The specified temporal must have the same chronology as this period. This returns a temporal with the non-zero supported units added.

      This instance is immutable and unaffected by this method call.

      Specified by:
      addTo in interface TemporalAmount
      Specified by:
      addTo in class ChronoPeriod
      Parameters:
      temporal - the temporal object to adjust, not null
      Returns:
      an object of the same type with the adjustment made, not null
    • subtractFrom

      public Temporal subtractFrom(Temporal temporal)
      Description copied from class: ChronoPeriod
      Subtracts this period from the specified temporal object.

      This returns a temporal object of the same observable type as the input with this period subtracted.

      In most cases, it is clearer to reverse the calling pattern by using Temporal.minus(TemporalAmount).

         // these two lines are equivalent, but the second approach is recommended
         dateTime = thisPeriod.subtractFrom(dateTime);
         dateTime = dateTime.minus(thisPeriod);
       

      The specified temporal must have the same chronology as this period. This returns a temporal with the non-zero supported units subtracted.

      This instance is immutable and unaffected by this method call.

      Specified by:
      subtractFrom in interface TemporalAmount
      Specified by:
      subtractFrom in class ChronoPeriod
      Parameters:
      temporal - the temporal object to adjust, not null
      Returns:
      an object of the same type with the adjustment made, not null
    • equals

      public boolean equals(Object obj)
      Description copied from class: ChronoPeriod
      Checks if this period is equal to another period, including the chronology.

      Compares this period with another ensuring that the type, each amount and the chronology are the same. Note that this means that a period of "15 Months" is not equal to a period of "1 Year and 3 Months".

      Specified by:
      equals in class ChronoPeriod
      Parameters:
      obj - the object to check, null returns false
      Returns:
      true if this is equal to the other period
    • hashCode

      public int hashCode()
      Description copied from class: ChronoPeriod
      A hash code for this period.
      Specified by:
      hashCode in class ChronoPeriod
      Returns:
      a suitable hash code
    • toString

      public String toString()
      Description copied from class: ChronoPeriod
      Outputs this period as a String.

      The output will include the period amounts and chronology.

      Specified by:
      toString in class ChronoPeriod
      Returns:
      a string representation of this period, not null