提交 2bf5a83f 编写于 作者: R rriggs

8024686: Cleanup of java.time serialization source

Summary: optimize serialized form of OffsetTime, OffsetDateTime; correct order of modifiers
Reviewed-by: sherman
上级 c255af6f
...@@ -141,7 +141,7 @@ public final class Duration ...@@ -141,7 +141,7 @@ public final class Duration
/** /**
* The pattern for parsing. * The pattern for parsing.
*/ */
private final static Pattern PATTERN = private static final Pattern PATTERN =
Pattern.compile("([-+]?)P(?:([-+]?[0-9]+)D)?" + Pattern.compile("([-+]?)P(?:([-+]?[0-9]+)D)?" +
"(T(?:([-+]?[0-9]+)H)?(?:([-+]?[0-9]+)M)?(?:([-+]?[0-9]+)(?:[.,]([0-9]{0,9}))?S)?)?", "(T(?:([-+]?[0-9]+)H)?(?:([-+]?[0-9]+)M)?(?:([-+]?[0-9]+)(?:[.,]([0-9]{0,9}))?S)?)?",
Pattern.CASE_INSENSITIVE); Pattern.CASE_INSENSITIVE);
...@@ -554,7 +554,7 @@ public final class Duration ...@@ -554,7 +554,7 @@ public final class Duration
* the simple initialization in Duration. * the simple initialization in Duration.
*/ */
private static class DurationUnits { private static class DurationUnits {
final static List<TemporalUnit> UNITS = static final List<TemporalUnit> UNITS =
Collections.unmodifiableList(Arrays.<TemporalUnit>asList(SECONDS, NANOS)); Collections.unmodifiableList(Arrays.<TemporalUnit>asList(SECONDS, NANOS));
} }
......
...@@ -1903,9 +1903,9 @@ public final class OffsetDateTime ...@@ -1903,9 +1903,9 @@ public final class OffsetDateTime
* <a href="../../serialized-form.html#java.time.Ser">dedicated serialized form</a>. * <a href="../../serialized-form.html#java.time.Ser">dedicated serialized form</a>.
* @serialData * @serialData
* <pre> * <pre>
* out.writeByte(10); // identifies a OffsetDateTime * out.writeByte(10); // identifies an OffsetDateTime
* out.writeObject(dateTime); * // the <a href="../../serialized-form.html#java.time.LocalDateTime">datetime</a> excluding the one byte header
* out.writeObject(offset); * // the <a href="../../serialized-form.html#java.time.ZoneOffset">offset</a> excluding the one byte header
* </pre> * </pre>
* *
* @return the instance of {@code Ser}, not null * @return the instance of {@code Ser}, not null
...@@ -1924,13 +1924,13 @@ public final class OffsetDateTime ...@@ -1924,13 +1924,13 @@ public final class OffsetDateTime
} }
void writeExternal(ObjectOutput out) throws IOException { void writeExternal(ObjectOutput out) throws IOException {
out.writeObject(dateTime); dateTime.writeExternal(out);
out.writeObject(offset); offset.writeExternal(out);
} }
static OffsetDateTime readExternal(ObjectInput in) throws IOException, ClassNotFoundException { static OffsetDateTime readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
LocalDateTime dateTime = (LocalDateTime) in.readObject(); LocalDateTime dateTime = LocalDateTime.readExternal(in);
ZoneOffset offset = (ZoneOffset) in.readObject(); ZoneOffset offset = ZoneOffset.readExternal(in);
return OffsetDateTime.of(dateTime, offset); return OffsetDateTime.of(dateTime, offset);
} }
......
...@@ -1374,9 +1374,9 @@ public final class OffsetTime ...@@ -1374,9 +1374,9 @@ public final class OffsetTime
* <a href="../../serialized-form.html#java.time.Ser">dedicated serialized form</a>. * <a href="../../serialized-form.html#java.time.Ser">dedicated serialized form</a>.
* @serialData * @serialData
* <pre> * <pre>
* out.writeByte(9); // identifies a OffsetTime * out.writeByte(9); // identifies an OffsetTime
* out.writeObject(time); * // the <a href="../../serialized-form.html#java.time.LocalTime">time</a> excluding the one byte header
* out.writeObject(offset); * // the <a href="../../serialized-form.html#java.time.ZoneOffset">offset</a> excluding the one byte header
* </pre> * </pre>
* *
* @return the instance of {@code Ser}, not null * @return the instance of {@code Ser}, not null
...@@ -1395,13 +1395,13 @@ public final class OffsetTime ...@@ -1395,13 +1395,13 @@ public final class OffsetTime
} }
void writeExternal(ObjectOutput out) throws IOException { void writeExternal(ObjectOutput out) throws IOException {
out.writeObject(time); time.writeExternal(out);
out.writeObject(offset); offset.writeExternal(out);
} }
static OffsetTime readExternal(ObjectInput in) throws IOException, ClassNotFoundException { static OffsetTime readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
LocalTime time = (LocalTime) in.readObject(); LocalTime time = LocalTime.readExternal(in);
ZoneOffset offset = (ZoneOffset) in.readObject(); ZoneOffset offset = ZoneOffset.readExternal(in);
return OffsetTime.of(time, offset); return OffsetTime.of(time, offset);
} }
......
...@@ -138,13 +138,13 @@ public final class Period ...@@ -138,13 +138,13 @@ public final class Period
/** /**
* The pattern for parsing. * The pattern for parsing.
*/ */
private final static Pattern PATTERN = private static final Pattern PATTERN =
Pattern.compile("([-+]?)P(?:([-+]?[0-9]+)Y)?(?:([-+]?[0-9]+)M)?(?:([-+]?[0-9]+)W)?(?:([-+]?[0-9]+)D)?", Pattern.CASE_INSENSITIVE); Pattern.compile("([-+]?)P(?:([-+]?[0-9]+)Y)?(?:([-+]?[0-9]+)M)?(?:([-+]?[0-9]+)W)?(?:([-+]?[0-9]+)D)?", Pattern.CASE_INSENSITIVE);
/** /**
* The set of supported units. * The set of supported units.
*/ */
private final static List<TemporalUnit> SUPPORTED_UNITS = private static final List<TemporalUnit> SUPPORTED_UNITS =
Collections.unmodifiableList(Arrays.<TemporalUnit>asList(YEARS, MONTHS, DAYS)); Collections.unmodifiableList(Arrays.<TemporalUnit>asList(YEARS, MONTHS, DAYS));
/** /**
......
...@@ -104,7 +104,7 @@ final class ChronoPeriodImpl ...@@ -104,7 +104,7 @@ final class ChronoPeriodImpl
/** /**
* The set of supported units. * The set of supported units.
*/ */
private final static List<TemporalUnit> SUPPORTED_UNITS = private static final List<TemporalUnit> SUPPORTED_UNITS =
Collections.unmodifiableList(Arrays.<TemporalUnit>asList(YEARS, MONTHS, DAYS)); Collections.unmodifiableList(Arrays.<TemporalUnit>asList(YEARS, MONTHS, DAYS));
/** /**
......
...@@ -140,7 +140,7 @@ public final class JapaneseDate ...@@ -140,7 +140,7 @@ public final class JapaneseDate
/** /**
* The first day supported by the JapaneseChronology is Meiji 6, January 1st. * The first day supported by the JapaneseChronology is Meiji 6, January 1st.
*/ */
final static LocalDate MEIJI_6_ISODATE = LocalDate.of(1873, 1, 1); static final LocalDate MEIJI_6_ISODATE = LocalDate.of(1873, 1, 1);
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
/** /**
......
...@@ -245,15 +245,15 @@ public final class WeekFields implements Serializable { ...@@ -245,15 +245,15 @@ public final class WeekFields implements Serializable {
/** /**
* The field used to access the computed DayOfWeek. * The field used to access the computed DayOfWeek.
*/ */
private transient final TemporalField dayOfWeek = ComputedDayOfField.ofDayOfWeekField(this); private final transient TemporalField dayOfWeek = ComputedDayOfField.ofDayOfWeekField(this);
/** /**
* The field used to access the computed WeekOfMonth. * The field used to access the computed WeekOfMonth.
*/ */
private transient final TemporalField weekOfMonth = ComputedDayOfField.ofWeekOfMonthField(this); private final transient TemporalField weekOfMonth = ComputedDayOfField.ofWeekOfMonthField(this);
/** /**
* The field used to access the computed WeekOfYear. * The field used to access the computed WeekOfYear.
*/ */
private transient final TemporalField weekOfYear = ComputedDayOfField.ofWeekOfYearField(this); private final transient TemporalField weekOfYear = ComputedDayOfField.ofWeekOfYearField(this);
/** /**
* The field that represents the week-of-week-based-year. * The field that represents the week-of-week-based-year.
* <p> * <p>
...@@ -261,7 +261,7 @@ public final class WeekFields implements Serializable { ...@@ -261,7 +261,7 @@ public final class WeekFields implements Serializable {
* <p> * <p>
* This unit is an immutable and thread-safe singleton. * This unit is an immutable and thread-safe singleton.
*/ */
private transient final TemporalField weekOfWeekBasedYear = ComputedDayOfField.ofWeekOfWeekBasedYearField(this); private final transient TemporalField weekOfWeekBasedYear = ComputedDayOfField.ofWeekOfWeekBasedYearField(this);
/** /**
* The field that represents the week-based-year. * The field that represents the week-based-year.
* <p> * <p>
...@@ -269,7 +269,7 @@ public final class WeekFields implements Serializable { ...@@ -269,7 +269,7 @@ public final class WeekFields implements Serializable {
* <p> * <p>
* This unit is an immutable and thread-safe singleton. * This unit is an immutable and thread-safe singleton.
*/ */
private transient final TemporalField weekBasedYear = ComputedDayOfField.ofWeekBasedYearField(this); private final transient TemporalField weekBasedYear = ComputedDayOfField.ofWeekBasedYearField(this);
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
/** /**
......
...@@ -171,9 +171,9 @@ final class Ser implements Externalizable { ...@@ -171,9 +171,9 @@ final class Ser implements Externalizable {
* <li><a href="../../../serialized-form.html#java.time.zone.ZoneRules">ZoneRules</a> * <li><a href="../../../serialized-form.html#java.time.zone.ZoneRules">ZoneRules</a>
* - {@code ZoneRules.of(standardTransitions, standardOffsets, savingsInstantTransitions, wallOffsets, lastRules);} * - {@code ZoneRules.of(standardTransitions, standardOffsets, savingsInstantTransitions, wallOffsets, lastRules);}
* <li><a href="../../../serialized-form.html#java.time.zone.ZoneOffsetTransition">ZoneOffsetTransition</a> * <li><a href="../../../serialized-form.html#java.time.zone.ZoneOffsetTransition">ZoneOffsetTransition</a>
* - {@code ;} * - {@code ZoneOffsetTransition of(LocalDateTime.ofEpochSecond(epochSecond), offsetBefore, offsetAfter);}
* <li><a href="../../../serialized-form.html#java.time.zone.ZoneOffsetTransitionRule">ZoneOffsetTransitionRule</a> * <li><a href="../../../serialized-form.html#java.time.zone.ZoneOffsetTransitionRule">ZoneOffsetTransitionRule</a>
* - {@code ;} * - {@code ZoneOffsetTransitionRule.of(month, dom, dow, time, timeEndOfDay, timeDefinition, standardOffset, offsetBefore, offsetAfter);}
* </ul> * </ul>
* @param in the data to read, not null * @param in the data to read, not null
*/ */
......
...@@ -191,7 +191,7 @@ public final class ZoneOffsetTransition ...@@ -191,7 +191,7 @@ public final class ZoneOffsetTransition
* out.writeByte(2); // identifies a ZoneOffsetTransition * out.writeByte(2); // identifies a ZoneOffsetTransition
* out.writeEpochSec(toEpochSecond); * out.writeEpochSec(toEpochSecond);
* out.writeOffset(offsetBefore); * out.writeOffset(offsetBefore);
* out.writeOfset(offsetAfter); * out.writeOffset(offsetAfter);
* } * }
* </pre> * </pre>
* @return the replacing object, not null * @return the replacing object, not null
......
...@@ -59,9 +59,6 @@ ...@@ -59,9 +59,6 @@
*/ */
package tck.java.time.serial; package tck.java.time.serial;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import tck.java.time.AbstractTCKTest;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream; import java.io.DataOutputStream;
...@@ -69,6 +66,10 @@ import java.time.LocalDateTime; ...@@ -69,6 +66,10 @@ import java.time.LocalDateTime;
import java.time.OffsetDateTime; import java.time.OffsetDateTime;
import java.time.ZoneOffset; import java.time.ZoneOffset;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import tck.java.time.AbstractTCKTest;
/** /**
* Test OffsetDateTime serialization. * Test OffsetDateTime serialization.
*/ */
...@@ -96,11 +97,6 @@ public class TCKOffsetDateTimeSerialization extends AbstractTCKTest { ...@@ -96,11 +97,6 @@ public class TCKOffsetDateTimeSerialization extends AbstractTCKTest {
ByteArrayOutputStream baos = new ByteArrayOutputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream();
try (DataOutputStream dos = new DataOutputStream(baos) ) { try (DataOutputStream dos = new DataOutputStream(baos) ) {
dos.writeByte(10); // java.time.Ser.OFFSET_DATE_TIME_TYPE dos.writeByte(10); // java.time.Ser.OFFSET_DATE_TIME_TYPE
}
byte[] bytes = baos.toByteArray();
ByteArrayOutputStream baosDateTime = new ByteArrayOutputStream();
try (DataOutputStream dos = new DataOutputStream(baosDateTime) ) {
dos.writeByte(5);
dos.writeInt(2012); dos.writeInt(2012);
dos.writeByte(9); dos.writeByte(9);
dos.writeByte(16); dos.writeByte(16);
...@@ -108,17 +104,11 @@ public class TCKOffsetDateTimeSerialization extends AbstractTCKTest { ...@@ -108,17 +104,11 @@ public class TCKOffsetDateTimeSerialization extends AbstractTCKTest {
dos.writeByte(17); dos.writeByte(17);
dos.writeByte(59); dos.writeByte(59);
dos.writeInt(464_000_000); dos.writeInt(464_000_000);
}
byte[] bytesDateTime = baosDateTime.toByteArray();
ByteArrayOutputStream baosOffset = new ByteArrayOutputStream();
try (DataOutputStream dos = new DataOutputStream(baosOffset) ) {
dos.writeByte(8);
dos.writeByte(4); // quarter hours stored: 3600 / 900 dos.writeByte(4); // quarter hours stored: 3600 / 900
} }
byte[] bytesOffset = baosOffset.toByteArray(); byte[] bytes = baos.toByteArray();
LocalDateTime ldt = LocalDateTime.of(2012, 9, 16, 22, 17, 59, 464_000_000); LocalDateTime ldt = LocalDateTime.of(2012, 9, 16, 22, 17, 59, 464_000_000);
assertSerializedBySer(OffsetDateTime.of(ldt, ZoneOffset.ofHours(1)), bytes, bytesDateTime, bytesOffset); assertSerializedBySer(OffsetDateTime.of(ldt, ZoneOffset.ofHours(1)), bytes);
} }
} }
...@@ -97,25 +97,14 @@ public class TCKOffsetTimeSerialization extends AbstractTCKTest { ...@@ -97,25 +97,14 @@ public class TCKOffsetTimeSerialization extends AbstractTCKTest {
ByteArrayOutputStream baos = new ByteArrayOutputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream();
try (DataOutputStream dos = new DataOutputStream(baos) ) { try (DataOutputStream dos = new DataOutputStream(baos) ) {
dos.writeByte(9); // java.time.Ser.OFFSET_TIME_TYPE dos.writeByte(9); // java.time.Ser.OFFSET_TIME_TYPE
}
byte[] bytes = baos.toByteArray();
ByteArrayOutputStream baosTime = new ByteArrayOutputStream();
try (DataOutputStream dos = new DataOutputStream(baosTime) ) {
dos.writeByte(4);
dos.writeByte(22); dos.writeByte(22);
dos.writeByte(17); dos.writeByte(17);
dos.writeByte(59); dos.writeByte(59);
dos.writeInt(464_000_000); dos.writeInt(464_000_000);
}
byte[] bytesTime = baosTime.toByteArray();
ByteArrayOutputStream baosOffset = new ByteArrayOutputStream();
try (DataOutputStream dos = new DataOutputStream(baosOffset) ) {
dos.writeByte(8);
dos.writeByte(4); // quarter hours stored: 3600 / 900 dos.writeByte(4); // quarter hours stored: 3600 / 900
} }
byte[] bytesOffset = baosOffset.toByteArray(); byte[] bytes = baos.toByteArray();
assertSerializedBySer(OffsetTime.of(22, 17, 59, 464_000_000, ZoneOffset.ofHours(1)), bytes, assertSerializedBySer(OffsetTime.of(22, 17, 59, 464_000_000, ZoneOffset.ofHours(1)), bytes);
bytesTime, bytesOffset);
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册