Record Class NaftahDuration

java.lang.Object
java.lang.Record
org.daiitech.naftah.builtin.time.NaftahDuration
Record Components:
durationDefinition - the definition of the duration in Arabic text
temporalAmount - the actual duration value
All Implemented Interfaces:
NaftahTemporal, NaftahTemporalAmount

public record NaftahDuration(NaftahDuration.DurationDefinition durationDefinition, Duration temporalAmount) extends Record implements NaftahTemporalAmount
Represents a duration expressed in Arabic text, including hours, minutes, seconds, milliseconds, and nanoseconds.

This class wraps a NaftahDuration.DurationDefinition for formatting purposes and a Duration for the actual temporal amount.

Author:
Chakib Daii
  • Field Details

  • Constructor Details

  • Method Details

    • of

      public static NaftahDuration of(NaftahDuration.DurationDefinition durationDefinition, Duration temporalAmount)
      Creates a new NaftahDuration instance using the provided Arabic duration definition and the underlying Duration.
      Parameters:
      durationDefinition - the Arabic textual definition describing the duration (hours, minutes, seconds, etc.)
      temporalAmount - the underlying Duration representing the actual time-based amount
      Returns:
      a new NaftahDuration instance
    • of

      public static NaftahDuration of(Duration duration)
      Creates a new NaftahDuration instance from a Duration.

      The duration is decomposed into its time-based components (hours, minutes, seconds, milliseconds, and nanoseconds) to build the corresponding Arabic textual representation.

      Parameters:
      duration - the Duration to convert into an NaftahDuration
      Returns:
      a new NaftahDuration instance representing the given duration
    • ofZero

      public static NaftahDuration ofZero()
      Returns an NaftahDuration representing a zero duration.

      The returned duration has all components set to zero: hours, minutes, seconds, milliseconds, and nanoseconds.

      Returns:
      an NaftahDuration representing a duration of zero
    • ofDays

      public static NaftahDuration ofDays(long days)
      Obtains a ArabicDuration representing a number of standard 24 hour days.

      The seconds are calculated based on the standard definition of a day, where each day is 86400 seconds which implies a 24 hour day. The nanosecond in second field is set to zero.

      Parameters:
      days - the number of days, positive or negative
      Returns:
      a ArabicDuration, not null
      Throws:
      ArithmeticException - if the input days exceeds the capacity of Duration
    • ofHours

      public static NaftahDuration ofHours(long hours)
      Obtains a ArabicDuration representing a number of standard hours.

      The seconds are calculated based on the standard definition of an hour, where each hour is 3600 seconds. The nanosecond in second field is set to zero.

      Parameters:
      hours - the number of hours, positive or negative
      Returns:
      a ArabicDuration, not null
      Throws:
      ArithmeticException - if the input hours exceeds the capacity of Duration
    • ofMinutes

      public static NaftahDuration ofMinutes(long minutes)
      Obtains a ArabicDuration representing a number of standard minutes.

      The seconds are calculated based on the standard definition of a minute, where each minute is 60 seconds. The nanosecond in second field is set to zero.

      Parameters:
      minutes - the number of minutes, positive or negative
      Returns:
      a ArabicDuration, not null
      Throws:
      ArithmeticException - if the input minutes exceeds the capacity of Duration
    • ofSeconds

      public static NaftahDuration ofSeconds(long seconds)
      Obtains a ArabicDuration representing a number of seconds.

      The nanosecond in second field is set to zero.

      Parameters:
      seconds - the number of seconds, positive or negative
      Returns:
      a ArabicDuration, not null
    • ofSeconds

      public static NaftahDuration ofSeconds(long seconds, long nanoAdjustment)
      Obtains a ArabicDuration representing a number of seconds and an adjustment in nanoseconds.

      This method allows an arbitrary number of nanoseconds to be passed in. The factory will alter the values of the second and nanosecond in order to ensure that the stored nanosecond is in the range 0 to 999,999,999. For example, the following will result in exactly the same duration:

       Duration.ofSeconds(3, 1);
       Duration.ofSeconds(4, -999_999_999);
       Duration.ofSeconds(2, 1000_000_001);
       
      Parameters:
      seconds - the number of seconds, positive or negative
      nanoAdjustment - the nanosecond adjustment to the number of seconds, positive or negative
      Returns:
      a ArabicDuration, not null
      Throws:
      ArithmeticException - if the adjustment causes the seconds to exceed the capacity of Duration
    • ofMillis

      public static NaftahDuration ofMillis(long millis)
      Obtains a ArabicDuration representing a number of milliseconds.

      The seconds and nanoseconds are extracted from the specified milliseconds.

      Parameters:
      millis - the number of milliseconds, positive or negative
      Returns:
      a ArabicDuration, not null
    • ofNanos

      public static NaftahDuration ofNanos(long nanos)
      Obtains a ArabicDuration representing a number of nanoseconds.

      The seconds and nanoseconds are extracted from the specified nanoseconds.

      Parameters:
      nanos - the number of nanoseconds, positive or negative
      Returns:
      a ArabicDuration, not null
    • getHours

      public long getHours()
      Returns the total number of hours in this duration.
      Returns:
      the total hours
    • getMinutes

      public long getMinutes()
      Returns the minute-of-hour component of this duration.

      This is calculated as the total minutes modulo 60, representing the remaining minutes after full hours are accounted for.

      Returns:
      the minutes within the current hour (0-59)
    • getSeconds

      public long getSeconds()
      Returns the second-of-minute component of this duration.

      This is calculated as the total seconds modulo 60, representing the remaining seconds after full minutes are accounted for.

      Returns:
      the seconds within the current minute (0-59)
    • getMillis

      public long getMillis()
      Returns the millisecond-of-second component of this duration.

      This is calculated as the total milliseconds modulo 1000, representing the remaining milliseconds after full seconds are accounted for.

      Returns:
      the milliseconds within the current second (0-999)
    • getNano

      public int getNano()
      Returns the nanosecond-of-second component of this duration.
      Returns:
      the nanoseconds within the current second (0-999,999,999)
    • plus

      public NaftahDuration plus(NaftahTemporalAmount other)
      Returns a new ArabicDuration obtained by adding the given Arabic temporal amount to this duration.

      Addition is supported only when the other amount is also an ArabicDuration.

      Specified by:
      plus in interface NaftahTemporalAmount
      Parameters:
      other - the temporal amount to add
      Returns:
      a new ArabicDuration instance
    • plusDays

      public NaftahDuration plusDays(long daysToAdd)
      Returns a copy of this duration with the specified duration in standard 24 hour days added.

      The number of days is multiplied by 86400 to obtain the number of seconds to add. This is based on the standard definition of a day as 24 hours.

      This instance is immutable and unaffected by this method call.

      Parameters:
      daysToAdd - the days to add, positive or negative
      Returns:
      a ArabicDuration based on this duration with the specified days added, not null
      Throws:
      ArithmeticException - if numeric overflow occurs
    • plusHours

      public NaftahDuration plusHours(long hoursToAdd)
      Returns a copy of this duration with the specified duration in hours added.

      This instance is immutable and unaffected by this method call.

      Parameters:
      hoursToAdd - the hours to add, positive or negative
      Returns:
      a ArabicDuration based on this duration with the specified hours added, not null
      Throws:
      ArithmeticException - if numeric overflow occurs
    • plusMinutes

      public NaftahDuration plusMinutes(long minutesToAdd)
      Returns a copy of this duration with the specified duration in minutes added.

      This instance is immutable and unaffected by this method call.

      Parameters:
      minutesToAdd - the minutes to add, positive or negative
      Returns:
      a ArabicDuration based on this duration with the specified minutes added, not null
      Throws:
      ArithmeticException - if numeric overflow occurs
    • plusSeconds

      public NaftahDuration plusSeconds(long secondsToAdd)
      Returns a copy of this duration with the specified duration in seconds added.

      This instance is immutable and unaffected by this method call.

      Parameters:
      secondsToAdd - the seconds to add, positive or negative
      Returns:
      a ArabicDuration based on this duration with the specified seconds added, not null
      Throws:
      ArithmeticException - if numeric overflow occurs
    • plusMillis

      public NaftahDuration plusMillis(long millisToAdd)
      Returns a copy of this duration with the specified duration in milliseconds added.

      This instance is immutable and unaffected by this method call.

      Parameters:
      millisToAdd - the milliseconds to add, positive or negative
      Returns:
      a ArabicDuration based on this duration with the specified milliseconds added, not null
      Throws:
      ArithmeticException - if numeric overflow occurs
    • plusNanos

      public NaftahDuration plusNanos(long nanosToAdd)
      Returns a copy of this duration with the specified duration in nanoseconds added.

      This instance is immutable and unaffected by this method call.

      Parameters:
      nanosToAdd - the nanoseconds to add, positive or negative
      Returns:
      a ArabicDuration based on this duration with the specified nanoseconds added, not null
      Throws:
      ArithmeticException - if numeric overflow occurs
    • minus

      public NaftahDuration minus(NaftahTemporalAmount other)
      Returns a new ArabicDuration obtained by subtracting the given Arabic temporal amount from this duration.

      Subtraction is supported only when the other amount is also an ArabicDuration.

      Specified by:
      minus in interface NaftahTemporalAmount
      Parameters:
      other - the temporal amount to subtract
      Returns:
      a new ArabicDuration instance
    • minusDays

      public NaftahDuration minusDays(long daysToSubtract)
      Returns a copy of this duration with the specified duration in standard 24 hour days subtracted.

      The number of days is multiplied by 86400 to obtain the number of seconds to subtract. This is based on the standard definition of a day as 24 hours.

      This instance is immutable and unaffected by this method call.

      Parameters:
      daysToSubtract - the days to subtract, positive or negative
      Returns:
      a ArabicDuration based on this duration with the specified days subtracted, not null
      Throws:
      ArithmeticException - if numeric overflow occurs
    • minusHours

      public NaftahDuration minusHours(long hoursToSubtract)
      Returns a copy of this duration with the specified duration in hours subtracted.

      The number of hours is multiplied by 3600 to obtain the number of seconds to subtract.

      This instance is immutable and unaffected by this method call.

      Parameters:
      hoursToSubtract - the hours to subtract, positive or negative
      Returns:
      a ArabicDuration based on this duration with the specified hours subtracted, not null
      Throws:
      ArithmeticException - if numeric overflow occurs
    • minusMinutes

      public NaftahDuration minusMinutes(long minutesToSubtract)
      Returns a copy of this duration with the specified duration in minutes subtracted.

      The number of hours is multiplied by 60 to obtain the number of seconds to subtract.

      This instance is immutable and unaffected by this method call.

      Parameters:
      minutesToSubtract - the minutes to subtract, positive or negative
      Returns:
      a ArabicDuration based on this duration with the specified minutes subtracted, not null
      Throws:
      ArithmeticException - if numeric overflow occurs
    • minusSeconds

      public NaftahDuration minusSeconds(long secondsToSubtract)
      Returns a copy of this duration with the specified duration in seconds subtracted.

      This instance is immutable and unaffected by this method call.

      Parameters:
      secondsToSubtract - the seconds to subtract, positive or negative
      Returns:
      a ArabicDuration based on this duration with the specified seconds subtracted, not null
      Throws:
      ArithmeticException - if numeric overflow occurs
    • minusMillis

      public NaftahDuration minusMillis(long millisToSubtract)
      Returns a copy of this duration with the specified duration in milliseconds subtracted.

      This instance is immutable and unaffected by this method call.

      Parameters:
      millisToSubtract - the milliseconds to subtract, positive or negative
      Returns:
      a ArabicDuration based on this duration with the specified milliseconds subtracted, not null
      Throws:
      ArithmeticException - if numeric overflow occurs
    • minusNanos

      public NaftahDuration minusNanos(long nanosToSubtract)
      Returns a copy of this duration with the specified duration in nanoseconds subtracted.

      This instance is immutable and unaffected by this method call.

      Parameters:
      nanosToSubtract - the nanoseconds to subtract, positive or negative
      Returns:
      a ArabicDuration based on this duration with the specified nanoseconds subtracted, not null
      Throws:
      ArithmeticException - if numeric overflow occurs
    • isEquals

      public boolean isEquals(NaftahTemporalAmount other)
      Determines whether this duration is equal to the given temporal amount.
      Specified by:
      isEquals in interface NaftahTemporalAmount
      Parameters:
      other - the temporal amount to compare with
      Returns:
      true if the amounts are equal; false otherwise
    • notEquals

      public boolean notEquals(NaftahTemporalAmount other)
      Determines whether this duration is not equal to the given temporal amount.
      Specified by:
      notEquals in interface NaftahTemporalAmount
      Parameters:
      other - the temporal amount to compare with
      Returns:
      true if the amounts are not equal; false otherwise
    • greaterThan

      public boolean greaterThan(NaftahTemporalAmount other)
      Determines whether this duration is greater than the given temporal amount.
      Specified by:
      greaterThan in interface NaftahTemporalAmount
      Parameters:
      other - the temporal amount to compare with
      Returns:
      true if this duration is greater
    • greaterThanEquals

      public boolean greaterThanEquals(NaftahTemporalAmount other)
      Determines whether this duration is greater than or equal to the given temporal amount.
      Specified by:
      greaterThanEquals in interface NaftahTemporalAmount
      Parameters:
      other - the temporal amount to compare with
      Returns:
      true if this duration is greater than or equal
    • lessThan

      public boolean lessThan(NaftahTemporalAmount other)
      Determines whether this duration is less than the given temporal amount.
      Specified by:
      lessThan in interface NaftahTemporalAmount
      Parameters:
      other - the temporal amount to compare with
      Returns:
      true if this duration is less
    • lessThanEquals

      public boolean lessThanEquals(NaftahTemporalAmount other)
      Determines whether this duration is less than or equal to the given temporal amount.
      Specified by:
      lessThanEquals in interface NaftahTemporalAmount
      Parameters:
      other - the temporal amount to compare with
      Returns:
      true if this duration is less than or equal
    • toString

      public String toString()
      Returns the Arabic textual representation of the duration.
      Specified by:
      toString in class Record
      Returns:
      a string describing the duration in Arabic
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. All components in this record class are compared with Objects::equals(Object,Object).
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • durationDefinition

      public NaftahDuration.DurationDefinition durationDefinition()
      Returns the value of the durationDefinition record component.
      Returns:
      the value of the durationDefinition record component
    • temporalAmount

      public Duration temporalAmount()
      Returns the value of the temporalAmount record component.
      Specified by:
      temporalAmount in interface NaftahTemporalAmount
      Returns:
      the value of the temporalAmount record component