java mutator_src/java/time/temporal/TemporalAccessor.java · 康康/LearningJDK - Gitee.com

/*

* Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.

* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.

*

* This code is free software; you can redistribute it and/or modify it

* under the terms of the GNU General Public License version 2 only, as

* published by the Free Software Foundation. Oracle designates this

* particular file as subject to the "Classpath" exception as provided

* by Oracle in the LICENSE file that accompanied this code.

*

* This code is distributed in the hope that it will be useful, but WITHOUT

* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or

* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License

* version 2 for more details (a copy is included in the LICENSE file that

* accompanied this code).

*

* You should have received a copy of the GNU General Public License version

* 2 along with this work; if not, write to the Free Software Foundation,

* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.

*

* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA

* or visit www.oracle.com if you need additional information or have any

* questions.

*/

/*

* This file is available under and governed by the GNU General Public

* License version 2 only, as published by the Free Software Foundation.

* However, the following notice accompanied the original version of this

* file:

*

* Copyright (c) 2012, Stephen Colebourne & Michael Nascimento Santos

*

* All rights reserved.

*

* Redistribution and use in source and binary forms, with or without

* modification, are permitted provided that the following conditions are met:

*

* * Redistributions of source code must retain the above copyright notice,

* this list of conditions and the following disclaimer.

*

* * Redistributions in binary form must reproduce the above copyright notice,

* this list of conditions and the following disclaimer in the documentation

* and/or other materials provided with the distribution.

*

* * Neither the name of JSR-310 nor the names of its contributors

* may be used to endorse or promote products derived from this software

* without specific prior written permission.

*

* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS

* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT

* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR

* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR

* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,

* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,

* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR

* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF

* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING

* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS

* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*/

package java.time.temporal;

import java.time.DateTimeException;

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.

*

* This is the base interface type for date, time and offset objects.

* It is implemented by those classes that can provide information

* as {@linkplain TemporalField fields} or {@linkplain TemporalQuery queries}.

*

* Most date and time information can be represented as a number.

* These are modeled using {@code TemporalField} with the number held using

* 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.

*

* Two pieces of date/time information cannot be represented by numbers,

* 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}.

*

* A sub-interface, {@link Temporal}, extends this definition to one that also

* supports adjustment and manipulation on more complete temporal objects.

*

* This interface is a framework-level interface that should not be widely

* used in application code. Instead, applications should create and pass

* around instances of concrete types, such as {@code LocalDate}.

* There are many reasons for this, part of which is that implementations

* of this interface may be in calendar systems other than ISO.

* See {@link java.time.chrono.ChronoLocalDate} for a fuller discussion of the issues.

*

* @implSpec

* This interface places no restrictions on the mutability of implementations,

* however immutability is strongly recommended.

*

* @since 1.8

*/

/*

* 时间量访问器的接口,用来访问时间量中的字段信息。

*

* 注:所有时间量均实现了该接口。

*/

