71 import static java.time.temporal.ChronoUnit.DAYS;
72 import static java.time.temporal.ChronoUnit.NANOS;
73 import static java.time.temporal.ChronoUnit.SECONDS;
74
75 import java.io.DataInput;
76 import java.io.DataOutput;
77 import java.io.IOException;
78 import java.io.InvalidObjectException;
79 import java.io.ObjectInputStream;
80 import java.io.Serializable;
81 import java.math.BigDecimal;
82 import java.math.BigInteger;
83 import java.math.RoundingMode;
84 import java.time.format.DateTimeParseException;
85 import java.time.temporal.ChronoField;
86 import java.time.temporal.ChronoUnit;
87 import java.time.temporal.Temporal;
88 import java.time.temporal.TemporalAmount;
89 import java.time.temporal.TemporalUnit;
90 import java.time.temporal.UnsupportedTemporalTypeException;
91 import java.util.Arrays;
92 import java.util.Collections;
93 import java.util.List;
94 import java.util.Objects;
95 import java.util.regex.Matcher;
96 import java.util.regex.Pattern;
97
98 /**
99 * A time-based amount of time, such as '34.5 seconds'.
100 * <p>
101 * This class models a quantity or amount of time in terms of seconds and nanoseconds.
102 * It can be accessed using other duration-based units, such as minutes and hours.
103 * In addition, the {@link ChronoUnit#DAYS DAYS} unit can be used and is treated as
104 * exactly equal to 24 hours, thus ignoring daylight savings effects.
105 * See {@link Period} for the date-based equivalent to this class.
106 * <p>
107 * A physical duration could be of infinite length.
108 * For practicality, the duration is stored with constraints similar to {@link Instant}.
109 * The duration uses nanosecond resolution with a maximum value of the seconds that can
110 * be held in a {@code long}. This is greater than the current estimated age of the universe.
111 * <p>
112 * The range of a duration requires the storage of a number larger than a {@code long}.
561 * The supported units are {@link ChronoUnit#SECONDS SECONDS},
562 * and {@link ChronoUnit#NANOS NANOS}.
563 * They are returned in the order seconds, nanos.
564 * <p>
565 * This set can be used in conjunction with {@link #get(TemporalUnit)}
566 * to access the entire state of the duration.
567 *
568 * @return a list containing the seconds and nanos units, not null
569 */
570 @Override
571 public List<TemporalUnit> getUnits() {
572 return DurationUnits.UNITS;
573 }
574
575 /**
576 * Private class to delay initialization of this list until needed.
577 * The circular dependency between Duration and ChronoUnit prevents
578 * the simple initialization in Duration.
579 */
580 private static class DurationUnits {
581 static final List<TemporalUnit> UNITS =
582 Collections.unmodifiableList(Arrays.<TemporalUnit>asList(SECONDS, NANOS));
583 }
584
585 //-----------------------------------------------------------------------
586 /**
587 * Checks if this duration is zero length.
588 * <p>
589 * A {@code Duration} represents a directed distance between two points on
590 * the time-line and can therefore be positive, zero or negative.
591 * This method checks whether the length is zero.
592 *
593 * @return true if this duration has a total length equal to zero
594 */
595 public boolean isZero() {
596 return (seconds | nanos) == 0;
597 }
598
599 /**
600 * Checks if this duration is negative, excluding zero.
601 * <p>
602 * A {@code Duration} represents a directed distance between two points on
|
71 import static java.time.temporal.ChronoUnit.DAYS;
72 import static java.time.temporal.ChronoUnit.NANOS;
73 import static java.time.temporal.ChronoUnit.SECONDS;
74
75 import java.io.DataInput;
76 import java.io.DataOutput;
77 import java.io.IOException;
78 import java.io.InvalidObjectException;
79 import java.io.ObjectInputStream;
80 import java.io.Serializable;
81 import java.math.BigDecimal;
82 import java.math.BigInteger;
83 import java.math.RoundingMode;
84 import java.time.format.DateTimeParseException;
85 import java.time.temporal.ChronoField;
86 import java.time.temporal.ChronoUnit;
87 import java.time.temporal.Temporal;
88 import java.time.temporal.TemporalAmount;
89 import java.time.temporal.TemporalUnit;
90 import java.time.temporal.UnsupportedTemporalTypeException;
91 import java.util.List;
92 import java.util.Objects;
93 import java.util.regex.Matcher;
94 import java.util.regex.Pattern;
95
96 /**
97 * A time-based amount of time, such as '34.5 seconds'.
98 * <p>
99 * This class models a quantity or amount of time in terms of seconds and nanoseconds.
100 * It can be accessed using other duration-based units, such as minutes and hours.
101 * In addition, the {@link ChronoUnit#DAYS DAYS} unit can be used and is treated as
102 * exactly equal to 24 hours, thus ignoring daylight savings effects.
103 * See {@link Period} for the date-based equivalent to this class.
104 * <p>
105 * A physical duration could be of infinite length.
106 * For practicality, the duration is stored with constraints similar to {@link Instant}.
107 * The duration uses nanosecond resolution with a maximum value of the seconds that can
108 * be held in a {@code long}. This is greater than the current estimated age of the universe.
109 * <p>
110 * The range of a duration requires the storage of a number larger than a {@code long}.
559 * The supported units are {@link ChronoUnit#SECONDS SECONDS},
560 * and {@link ChronoUnit#NANOS NANOS}.
561 * They are returned in the order seconds, nanos.
562 * <p>
563 * This set can be used in conjunction with {@link #get(TemporalUnit)}
564 * to access the entire state of the duration.
565 *
566 * @return a list containing the seconds and nanos units, not null
567 */
568 @Override
569 public List<TemporalUnit> getUnits() {
570 return DurationUnits.UNITS;
571 }
572
573 /**
574 * Private class to delay initialization of this list until needed.
575 * The circular dependency between Duration and ChronoUnit prevents
576 * the simple initialization in Duration.
577 */
578 private static class DurationUnits {
579 static final List<TemporalUnit> UNITS = List.of(SECONDS, NANOS);
580 }
581
582 //-----------------------------------------------------------------------
583 /**
584 * Checks if this duration is zero length.
585 * <p>
586 * A {@code Duration} represents a directed distance between two points on
587 * the time-line and can therefore be positive, zero or negative.
588 * This method checks whether the length is zero.
589 *
590 * @return true if this duration has a total length equal to zero
591 */
592 public boolean isZero() {
593 return (seconds | nanos) == 0;
594 }
595
596 /**
597 * Checks if this duration is negative, excluding zero.
598 * <p>
599 * A {@code Duration} represents a directed distance between two points on
|