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) 2007-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 static java.time.temporal.ChronoField.EPOCH_DAY;
  65 import static java.time.temporal.ChronoField.NANO_OF_DAY;
  66 import static java.time.temporal.ChronoField.OFFSET_SECONDS;
  67 
  68 import java.time.LocalDate;
  69 import java.time.LocalTime;
  70 import java.time.OffsetDateTime;
  71 import java.time.ZoneId;
  72 import java.time.ZoneOffset;
  73 import java.time.ZonedDateTime;
  74 import java.time.chrono.Chronology;
  75 
  76 /**
  77  * Common implementations of {@code TemporalQuery}.
  78  * <p>
  79  * This class provides common implementations of {@link TemporalQuery}.
  80  * These queries are primarily used as optimizations, allowing the internals
  81  * of other objects to be extracted effectively. Note that application code
  82  * can also use the {@code from(TemporalAccessor)} method on most temporal
  83  * objects as a method reference matching the query interface, such as
  84  * {@code LocalDate::from} and {@code ZoneId::from}.
  85  * <p>
  86  * There are two equivalent ways of using a {@code TemporalQuery}.
  87  * The first is to invoke the method on the interface directly.
  88  * The second is to use {@link TemporalAccessor#query(TemporalQuery)}:
  89  * <pre>
  90  *   // these two lines are equivalent, but the second approach is recommended
  91  *   dateTime = query.queryFrom(dateTime);
  92  *   dateTime = dateTime.query(query);
  93  * </pre>
  94  * It is recommended to use the second approach, {@code query(TemporalQuery)},
  95  * as it is a lot clearer to read in code.
  96  *
  97  * <h3>Specification for implementors</h3>
  98  * This is a thread-safe utility class.
  99  * All returned adjusters are immutable and thread-safe.
 100  *
 101  * @since 1.8
 102  */
 103 public final class Queries {
 104     // note that it is vital that each method supplies a constant, not a
 105     // calculated value, as they will be checked for using ==
 106     // it is also vital that each constant is different (due to the == checking)
 107     // as such, alterations to use lambdas must be done with extreme care
 108 
 109     /**
 110      * Private constructor since this is a utility class.
 111      */
 112     private Queries() {
 113     }
 114 
 115     //-----------------------------------------------------------------------
 116     // special constants should be used to extract information from a TemporalAccessor
 117     // that cannot be derived in other ways
 118     // Javadoc added here, so as to pretend they are more normal than they really are
 119 
 120     /**
 121      * A strict query for the {@code ZoneId}.
 122      * <p>
 123      * This queries a {@code TemporalAccessor} for the zone.
 124      * The zone is only returned if the date-time conceptually contains a {@code ZoneId}.
 125      * It will not be returned if the date-time only conceptually has an {@code ZoneOffset}.
 126      * Thus a {@link ZonedDateTime} will return the result of {@code getZone()},
 127      * but an {@link OffsetDateTime} will return null.

 128      * <p>
 129      * In most cases, applications should use {@link #ZONE} as this query is too strict.
 130      * <p>
 131      * The result from JDK classes implementing {@code TemporalAccessor} is as follows:<br>
 132      * {@code LocalDate} returns null<br>
 133      * {@code LocalTime} returns null<br>
 134      * {@code LocalDateTime} returns null<br>
 135      * {@code ZonedDateTime} returns the associated zone<br>

 136      * {@code OffsetTime} returns null<br>
 137      * {@code OffsetDateTime} returns null<br>
 138      * {@code ChronoLocalDate} returns null<br>
 139      * {@code ChronoLocalDateTime} returns null<br>
 140      * {@code ChronoZonedDateTime} returns the associated zone<br>
 141      * {@code Era} returns null<br>
 142      * {@code DayOfWeek} returns null<br>
 143      * {@code Month} returns null<br>
 144      * {@code Year} returns null<br>
 145      * {@code YearMonth} returns null<br>
 146      * {@code MonthDay} returns null<br>
 147      * {@code ZoneOffset} returns null<br>
 148      * {@code Instant} returns null<br>
 149      *
 150      * @return a query that can obtain the zone ID of a temporal, not null
 151      */
 152     public static final TemporalQuery<ZoneId> zoneId() {
 153         return ZONE_ID;
 154     }
 155     static final TemporalQuery<ZoneId> ZONE_ID = (temporal) -> {
 156         return temporal.query(ZONE_ID);



 157     };
 158 
 159     /**
 160      * A query for the {@code Chronology}.
 161      * <p>
 162      * This queries a {@code TemporalAccessor} for the chronology.
 163      * If the target {@code TemporalAccessor} represents a date, or part of a date,
 164      * then it should return the chronology that the date is expressed in.
 165      * As a result of this definition, objects only representing time, such as
 166      * {@code LocalTime}, will return null.
 167      * <p>
 168      * The result from JDK classes implementing {@code TemporalAccessor} is as follows:<br>
 169      * {@code LocalDate} returns {@code IsoChronology.INSTANCE}<br>
 170      * {@code LocalTime} returns null (does not represent a date)<br>
 171      * {@code LocalDateTime} returns {@code IsoChronology.INSTANCE}<br>
 172      * {@code ZonedDateTime} returns {@code IsoChronology.INSTANCE}<br>

 173      * {@code OffsetTime} returns null (does not represent a date)<br>
 174      * {@code OffsetDateTime} returns {@code IsoChronology.INSTANCE}<br>
 175      * {@code ChronoLocalDate} returns the associated chronology<br>
 176      * {@code ChronoLocalDateTime} returns the associated chronology<br>
 177      * {@code ChronoZonedDateTime} returns the associated chronology<br>
 178      * {@code Era} returns the associated chronology<br>
 179      * {@code DayOfWeek} returns null (shared across chronologies)<br>
 180      * {@code Month} returns {@code IsoChronology.INSTANCE}<br>
 181      * {@code Year} returns {@code IsoChronology.INSTANCE}<br>
 182      * {@code YearMonth} returns {@code IsoChronology.INSTANCE}<br>
 183      * {@code MonthDay} returns null {@code IsoChronology.INSTANCE}<br>
 184      * {@code ZoneOffset} returns null (does not represent a date)<br>
 185      * {@code Instant} returns null (does not represent a date)<br>
 186      * <p>
 187      * The method {@link Chronology#from(TemporalAccessor)} can be used as a
 188      * {@code TemporalQuery} via a method reference, {@code Chronology::from}.
 189      * That method is equivalent to this query, except that it throws an
 190      * exception if a chronology cannot be obtained.
 191      *
 192      * @return a query that can obtain the chronology of a temporal, not null
 193      */
 194     public static final TemporalQuery<Chronology> chronology() {
 195         return CHRONO;
 196     }
 197     static final TemporalQuery<Chronology> CHRONO = (temporal) -> {
 198         return temporal.query(CHRONO);



 199     };
 200 
 201     /**
 202      * A query for the smallest supported unit.
 203      * <p>
 204      * This queries a {@code TemporalAccessor} for the time precision.
 205      * If the target {@code TemporalAccessor} represents a consistent or complete date-time,
 206      * date or time then this must return the smallest precision actually supported.
 207      * Note that fields such as {@code NANO_OF_DAY} and {@code NANO_OF_SECOND}
 208      * are defined to always return ignoring the precision, thus this is the only
 209      * way to find the actual smallest supported unit.
 210      * For example, were {@code GregorianCalendar} to implement {@code TemporalAccessor}
 211      * it would return a precision of {@code MILLIS}.
 212      * <p>
 213      * The result from JDK classes implementing {@code TemporalAccessor} is as follows:<br>
 214      * {@code LocalDate} returns {@code DAYS}<br>
 215      * {@code LocalTime} returns {@code NANOS}<br>
 216      * {@code LocalDateTime} returns {@code NANOS}<br>
 217      * {@code ZonedDateTime} returns {@code NANOS}<br>

 218      * {@code OffsetTime} returns {@code NANOS}<br>
 219      * {@code OffsetDateTime} returns {@code NANOS}<br>
 220      * {@code ChronoLocalDate} returns {@code DAYS}<br>
 221      * {@code ChronoLocalDateTime} returns {@code NANOS}<br>
 222      * {@code ChronoZonedDateTime} returns {@code NANOS}<br>
 223      * {@code Era} returns {@code ERAS}<br>
 224      * {@code DayOfWeek} returns {@code DAYS}<br>
 225      * {@code Month} returns {@code MONTHS}<br>
 226      * {@code Year} returns {@code YEARS}<br>
 227      * {@code YearMonth} returns {@code MONTHS}<br>
 228      * {@code MonthDay} returns null (does not represent a complete date or time)<br>
 229      * {@code ZoneOffset} returns null (does not represent a date or time)<br>
 230      * {@code Instant} returns {@code NANOS}<br>
 231      *
 232      * @return a query that can obtain the precision of a temporal, not null
 233      */
 234     public static final TemporalQuery<TemporalUnit> precision() {
 235         return PRECISION;
 236     }
 237     static final TemporalQuery<TemporalUnit> PRECISION = (temporal) -> {
 238         return temporal.query(PRECISION);



 239     };
 240 
 241     //-----------------------------------------------------------------------
 242     // non-special constants are standard queries that derive information from other information
 243     /**
 244      * A lenient query for the {@code ZoneId}, falling back to the {@code ZoneOffset}.
 245      * <p>
 246      * This queries a {@code TemporalAccessor} for the zone.
 247      * It first tries to obtain the zone, using {@link #zoneId()}.
 248      * If that is not found it tries to obtain the {@link #offset()}.
 249      * Thus a {@link ZonedDateTime} will return the result of {@code getZone()},
 250      * while an {@link OffsetDateTime} will return the result of {@code getOffset()}.
 251      * <p>
 252      * In most cases, applications should use this query rather than {@code #zoneId()}.
 253      * <p>



 254      * The method {@link ZoneId#from(TemporalAccessor)} can be used as a
 255      * {@code TemporalQuery} via a method reference, {@code ZoneId::from}.
 256      * That method is equivalent to this query, except that it throws an
 257      * exception if a zone cannot be obtained.
 258      *
 259      * @return a query that can obtain the zone ID or offset of a temporal, not null
 260      */
 261     public static final TemporalQuery<ZoneId> zone() {
 262         return ZONE;
 263     }
 264     static final TemporalQuery<ZoneId> ZONE = (temporal) -> {


 265         ZoneId zone = temporal.query(ZONE_ID);
 266         return (zone != null ? zone : temporal.query(OFFSET));

 267     };
 268 
 269     /**
 270      * A query for {@code ZoneOffset} returning null if not found.
 271      * <p>
 272      * This returns a {@code TemporalQuery} that can be used to query a temporal
 273      * object for the offset. The query will return null if the temporal
 274      * object cannot supply an offset.
 275      * <p>
 276      * The query implementation examines the {@link ChronoField#OFFSET_SECONDS OFFSET_SECONDS}
 277      * field and uses it to create a {@code ZoneOffset}.
 278      * <p>
 279      * The method {@link ZoneOffset#from(TemporalAccessor)} can be used as a
 280      * {@code TemporalQuery} via a method reference, {@code ZoneOffset::from}.
 281      * This query and {@code ZoneOffset::from} will return the same result if the
 282      * temporal object contains an offset. If the temporal object does not contain
 283      * an offset, then the method reference will throw an exception, whereas this
 284      * query will return null.
 285      *
 286      * @return a query that can obtain the offset of a temporal, not null
 287      */
 288     public static final TemporalQuery<ZoneOffset> offset() {
 289         return OFFSET;
 290     }
 291     static final TemporalQuery<ZoneOffset> OFFSET = (temporal) -> {


 292         if (temporal.isSupported(OFFSET_SECONDS)) {
 293             return ZoneOffset.ofTotalSeconds(temporal.get(OFFSET_SECONDS));
 294         }
 295         return null;
 296     };
 297 
 298     /**
 299      * A query for {@code LocalDate} returning null if not found.
 300      * <p>
 301      * This returns a {@code TemporalQuery} that can be used to query a temporal
 302      * object for the local date. The query will return null if the temporal
 303      * object cannot supply a local date.
 304      * <p>
 305      * The query implementation examines the {@link ChronoField#EPOCH_DAY EPOCH_DAY}
 306      * field and uses it to create a {@code LocalDate}.
 307      * <p>
 308      * The method {@link ZoneOffset#from(TemporalAccessor)} can be used as a
 309      * {@code TemporalQuery} via a method reference, {@code LocalDate::from}.
 310      * This query and {@code LocalDate::from} will return the same result if the
 311      * temporal object contains a date. If the temporal object does not contain
 312      * a date, then the method reference will throw an exception, whereas this
 313      * query will return null.
 314      *
 315      * @return a query that can obtain the date of a temporal, not null
 316      */
 317     public static final TemporalQuery<LocalDate> localDate() {
 318         return LOCAL_DATE;
 319     }
 320     static final TemporalQuery<LocalDate> LOCAL_DATE = (temporal) -> {
 321         if (temporal.isSupported(EPOCH_DAY)) {
 322             return LocalDate.ofEpochDay(temporal.getLong(EPOCH_DAY));
 323         }
 324         return null;
 325     };
 326 
 327     /**
 328      * A query for {@code LocalTime} returning null if not found.
 329      * <p>
 330      * This returns a {@code TemporalQuery} that can be used to query a temporal
 331      * object for the local time. The query will return null if the temporal
 332      * object cannot supply a local time.
 333      * <p>
 334      * The query implementation examines the {@link ChronoField#NANO_OF_DAY NANO_OF_DAY}
 335      * field and uses it to create a {@code LocalTime}.
 336      * <p>
 337      * The method {@link ZoneOffset#from(TemporalAccessor)} can be used as a
 338      * {@code TemporalQuery} via a method reference, {@code LocalTime::from}.
 339      * This query and {@code LocalTime::from} will return the same result if the
 340      * temporal object contains a time. If the temporal object does not contain
 341      * a time, then the method reference will throw an exception, whereas this
 342      * query will return null.
 343      *
 344      * @return a query that can obtain the time of a temporal, not null
 345      */
 346     public static final TemporalQuery<LocalTime> localTime() {
 347         return LOCAL_TIME;
 348     }
 349     static final TemporalQuery<LocalTime> LOCAL_TIME = (temporal) -> {
 350         if (temporal.isSupported(NANO_OF_DAY)) {
 351             return LocalTime.ofNanoOfDay(temporal.getLong(NANO_OF_DAY));
 352         }
 353         return null;
 354     };
 355 
 356 }
--- EOF ---