Created
February 1, 2013 14:40
-
-
Save jodastephen/4691683 to your computer and use it in GitHub Desktop.
APPLIED: Static methods on chrono classes. Patch for ThreeTen/threeten#203
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/src/share/classes/java/time/chrono/ChronoLocalDate.java b/src/share/classes/java/time/chrono/ChronoLocalDate.java | |
--- a/src/share/classes/java/time/chrono/ChronoLocalDate.java | |
+++ b/src/share/classes/java/time/chrono/ChronoLocalDate.java | |
@@ -270,6 +270,39 @@ | |
//----------------------------------------------------------------------- | |
/** | |
+ * Obtains an instance of {@code ChronoLocalDate} from a temporal object. | |
+ * <p> | |
+ * This creates a local date based on the specified temporal. | |
+ * A {@code TemporalAccessor} represents some form of date and time information. | |
+ * This factory converts the arbitrary temporal object to an instance of {@code ChronoLocalDate}. | |
+ * <p> | |
+ * The conversion extracts and combines the chronology and date. | |
+ * The chronology is extracted first, delegating the rest of the conversion | |
+ * to {@link Chronology#date(TemporalAccessor)} | |
+ * Implementations are permitted to perform optimizations such as accessing | |
+ * those fields that are equivalent to the relevant objects. | |
+ * <p> | |
+ * This method matches the signature of the functional interface {@link TemporalQuery} | |
+ * allowing it to be used as a query via method reference, {@code ChronoLocalDate::from}. | |
+ * | |
+ * @param temporal the temporal object to convert, not null | |
+ * @return the date, not null | |
+ * @throws DateTimeException if unable to convert to a {@code ChronoLocalDate} | |
+ * @see Chronology#date(TemporalAccessor) | |
+ */ | |
+ public static ChronoLocalDate<?> from(TemporalAccessor temporal) { | |
+ if (temporal instanceof ChronoLocalDate) { | |
+ return (ChronoLocalDate) temporal; | |
+ } | |
+ Chronology<?> chrono = temporal.query(Queries.chronology()); | |
+ if (chrono == null) { | |
+ throw new DateTimeException("Unable to obtain ChronoLocalDate from TemporalAccessor: " + temporal.getClass()); | |
+ } | |
+ return chrono.date(temporal); | |
+ } | |
+ | |
+ //----------------------------------------------------------------------- | |
+ /** | |
* Gets the chronology of this date. | |
* <p> | |
* The {@code Chronology} represents the calendar system in use. | |
diff --git a/src/share/classes/java/time/chrono/ChronoLocalDateTime.java b/src/share/classes/java/time/chrono/ChronoLocalDateTime.java | |
--- a/src/share/classes/java/time/chrono/ChronoLocalDateTime.java | |
+++ b/src/share/classes/java/time/chrono/ChronoLocalDateTime.java | |
@@ -142,6 +142,40 @@ | |
} | |
}; | |
+ //----------------------------------------------------------------------- | |
+ /** | |
+ * Obtains an instance of {@code ChronoLocalDateTime} from a temporal object. | |
+ * <p> | |
+ * This creates a local date-time based on the specified temporal. | |
+ * A {@code TemporalAccessor} represents some form of date and time information. | |
+ * This factory converts the arbitrary temporal object to an instance of {@code ChronoLocalDateTime}. | |
+ * <p> | |
+ * The conversion extracts and combines the chronology, date and time. | |
+ * The chronology is extracted first, delegating the rest of the conversion | |
+ * to {@link Chronology#localDateTime(TemporalAccessor)} | |
+ * Implementations are permitted to perform optimizations such as accessing | |
+ * those fields that are equivalent to the relevant objects. | |
+ * <p> | |
+ * This method matches the signature of the functional interface {@link TemporalQuery} | |
+ * allowing it to be used as a query via method reference, {@code ChronoLocalDateTime::from}. | |
+ * | |
+ * @param temporal the temporal object to convert, not null | |
+ * @return the date-time, not null | |
+ * @throws DateTimeException if unable to convert to a {@code ChronoLocalDateTime} | |
+ * @see Chronology#localDateTime(TemporalAccessor) | |
+ */ | |
+ public static ChronoLocalDateTime<?> from(TemporalAccessor temporal) { | |
+ if (temporal instanceof ChronoLocalDateTime) { | |
+ return (ChronoLocalDateTime) temporal; | |
+ } | |
+ Chronology<?> chrono = temporal.query(Queries.chronology()); | |
+ if (chrono == null) { | |
+ throw new DateTimeException("Unable to obtain ChronoLocalDateTime from TemporalAccessor: " + temporal.getClass()); | |
+ } | |
+ return chrono.localDateTime(temporal); | |
+ } | |
+ | |
+ //----------------------------------------------------------------------- | |
/** | |
* Gets the local date part of this date-time. | |
* <p> | |
diff --git a/src/share/classes/java/time/chrono/ChronoZonedDateTime.java b/src/share/classes/java/time/chrono/ChronoZonedDateTime.java | |
--- a/src/share/classes/java/time/chrono/ChronoZonedDateTime.java | |
+++ b/src/share/classes/java/time/chrono/ChronoZonedDateTime.java | |
@@ -141,6 +141,40 @@ | |
} | |
}; | |
+ //----------------------------------------------------------------------- | |
+ /** | |
+ * Obtains an instance of {@code ChronoZonedDateTime} from a temporal object. | |
+ * <p> | |
+ * This creates a zoned date-time based on the specified temporal. | |
+ * A {@code TemporalAccessor} represents some form of date and time information. | |
+ * This factory converts the arbitrary temporal object to an instance of {@code ChronoZonedDateTime}. | |
+ * <p> | |
+ * The conversion extracts and combines the chronology, date, time and zone. | |
+ * The chronology is extracted first, delegating the rest of the conversion | |
+ * to {@link Chronology#zonedDateTime(TemporalAccessor)} | |
+ * Implementations are permitted to perform optimizations such as accessing | |
+ * those fields that are equivalent to the relevant objects. | |
+ * <p> | |
+ * This method matches the signature of the functional interface {@link TemporalQuery} | |
+ * allowing it to be used as a query via method reference, {@code ChronoZonedDateTime::from}. | |
+ * | |
+ * @param temporal the temporal object to convert, not null | |
+ * @return the date-time, not null | |
+ * @throws DateTimeException if unable to convert to a {@code ChronoZonedDateTime} | |
+ * @see Chronology#zonedDateTime(TemporalAccessor) | |
+ */ | |
+ public static ChronoZonedDateTime<?> from(TemporalAccessor temporal) { | |
+ if (temporal instanceof ChronoZonedDateTime) { | |
+ return (ChronoZonedDateTime) temporal; | |
+ } | |
+ Chronology<?> chrono = temporal.query(Queries.chronology()); | |
+ if (chrono == null) { | |
+ throw new DateTimeException("Unable to obtain ChronoZonedDateTime from TemporalAccessor: " + temporal.getClass()); | |
+ } | |
+ return chrono.zonedDateTime(temporal); | |
+ } | |
+ | |
+ //----------------------------------------------------------------------- | |
@Override | |
public default ValueRange range(TemporalField field) { | |
if (field instanceof ChronoField) { | |
diff --git a/src/share/classes/java/time/chrono/Chronology.java b/src/share/classes/java/time/chrono/Chronology.java | |
--- a/src/share/classes/java/time/chrono/Chronology.java | |
+++ b/src/share/classes/java/time/chrono/Chronology.java | |
@@ -595,6 +595,7 @@ | |
* @param temporal the temporal object to convert, not null | |
* @return the local date in this chronology, not null | |
* @throws DateTimeException if unable to create the date | |
+ * @see ChronoLocalDate#from(TemporalAccessor) | |
*/ | |
public abstract ChronoLocalDate<C> date(TemporalAccessor temporal); | |
@@ -617,6 +618,7 @@ | |
* @param temporal the temporal object to convert, not null | |
* @return the local date-time in this chronology, not null | |
* @throws DateTimeException if unable to create the date-time | |
+ * @see ChronoLocalDateTime#from(TemporalAccessor) | |
*/ | |
public ChronoLocalDateTime<C> localDateTime(TemporalAccessor temporal) { | |
try { | |
@@ -648,6 +650,7 @@ | |
* @param temporal the temporal object to convert, not null | |
* @return the zoned date-time in this chronology, not null | |
* @throws DateTimeException if unable to create the date-time | |
+ * @see ChronoZonedDateTime#from(TemporalAccessor) | |
*/ | |
public ChronoZonedDateTime<C> zonedDateTime(TemporalAccessor temporal) { | |
try { |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment