1 /*
   2  * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 /*
  27  * This file is available under and governed by the GNU General Public
  28  * License version 2 only, as published by the Free Software Foundation.
  29  * However, the following notice accompanied the original version of this
  30  * file:
  31  *
  32  * Copyright (c) 2012, Stephen Colebourne & Michael Nascimento Santos
  33  *
  34  * All rights reserved.
  35  *
  36  * Redistribution and use in source and binary forms, with or without
  37  * modification, are permitted provided that the following conditions are met:
  38  *
  39  *  * Redistributions of source code must retain the above copyright notice,
  40  *    this list of conditions and the following disclaimer.
  41  *
  42  *  * Redistributions in binary form must reproduce the above copyright notice,
  43  *    this list of conditions and the following disclaimer in the documentation
  44  *    and/or other materials provided with the distribution.
  45  *
  46  *  * Neither the name of JSR-310 nor the names of its contributors
  47  *    may be used to endorse or promote products derived from this software
  48  *    without specific prior written permission.
  49  *
  50  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  51  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  52  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  53  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  54  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  55  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  56  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  57  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  58  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  59  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  60  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  61  */
  62 package java.time.temporal;
  63 
  64 import java.time.DateTimeException;
  65 import java.time.format.DateTimeBuilder;
  66 import java.util.Comparator;
  67 
  68 /**
  69  * A field of date-time, such as month-of-year or hour-of-minute.
  70  * <p>
  71  * Date and time is expressed using fields which partition the time-line into something
  72  * meaningful for humans. Implementations of this interface represent those fields.
  73  * <p>
  74  * The most commonly used units are defined in {@link ChronoField}.
  75  * Further fields are supplied in {@link ISOFields}, {@link WeekFields} and {@link JulianFields}.
  76  * Fields can also be written by application code by implementing this interface.
  77  * <p>
  78  * The field works using double dispatch. Client code calls methods on a date-time like
  79  * {@code LocalDateTime} which check if the field is a {@code ChronoField}.
  80  * If it is, then the date-time must handle it.
  81  * Otherwise, the method call is re-dispatched to the matching method in this interface.
  82  *
  83  * <h3>Specification for implementors</h3>
  84  * This interface must be implemented with care to ensure other classes operate correctly.
  85  * All implementations that can be instantiated must be final, immutable and thread-safe.
  86  * It is recommended to use an enum where possible.
  87  *
  88  * @since 1.8
  89  */
  90 public interface TemporalField extends Comparator<TemporalAccessor> {
  91 
  92     /**
  93      * Gets a descriptive name for the field.
  94      * <p>
  95      * The should be of the format 'BaseOfRange', such as 'MonthOfYear',
  96      * unless the field has a range of {@code FOREVER}, when only
  97      * the base unit is mentioned, such as 'Year' or 'Era'.
  98      *
  99      * @return the name, not null
 100      */
 101     String getName();
 102 
 103     /**
 104      * Gets the unit that the field is measured in.
 105      * <p>
 106      * The unit of the field is the period that varies within the range.
 107      * For example, in the field 'MonthOfYear', the unit is 'Months'.
 108      * See also {@link #getRangeUnit()}.
 109      *
 110      * @return the period unit defining the base unit of the field, not null
 111      */
 112     TemporalUnit getBaseUnit();
 113 
 114     /**
 115      * Gets the range that the field is bound by.
 116      * <p>
 117      * The range of the field is the period that the field varies within.
 118      * For example, in the field 'MonthOfYear', the range is 'Years'.
 119      * See also {@link #getBaseUnit()}.
 120      * <p>
 121      * The range is never null. For example, the 'Year' field is shorthand for
 122      * 'YearOfForever'. It therefore has a unit of 'Years' and a range of 'Forever'.
 123      *
 124      * @return the period unit defining the range of the field, not null
 125      */
 126     TemporalUnit getRangeUnit();
 127 
 128     //-----------------------------------------------------------------------
 129     /**
 130      * Compares the value of this field in two temporal objects.
 131      * <p>
 132      * All fields implement {@link Comparator} on {@link TemporalAccessor}.
 133      * This allows a list of date-times to be compared using the value of a field.
 134      * For example, you could sort a list of arbitrary temporal objects by the value of
 135      * the month-of-year field - {@code Collections.sort(list, MONTH_OF_YEAR)}
 136      * <p>
 137      * The default implementation must behave equivalent to this code:
 138      * <pre>
 139      *  return Long.compare(temporal1.getLong(this), temporal2.getLong(this));
 140      * </pre>
 141      *
 142      * @param temporal1  the first temporal object to compare, not null
 143      * @param temporal2  the second temporal object to compare, not null
 144      * @throws DateTimeException if unable to obtain the value for this field
 145      */
 146     public default int compare(TemporalAccessor temporal1, TemporalAccessor temporal2) {
 147          return Long.compare(temporal1.getLong(this), temporal2.getLong(this));
 148     }
 149 
 150     /**
 151      * Gets the range of valid values for the field.
 152      * <p>
 153      * All fields can be expressed as a {@code long} integer.
 154      * This method returns an object that describes the valid range for that value.
 155      * This method is generally only applicable to the ISO-8601 calendar system.
 156      * <p>
 157      * Note that the result only describes the minimum and maximum valid values
 158      * and it is important not to read too much into them. For example, there
 159      * could be values within the range that are invalid for the field.
 160      *
 161      * @return the range of valid values for the field, not null
 162      */
 163     ValueRange range();
 164 
 165     //-----------------------------------------------------------------------
 166     /**
 167      * Checks if this field is supported by the temporal object.
 168      * <p>
 169      * This determines whether the temporal accessor supports this field.
 170      * If this returns false, the the temporal cannot be queried for this field.
 171      * <p>
 172      * There are two equivalent ways of using this method.
 173      * The first is to invoke this method directly.
 174      * The second is to use {@link TemporalAccessor#isSupported(TemporalField)}:
 175      * <pre>
 176      *   // these two lines are equivalent, but the second approach is recommended
 177      *   temporal = thisField.doIsSupported(temporal);
 178      *   temporal = temporal.isSupported(thisField);
 179      * </pre>
 180      * It is recommended to use the second approach, {@code isSupported(TemporalField)},
 181      * as it is a lot clearer to read in code.
 182      * <p>
 183      * Implementations should determine whether they are supported using the fields
 184      * available in {@link ChronoField}.
 185      *
 186      * @param temporal  the temporal object to query, not null
 187      * @return true if the date-time can be queried for this field, false if not
 188      */
 189     boolean doIsSupported(TemporalAccessor temporal);
 190 
 191     /**
 192      * Get the range of valid values for this field using the temporal object to
 193      * refine the result.
 194      * <p>
 195      * This uses the temporal object to find the range of valid values for the field.
 196      * This is similar to {@link #range()}, however this method refines the result
 197      * using the temporal. For example, if the field is {@code DAY_OF_MONTH} the
 198      * {@code range} method is not accurate as there are four possible month lengths,
 199      * 28, 29, 30 and 31 days. Using this method with a date allows the range to be
 200      * accurate, returning just one of those four options.
 201      * <p>
 202      * There are two equivalent ways of using this method.
 203      * The first is to invoke this method directly.
 204      * The second is to use {@link TemporalAccessor#range(TemporalField)}:
 205      * <pre>
 206      *   // these two lines are equivalent, but the second approach is recommended
 207      *   temporal = thisField.doRange(temporal);
 208      *   temporal = temporal.range(thisField);
 209      * </pre>
 210      * It is recommended to use the second approach, {@code range(TemporalField)},
 211      * as it is a lot clearer to read in code.
 212      * <p>
 213      * Implementations should perform any queries or calculations using the fields
 214      * available in {@link ChronoField}.
 215      * If the field is not supported a {@code DateTimeException} must be thrown.
 216      *
 217      * @param temporal  the temporal object used to refine the result, not null
 218      * @return the range of valid values for this field, not null
 219      * @throws DateTimeException if the range for the field cannot be obtained
 220      */
 221     ValueRange doRange(TemporalAccessor temporal);
 222 
 223     /**
 224      * Gets the value of this field from the specified temporal object.
 225      * <p>
 226      * This queries the temporal object for the value of this field.
 227      * <p>
 228      * There are two equivalent ways of using this method.
 229      * The first is to invoke this method directly.
 230      * The second is to use {@link TemporalAccessor#getLong(TemporalField)}
 231      * (or {@link TemporalAccessor#get(TemporalField)}):
 232      * <pre>
 233      *   // these two lines are equivalent, but the second approach is recommended
 234      *   temporal = thisField.doGet(temporal);
 235      *   temporal = temporal.getLong(thisField);
 236      * </pre>
 237      * It is recommended to use the second approach, {@code getLong(TemporalField)},
 238      * as it is a lot clearer to read in code.
 239      * <p>
 240      * Implementations should perform any queries or calculations using the fields
 241      * available in {@link ChronoField}.
 242      * If the field is not supported a {@code DateTimeException} must be thrown.
 243      *
 244      * @param temporal  the temporal object to query, not null
 245      * @return the value of this field, not null
 246      * @throws DateTimeException if a value for the field cannot be obtained
 247      */
 248     long doGet(TemporalAccessor temporal);
 249 
 250     /**
 251      * Returns a copy of the specified temporal object with the value of this field set.
 252      * <p>
 253      * This returns a new temporal object based on the specified one with the value for
 254      * this field changed. For example, on a {@code LocalDate}, this could be used to
 255      * set the year, month or day-of-month.
 256      * The returned object has the same observable type as the specified object.
 257      * <p>
 258      * In some cases, changing a field is not fully defined. For example, if the target object is
 259      * a date representing the 31st January, then changing the month to February would be unclear.
 260      * In cases like this, the implementation is responsible for resolving the result.
 261      * Typically it will choose the previous valid date, which would be the last valid
 262      * day of February in this example.
 263      * <p>
 264      * There are two equivalent ways of using this method.
 265      * The first is to invoke this method directly.
 266      * The second is to use {@link Temporal#with(TemporalField, long)}:
 267      * <pre>
 268      *   // these two lines are equivalent, but the second approach is recommended
 269      *   temporal = thisField.doWith(temporal);
 270      *   temporal = temporal.with(thisField);
 271      * </pre>
 272      * It is recommended to use the second approach, {@code with(TemporalField)},
 273      * as it is a lot clearer to read in code.
 274      * <p>
 275      * Implementations should perform any queries or calculations using the fields
 276      * available in {@link ChronoField}.
 277      * If the field is not supported a {@code DateTimeException} must be thrown.
 278      * <p>
 279      * Implementations must not alter the specified temporal object.
 280      * Instead, an adjusted copy of the original must be returned.
 281      * This provides equivalent, safe behavior for immutable and mutable implementations.
 282      *
 283      * @param <R>  the type of the Temporal object
 284      * @param temporal the temporal object to adjust, not null
 285      * @param newValue the new value of the field
 286      * @return the adjusted temporal object, not null
 287      * @throws DateTimeException if the field cannot be set
 288      */
 289     <R extends Temporal> R doWith(R temporal, long newValue);
 290 
 291     /**
 292      * Resolves the date/time information in the builder
 293      * <p>
 294      * This method is invoked during the resolve of the builder.
 295      * Implementations should combine the associated field with others to form
 296      * objects like {@code LocalDate}, {@code LocalTime} and {@code LocalDateTime}
 297      *
 298      * @param builder  the builder to resolve, not null
 299      * @param value  the value of the associated field
 300      * @return true if builder has been changed, false otherwise
 301      * @throws DateTimeException if unable to resolve
 302      */
 303     boolean resolve(DateTimeBuilder builder, long value);
 304 
 305 }