public interface TemporalAccessor {

/**

* Checks if the specified field is supported.

*

* This checks if the date-time can be queried for the specified field.

* 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 must be returned, otherwise false must be returned.

*

* 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.

*

* 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);

/**

* Gets the range of valid values for the specified field.

*

* All fields can be expressed as a {@code long} integer.

* This method returns an object that describes the valid range for that value.

* The value of this temporal object is used to enhance the accuracy of the returned range.

* If the date-time cannot return the range, because the field is unsupported or for

* some other reason, an exception will be thrown.

*

* Note that the result only describes the minimum and maximum valid values

* and it is important not to read too much into them. For example, there

* could be values within the range that are invalid for the field.

*

* @implSpec

* Implementations must check and handle all fields defined in {@link ChronoField}.

* If the field is supported, then the range of the field must be returned.

* If unsupported, then an {@code UnsupportedTemporalTypeException} must be thrown.

*

* 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.

*

* Implementations must ensure that no observable state is altered when this

* read-only method is invoked.

*

* The default implementation must behave equivalent to this code:

*

 
 

* if (field instanceof ChronoField) {

* if (isSupported(field)) {

* return field.range();

* }

* throw new UnsupportedTemporalTypeException("Unsupported field: " + field);

* }

* return field.rangeRefinedBy(this);

*

*

* @param field the field to query the range for, not null

* @return the range of valid values for the field, not null

* @throws DateTimeException if the range for the field cannot be obtained

* @throws UnsupportedTemporalTypeException if the field is not supported

*/

// 返回时间量字段field的取值区间,通常要求当前时间量支持该时间量字段

default ValueRange range(TemporalField field) {

Objects.requireNonNull(field, "field");

if(field instanceof ChronoField) {

// 确保当前时间量支持指定的时间量字段

if(isSupported(field)) {

return field.range();

}

throw new UnsupportedTemporalTypeException("Unsupported field: " + field);

}

return field.rangeRefinedBy(this);

}

/**

* Gets the value of the specified field as an {@code int}.

*

* This queries the date-time for the value of the specified field.

* The returned value will always be within the valid range of values for the field.

* If the date-time cannot return the value, because the field is unsupported or for

* some other reason, an exception will be thrown.

*

* @implSpec

* Implementations must check and handle all fields defined in {@link ChronoField}.

* If the field is supported and has an {@code int} range, then the value of

* the field must be returned.

* If unsupported, then an {@code UnsupportedTemporalTypeException} must be thrown.

*

* 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.

*

* Implementations must ensure that no observable state is altered when this

* read-only method is invoked.

*

* The default implementation must behave equivalent to this code:

*

 
 

* if (range(field).isIntValue()) {

* return range(field).checkValidIntValue(getLong(field), field);

* }

* throw new UnsupportedTemporalTypeException("Invalid field " + field + " + for get() method, use getLong() instead");

*

*

* @param field the field to get, not null

* @return the value for the field, within the valid range of values

* @throws DateTimeException if a value for the field cannot be obtained or

* the value is outside the range of valid values for the field

* @throws UnsupportedTemporalTypeException if the field is not supported or

* the range of values exceeds an {@code int}

* @throws ArithmeticException if numeric overflow occurs

*/

// 以int形式返回时间量字段field的值

default int get(TemporalField field) {

// 返回时间量字段field的取值区间,通常要求当前时间量支持该时间量字段

ValueRange range = range(field);

// 确保field的取值区间是否在int的范围内

if(!range.isIntValue()) {

throw new UnsupportedTemporalTypeException("Invalid field " + field + " for get() method, use getLong() instead");

}

// 以long形式返回时间量字段field的值

long value = getLong(field);

// 确保给定的值落在字段的取值区间中

if(!range.isValidValue(value)) {

throw new DateTimeException("Invalid value for " + field + " (valid values " + range + "): " + value);

}

// 将long截断为int

return (int) value;

}

/**

* Gets the value of the specified field as a {@code long}.

*

* This queries the date-time for the value of the specified field.

* The returned value may be outside the valid range of values for the field.

* If the date-time cannot return the value, because the field is unsupported or for

* some other reason, an exception will be thrown.

*

* @implSpec

* Implementations must check and handle all fields defined in {@link ChronoField}.

* If the field is supported, then the value of the field must be returned.

* If unsupported, then an {@code UnsupportedTemporalTypeException} must be thrown.

*

* 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.

*

* 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

* @throws ArithmeticException if numeric overflow occurs

*/

// 以long形式返回时间量字段field的值

long getLong(TemporalField field);

/**

* Queries this date-time.

*

* This queries this date-time using the specified query strategy object.

*

* Queries are a key tool for extracting information from date-times.

* They exists to externalize the process of querying, permitting different

* approaches, as per the strategy design pattern.

* Examples might be a query that checks if the date is the day before February 29th

* in a leap year, or calculates the number of days to your next birthday.

*

* The most common query implementations are method references, such as

* {@code LocalDate::from} and {@code ZoneId::from}.

* Additional implementations are provided as static methods on {@link TemporalQuery}.

*

* @implSpec

* The default implementation must behave equivalent to this code:

*

 
 

* if (query == TemporalQueries.zoneId() ||

* query == TemporalQueries.chronology() || query == TemporalQueries.precision()) {

* return null;

* }

* return query.queryFrom(this);

*

* Future versions are permitted to add further queries to the if statement.

*

* All classes implementing this interface and overriding this method must call

* {@code TemporalAccessor.super.query(query)}. JDK classes may avoid calling

* super if they provide behavior equivalent to the default behaviour, however

* non-JDK classes may not utilize this optimization and must call {@code super}.

*

* If the implementation can supply a value for one of the queries listed in the

* if statement of the default implementation, then it must do so.

* For example, an application-defined {@code HourMin} class storing the hour

* and minute must override this method as follows:

*

 
 

* if (query == TemporalQueries.precision()) {

* return MINUTES;

* }

* return TemporalAccessor.super.query(query);

*

*

* Implementations must ensure that no observable state is altered when this

* read-only method is invoked.

*

* @param 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

* @throws ArithmeticException if numeric overflow occurs

*/

/*

* 使用指定的时间量查询器,从当前时间量中查询目标信息

*

* 注:这里查询的目标信息,往往就是某个时间量字段的值

*/

default R query(TemporalQuery query) {

if(query == TemporalQueries.zoneId() || query == TemporalQueries.chronology() || query == TemporalQueries.precision()) {

return null;

}

return query.queryFrom(this);

}

}

一键复制

编辑

Web IDE

原始数据

按行查看

历史

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值