src/share/classes/java/time/temporal/TemporalAccessor.java
Print this page
@@ -60,11 +60,10 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package java.time.temporal;
import java.time.DateTimeException;
-import java.time.ZoneId;
import java.util.Objects;
/**
* Framework-level interface defining read-only access to a temporal object,
* such as a date, time, offset or some combination of these.
@@ -78,11 +77,12 @@
* a {@code long} to handle large values. Year, month and day-of-month are
* simple examples of fields, but they also include instant and offsets.
* See {@link ChronoField} for the standard set of fields.
* <p>
* Two pieces of date/time information cannot be represented by numbers,
- * the {@linkplain java.time.chrono.Chronology chronology} and the {@linkplain ZoneId time-zone}.
+ * the {@linkplain java.time.chrono.Chronology chronology} and the
+ * {@linkplain java.time.ZoneId time-zone}.
* These can be accessed via {@linkplain #query(TemporalQuery) queries} using
* the static methods defined on {@link TemporalQuery}.
* <p>
* A sub-interface, {@link Temporal}, extends this definition to one that also
* supports adjustment and manipulation on more complete temporal objects.
@@ -109,17 +109,18 @@
* If false, then calling the {@link #range(TemporalField) range} and {@link #get(TemporalField) get}
* methods will throw an exception.
*
* @implSpec
* Implementations must check and handle all fields defined in {@link ChronoField}.
- * If the field is supported, then true is returned, otherwise false
+ * If the field is supported, then true must be returned, otherwise false must be returned.
* <p>
* If the field is not a {@code ChronoField}, then the result of this method
* is obtained by invoking {@code TemporalField.isSupportedBy(TemporalAccessor)}
* passing {@code this} as the argument.
* <p>
- * Implementations must not alter either this object.
+ * Implementations must ensure that no observable state is altered when this
+ * read-only method is invoked.
*
* @param field the field to check, null returns false
* @return true if this date-time can be queried for the field, false if not
*/
boolean isSupported(TemporalField field);
@@ -144,19 +145,20 @@
* <p>
* If the field is not a {@code ChronoField}, then the result of this method
* is obtained by invoking {@code TemporalField.rangeRefinedBy(TemporalAccessorl)}
* passing {@code this} as the argument.
* <p>
- * Implementations must not alter either this object.
+ * Implementations must ensure that no observable state is altered when this
+ * read-only method is invoked.
* <p>
* The default implementation must behave equivalent to this code:
* <pre>
* if (field instanceof ChronoField) {
* if (isSupported(field)) {
* return field.range();
* }
- * throw new UnsupportedTemporalTypeException("Unsupported field: " + field.getName());
+ * throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
* }
* return field.rangeRefinedBy(this);
* </pre>
*
* @param field the field to query the range for, not null
@@ -167,11 +169,11 @@
default ValueRange range(TemporalField field) {
if (field instanceof ChronoField) {
if (isSupported(field)) {
return field.range();
}
- throw new UnsupportedTemporalTypeException("Unsupported field: " + field.getName());
+ throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
}
Objects.requireNonNull(field, "field");
return field.rangeRefinedBy(this);
}
@@ -191,11 +193,12 @@
* <p>
* If the field is not a {@code ChronoField}, then the result of this method
* is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)}
* passing {@code this} as the argument.
* <p>
- * Implementations must not alter this object.
+ * Implementations must ensure that no observable state is altered when this
+ * read-only method is invoked.
* <p>
* The default implementation must behave equivalent to this code:
* <pre>
* if (range(field).isIntValue()) {
* return range(field).checkValidIntValue(getLong(field), field);
@@ -238,11 +241,12 @@
* <p>
* If the field is not a {@code ChronoField}, then the result of this method
* is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)}
* passing {@code this} as the argument.
* <p>
- * Implementations must not alter either this object.
+ * Implementations must ensure that no observable state is altered when this
+ * read-only method is invoked.
*
* @param field the field to get, not null
* @return the value for the field
* @throws DateTimeException if a value for the field cannot be obtained
* @throws UnsupportedTemporalTypeException if the field is not supported
@@ -289,10 +293,13 @@
* if (query == TemporalQuery.precision()) {
* return MINUTES;
* }
* return TemporalAccessor.super.query(query);
* </pre>
+ * <p>
+ * Implementations must ensure that no observable state is altered when this
+ * read-only method is invoked.
*
* @param <R> the type of the result
* @param query the query to invoke, not null
* @return the query result, null may be returned (defined by the query)
* @throws DateTimeException if unable to query