123 * class; use of identity-sensitive operations (including reference equality
124 * ({@code ==}), identity hash code, or synchronization) on instances of
125 * {@code Duration} may have unpredictable results and should be avoided.
126 * The {@code equals} method should be used for comparisons.
127 *
128 * @implSpec
129 * This class is immutable and thread-safe.
130 *
131 * @since 1.8
132 */
133 public final class Duration
134 implements TemporalAmount, Comparable<Duration>, Serializable {
135
136 /**
137 * Constant for a duration of zero.
138 */
139 public static final Duration ZERO = new Duration(0, 0);
140 /**
141 * Serialization version.
142 */
143 private static final long serialVersionUID = 3078945930695997490L;
144 /**
145 * Constant for nanos per second.
146 */
147 private static final BigInteger BI_NANOS_PER_SECOND = BigInteger.valueOf(NANOS_PER_SECOND);
148 /**
149 * The pattern for parsing.
150 */
151 private static class Lazy {
152 static final Pattern PATTERN =
153 Pattern.compile("([-+]?)P(?:([-+]?[0-9]+)D)?" +
154 "(T(?:([-+]?[0-9]+)H)?(?:([-+]?[0-9]+)M)?(?:([-+]?[0-9]+)(?:[.,]([0-9]{0,9}))?S)?)?",
155 Pattern.CASE_INSENSITIVE);
156 }
157
158 /**
159 * The number of seconds in the duration.
160 */
161 private final long seconds;
162 /**
1512 }
1513 buf.setCharAt(pos, '.');
1514 }
1515 buf.append('S');
1516 return buf.toString();
1517 }
1518
1519 //-----------------------------------------------------------------------
1520 /**
1521 * Writes the object using a
1522 * <a href="{@docRoot}/serialized-form.html#java.time.Ser">dedicated serialized form</a>.
1523 * @serialData
1524 * <pre>
1525 * out.writeByte(1); // identifies a Duration
1526 * out.writeLong(seconds);
1527 * out.writeInt(nanos);
1528 * </pre>
1529 *
1530 * @return the instance of {@code Ser}, not null
1531 */
1532 private Object writeReplace() {
1533 return new Ser(Ser.DURATION_TYPE, this);
1534 }
1535
1536 /**
1537 * Defend against malicious streams.
1538 *
1539 * @param s the stream to read
1540 * @throws InvalidObjectException always
1541 */
1542 private void readObject(ObjectInputStream s) throws InvalidObjectException {
1543 throw new InvalidObjectException("Deserialization via serialization delegate");
1544 }
1545
1546 void writeExternal(DataOutput out) throws IOException {
1547 out.writeLong(seconds);
1548 out.writeInt(nanos);
1549 }
1550
1551 static Duration readExternal(DataInput in) throws IOException {
1552 long seconds = in.readLong();
1553 int nanos = in.readInt();
1554 return Duration.ofSeconds(seconds, nanos);
1555 }
1556
1557 }
|
123 * class; use of identity-sensitive operations (including reference equality
124 * ({@code ==}), identity hash code, or synchronization) on instances of
125 * {@code Duration} may have unpredictable results and should be avoided.
126 * The {@code equals} method should be used for comparisons.
127 *
128 * @implSpec
129 * This class is immutable and thread-safe.
130 *
131 * @since 1.8
132 */
133 public final class Duration
134 implements TemporalAmount, Comparable<Duration>, Serializable {
135
136 /**
137 * Constant for a duration of zero.
138 */
139 public static final Duration ZERO = new Duration(0, 0);
140 /**
141 * Serialization version.
142 */
143 @java.io.Serial
144 private static final long serialVersionUID = 3078945930695997490L;
145 /**
146 * Constant for nanos per second.
147 */
148 private static final BigInteger BI_NANOS_PER_SECOND = BigInteger.valueOf(NANOS_PER_SECOND);
149 /**
150 * The pattern for parsing.
151 */
152 private static class Lazy {
153 static final Pattern PATTERN =
154 Pattern.compile("([-+]?)P(?:([-+]?[0-9]+)D)?" +
155 "(T(?:([-+]?[0-9]+)H)?(?:([-+]?[0-9]+)M)?(?:([-+]?[0-9]+)(?:[.,]([0-9]{0,9}))?S)?)?",
156 Pattern.CASE_INSENSITIVE);
157 }
158
159 /**
160 * The number of seconds in the duration.
161 */
162 private final long seconds;
163 /**
1513 }
1514 buf.setCharAt(pos, '.');
1515 }
1516 buf.append('S');
1517 return buf.toString();
1518 }
1519
1520 //-----------------------------------------------------------------------
1521 /**
1522 * Writes the object using a
1523 * <a href="{@docRoot}/serialized-form.html#java.time.Ser">dedicated serialized form</a>.
1524 * @serialData
1525 * <pre>
1526 * out.writeByte(1); // identifies a Duration
1527 * out.writeLong(seconds);
1528 * out.writeInt(nanos);
1529 * </pre>
1530 *
1531 * @return the instance of {@code Ser}, not null
1532 */
1533 @java.io.Serial
1534 private Object writeReplace() {
1535 return new Ser(Ser.DURATION_TYPE, this);
1536 }
1537
1538 /**
1539 * Defend against malicious streams.
1540 *
1541 * @param s the stream to read
1542 * @throws InvalidObjectException always
1543 */
1544 @java.io.Serial
1545 private void readObject(ObjectInputStream s) throws InvalidObjectException {
1546 throw new InvalidObjectException("Deserialization via serialization delegate");
1547 }
1548
1549 void writeExternal(DataOutput out) throws IOException {
1550 out.writeLong(seconds);
1551 out.writeInt(nanos);
1552 }
1553
1554 static Duration readExternal(DataInput in) throws IOException {
1555 long seconds = in.readLong();
1556 int nanos = in.readInt();
1557 return Duration.ofSeconds(seconds, nanos);
1558 }
1559
1560 }
|