src/java.base/share/classes/java/time/temporal/WeekFields.java
author darcy
Thu, 29 Aug 2019 16:31:34 -0700
changeset 57956 e0b8b019d2f5
parent 54206 003cc64366da
permissions -rw-r--r--
8229997: Apply java.io.Serial annotations in java.base Reviewed-by: alanb, rriggs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     1
/*
54206
003cc64366da 8220249: fix headings in java.compiler
jjg
parents: 48251
diff changeset
     2
 * Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     4
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    10
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    15
 * accompanied this code).
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    16
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    20
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    23
 * questions.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    24
 */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    25
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    26
/*
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    27
 * This file is available under and governed by the GNU General Public
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    28
 * License version 2 only, as published by the Free Software Foundation.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    29
 * However, the following notice accompanied the original version of this
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    30
 * file:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    31
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    32
 * Copyright (c) 2011-2012, Stephen Colebourne & Michael Nascimento Santos
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    33
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    34
 * All rights reserved.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    35
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    36
 * Redistribution and use in source and binary forms, with or without
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    37
 * modification, are permitted provided that the following conditions are met:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    38
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    39
 *  * Redistributions of source code must retain the above copyright notice,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    40
 *    this list of conditions and the following disclaimer.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    41
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    42
 *  * Redistributions in binary form must reproduce the above copyright notice,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    43
 *    this list of conditions and the following disclaimer in the documentation
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    44
 *    and/or other materials provided with the distribution.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    45
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    46
 *  * Neither the name of JSR-310 nor the names of its contributors
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    47
 *    may be used to endorse or promote products derived from this software
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    48
 *    without specific prior written permission.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    49
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    50
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    51
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    52
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    53
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    54
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    55
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    56
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    57
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    58
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    59
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    60
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    61
 */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    62
package java.time.temporal;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    63
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    64
import static java.time.temporal.ChronoField.DAY_OF_MONTH;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    65
import static java.time.temporal.ChronoField.DAY_OF_WEEK;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    66
import static java.time.temporal.ChronoField.DAY_OF_YEAR;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    67
import static java.time.temporal.ChronoField.MONTH_OF_YEAR;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    68
import static java.time.temporal.ChronoField.YEAR;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    69
import static java.time.temporal.ChronoUnit.DAYS;
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
    70
import static java.time.temporal.ChronoUnit.FOREVER;
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    71
import static java.time.temporal.ChronoUnit.MONTHS;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    72
import static java.time.temporal.ChronoUnit.WEEKS;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    73
import static java.time.temporal.ChronoUnit.YEARS;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    74
22081
86eb26ff8f2b 8030002: Enhance deserialization using readObject
rriggs
parents: 21331
diff changeset
    75
import java.io.IOException;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    76
import java.io.InvalidObjectException;
22081
86eb26ff8f2b 8030002: Enhance deserialization using readObject
rriggs
parents: 21331
diff changeset
    77
import java.io.ObjectInputStream;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    78
import java.io.Serializable;
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
    79
import java.time.DateTimeException;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    80
import java.time.DayOfWeek;
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    81
import java.time.chrono.ChronoLocalDate;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    82
import java.time.chrono.Chronology;
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
    83
import java.time.format.ResolverStyle;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    84
import java.util.Locale;
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    85
import java.util.Map;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    86
import java.util.Objects;
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
    87
import java.util.ResourceBundle;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    88
import java.util.concurrent.ConcurrentHashMap;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    89
import java.util.concurrent.ConcurrentMap;
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    90
import sun.util.locale.provider.CalendarDataUtility;
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
    91
import sun.util.locale.provider.LocaleProviderAdapter;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
    92
import sun.util.locale.provider.LocaleResources;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    93
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    94
/**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    95
 * Localized definitions of the day-of-week, week-of-month and week-of-year fields.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    96
 * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    97
 * A standard week is seven days long, but cultures have different definitions for some
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    98
 * other aspects of a week. This class represents the definition of the week, for the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    99
 * purpose of providing {@link TemporalField} instances.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   100
 * <p>
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   101
 * WeekFields provides five fields,
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   102
 * {@link #dayOfWeek()}, {@link #weekOfMonth()}, {@link #weekOfYear()},
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   103
 * {@link #weekOfWeekBasedYear()}, and {@link #weekBasedYear()}
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   104
 * that provide access to the values from any {@linkplain Temporal temporal object}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   105
 * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   106
 * The computations for day-of-week, week-of-month, and week-of-year are based
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   107
 * on the  {@linkplain ChronoField#YEAR proleptic-year},
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   108
 * {@linkplain ChronoField#MONTH_OF_YEAR month-of-year},
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   109
 * {@linkplain ChronoField#DAY_OF_MONTH day-of-month}, and
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   110
 * {@linkplain ChronoField#DAY_OF_WEEK ISO day-of-week} which are based on the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   111
 * {@linkplain ChronoField#EPOCH_DAY epoch-day} and the chronology.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   112
 * The values may not be aligned with the {@linkplain ChronoField#YEAR_OF_ERA year-of-Era}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   113
 * depending on the Chronology.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   114
 * <p>A week is defined by:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   115
 * <ul>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   116
 * <li>The first day-of-week.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   117
 * For example, the ISO-8601 standard considers Monday to be the first day-of-week.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   118
 * <li>The minimal number of days in the first week.
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   119
 * For example, the ISO-8601 standard counts the first week as needing at least 4 days.
20873
e91d5b1cb8e6 8026516: javadoc errors in java.time
rriggs
parents: 20793
diff changeset
   120
 * </ul>
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   121
 * Together these two values allow a year or month to be divided into weeks.
20873
e91d5b1cb8e6 8026516: javadoc errors in java.time
rriggs
parents: 20793
diff changeset
   122
 *
54206
003cc64366da 8220249: fix headings in java.compiler
jjg
parents: 48251
diff changeset
   123
 * <h2>Week of Month</h2>
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   124
 * One field is used: week-of-month.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   125
 * The calculation ensures that weeks never overlap a month boundary.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   126
 * The month is divided into periods where each period starts on the defined first day-of-week.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   127
 * The earliest period is referred to as week 0 if it has less than the minimal number of days
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   128
 * and week 1 if it has at least the minimal number of days.
20873
e91d5b1cb8e6 8026516: javadoc errors in java.time
rriggs
parents: 20793
diff changeset
   129
 *
45124
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 25859
diff changeset
   130
 * <table class=striped style="text-align: left">
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   131
 * <caption>Examples of WeekFields</caption>
45124
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 25859
diff changeset
   132
 * <thead>
46149
b515ebc3db98 8186153: Fix a11y and HTML issues in the java.math, java.text and java.time packages
jjg
parents: 45124
diff changeset
   133
 * <tr><th scope="col">Date</th><th scope="col">Day-of-week</th>
b515ebc3db98 8186153: Fix a11y and HTML issues in the java.math, java.text and java.time packages
jjg
parents: 45124
diff changeset
   134
 *  <th scope="col">First day: Monday<br>Minimal days: 4</th><th scope="col">First day: Monday<br>Minimal days: 5</th></tr>
45124
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 25859
diff changeset
   135
 * </thead>
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 25859
diff changeset
   136
 * <tbody>
46149
b515ebc3db98 8186153: Fix a11y and HTML issues in the java.math, java.text and java.time packages
jjg
parents: 45124
diff changeset
   137
 * <tr><th scope="row">2008-12-31</th><td>Wednesday</td>
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   138
 *  <td>Week 5 of December 2008</td><td>Week 5 of December 2008</td></tr>
46149
b515ebc3db98 8186153: Fix a11y and HTML issues in the java.math, java.text and java.time packages
jjg
parents: 45124
diff changeset
   139
 * <tr><th scope="row">2009-01-01</th><td>Thursday</td>
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   140
 *  <td>Week 1 of January 2009</td><td>Week 0 of January 2009</td></tr>
46149
b515ebc3db98 8186153: Fix a11y and HTML issues in the java.math, java.text and java.time packages
jjg
parents: 45124
diff changeset
   141
 * <tr><th scope="row">2009-01-04</th><td>Sunday</td>
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   142
 *  <td>Week 1 of January 2009</td><td>Week 0 of January 2009</td></tr>
46149
b515ebc3db98 8186153: Fix a11y and HTML issues in the java.math, java.text and java.time packages
jjg
parents: 45124
diff changeset
   143
 * <tr><th scope="row">2009-01-05</th><td>Monday</td>
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   144
 *  <td>Week 2 of January 2009</td><td>Week 1 of January 2009</td></tr>
45124
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 25859
diff changeset
   145
 * </tbody>
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   146
 * </table>
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   147
 *
54206
003cc64366da 8220249: fix headings in java.compiler
jjg
parents: 48251
diff changeset
   148
 * <h2>Week of Year</h2>
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   149
 * One field is used: week-of-year.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   150
 * The calculation ensures that weeks never overlap a year boundary.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   151
 * The year is divided into periods where each period starts on the defined first day-of-week.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   152
 * The earliest period is referred to as week 0 if it has less than the minimal number of days
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   153
 * and week 1 if it has at least the minimal number of days.
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   154
 *
54206
003cc64366da 8220249: fix headings in java.compiler
jjg
parents: 48251
diff changeset
   155
 * <h2>Week Based Year</h2>
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   156
 * Two fields are used for week-based-year, one for the
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   157
 * {@link #weekOfWeekBasedYear() week-of-week-based-year} and one for
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   158
 * {@link #weekBasedYear() week-based-year}.  In a week-based-year, each week
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   159
 * belongs to only a single year.  Week 1 of a year is the first week that
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   160
 * starts on the first day-of-week and has at least the minimum number of days.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   161
 * The first and last weeks of a year may contain days from the
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   162
 * previous calendar year or next calendar year respectively.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   163
 *
45124
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 25859
diff changeset
   164
 * <table class=striped style="text-align: left;">
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   165
 * <caption>Examples of WeekFields for week-based-year</caption>
45124
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 25859
diff changeset
   166
 * <thead>
46149
b515ebc3db98 8186153: Fix a11y and HTML issues in the java.math, java.text and java.time packages
jjg
parents: 45124
diff changeset
   167
 * <tr><th scope="col">Date</th><th scope="col">Day-of-week</th>
b515ebc3db98 8186153: Fix a11y and HTML issues in the java.math, java.text and java.time packages
jjg
parents: 45124
diff changeset
   168
 *  <th scope="col">First day: Monday<br>Minimal days: 4</th><th scope="col">First day: Monday<br>Minimal days: 5</th></tr>
45124
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 25859
diff changeset
   169
 * </thead>
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 25859
diff changeset
   170
 * <tbody>
46149
b515ebc3db98 8186153: Fix a11y and HTML issues in the java.math, java.text and java.time packages
jjg
parents: 45124
diff changeset
   171
 * <tr><th scope="row">2008-12-31</th><td>Wednesday</td>
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   172
 *  <td>Week 1 of 2009</td><td>Week 53 of 2008</td></tr>
46149
b515ebc3db98 8186153: Fix a11y and HTML issues in the java.math, java.text and java.time packages
jjg
parents: 45124
diff changeset
   173
 * <tr><th scope="row">2009-01-01</th><td>Thursday</td>
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   174
 *  <td>Week 1 of 2009</td><td>Week 53 of 2008</td></tr>
46149
b515ebc3db98 8186153: Fix a11y and HTML issues in the java.math, java.text and java.time packages
jjg
parents: 45124
diff changeset
   175
 * <tr><th scope="row">2009-01-04</th><td>Sunday</td>
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   176
 *  <td>Week 1 of 2009</td><td>Week 53 of 2008</td></tr>
46149
b515ebc3db98 8186153: Fix a11y and HTML issues in the java.math, java.text and java.time packages
jjg
parents: 45124
diff changeset
   177
 * <tr><th scope="row">2009-01-05</th><td>Monday</td>
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   178
 *  <td>Week 2 of 2009</td><td>Week 1 of 2009</td></tr>
45124
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 25859
diff changeset
   179
 * </tbody>
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   180
 * </table>
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   181
 *
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   182
 * @implSpec
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   183
 * This class is immutable and thread-safe.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   184
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   185
 * @since 1.8
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   186
 */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   187
public final class WeekFields implements Serializable {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   188
    // implementation notes
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   189
    // querying week-of-month or week-of-year should return the week value bound within the month/year
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   190
    // however, setting the week value should be lenient (use plus/minus weeks)
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   191
    // allow week-of-month outer range [0 to 6]
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   192
    // allow week-of-year outer range [0 to 54]
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   193
    // this is because callers shouldn't be expected to know the details of validity
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   194
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   195
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   196
     * The cache of rules by firstDayOfWeek plus minimalDays.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   197
     * Initialized first to be available for definition of ISO, etc.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   198
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   199
    private static final ConcurrentMap<String, WeekFields> CACHE = new ConcurrentHashMap<>(4, 0.75f, 2);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   200
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   201
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   202
     * The ISO-8601 definition, where a week starts on Monday and the first week
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   203
     * has a minimum of 4 days.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   204
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   205
     * The ISO-8601 standard defines a calendar system based on weeks.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   206
     * It uses the week-based-year and week-of-week-based-year concepts to split
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   207
     * up the passage of days instead of the standard year/month/day.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   208
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   209
     * Note that the first week may start in the previous calendar year.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   210
     * Note also that the first few days of a calendar year may be in the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   211
     * week-based-year corresponding to the previous calendar year.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   212
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   213
    public static final WeekFields ISO = new WeekFields(DayOfWeek.MONDAY, 4);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   214
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   215
    /**
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   216
     * The common definition of a week that starts on Sunday and the first week
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   217
     * has a minimum of 1 day.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   218
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   219
     * Defined as starting on Sunday and with a minimum of 1 day in the month.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   220
     * This week definition is in use in the US and other European countries.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   221
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   222
    public static final WeekFields SUNDAY_START = WeekFields.of(DayOfWeek.SUNDAY, 1);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   223
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   224
    /**
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   225
     * The unit that represents week-based-years for the purpose of addition and subtraction.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   226
     * <p>
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   227
     * This allows a number of week-based-years to be added to, or subtracted from, a date.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   228
     * The unit is equal to either 52 or 53 weeks.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   229
     * The estimated duration of a week-based-year is the same as that of a standard ISO
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   230
     * year at {@code 365.2425 Days}.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   231
     * <p>
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   232
     * The rules for addition add the number of week-based-years to the existing value
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   233
     * for the week-based-year field retaining the week-of-week-based-year
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   234
     * and day-of-week, unless the week number it too large for the target year.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   235
     * In that case, the week is set to the last week of the year
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   236
     * with the same day-of-week.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   237
     * <p>
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   238
     * This unit is an immutable and thread-safe singleton.
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   239
     */
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   240
    public static final TemporalUnit WEEK_BASED_YEARS = IsoFields.WEEK_BASED_YEARS;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   241
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   242
    /**
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   243
     * Serialization version.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   244
     */
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 54206
diff changeset
   245
    @java.io.Serial
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   246
    private static final long serialVersionUID = -1177360819670808121L;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   247
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   248
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   249
     * The first day-of-week.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   250
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   251
    private final DayOfWeek firstDayOfWeek;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   252
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   253
     * The minimal number of days in the first week.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   254
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   255
    private final int minimalDays;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   256
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   257
     * The field used to access the computed DayOfWeek.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   258
     */
21331
8ee181e7e48b 8024686: Cleanup of java.time serialization source
rriggs
parents: 20873
diff changeset
   259
    private final transient TemporalField dayOfWeek = ComputedDayOfField.ofDayOfWeekField(this);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   260
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   261
     * The field used to access the computed WeekOfMonth.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   262
     */
21331
8ee181e7e48b 8024686: Cleanup of java.time serialization source
rriggs
parents: 20873
diff changeset
   263
    private final transient TemporalField weekOfMonth = ComputedDayOfField.ofWeekOfMonthField(this);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   264
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   265
     * The field used to access the computed WeekOfYear.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   266
     */
21331
8ee181e7e48b 8024686: Cleanup of java.time serialization source
rriggs
parents: 20873
diff changeset
   267
    private final transient TemporalField weekOfYear = ComputedDayOfField.ofWeekOfYearField(this);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   268
    /**
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   269
     * The field that represents the week-of-week-based-year.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   270
     * <p>
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   271
     * This field allows the week of the week-based-year value to be queried and set.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   272
     * <p>
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   273
     * This unit is an immutable and thread-safe singleton.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   274
     */
21331
8ee181e7e48b 8024686: Cleanup of java.time serialization source
rriggs
parents: 20873
diff changeset
   275
    private final transient TemporalField weekOfWeekBasedYear = ComputedDayOfField.ofWeekOfWeekBasedYearField(this);
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   276
    /**
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   277
     * The field that represents the week-based-year.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   278
     * <p>
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   279
     * This field allows the week-based-year value to be queried and set.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   280
     * <p>
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   281
     * This unit is an immutable and thread-safe singleton.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   282
     */
21331
8ee181e7e48b 8024686: Cleanup of java.time serialization source
rriggs
parents: 20873
diff changeset
   283
    private final transient TemporalField weekBasedYear = ComputedDayOfField.ofWeekBasedYearField(this);
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   284
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   285
    //-----------------------------------------------------------------------
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   286
    /**
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   287
     * Obtains an instance of {@code WeekFields} appropriate for a locale.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   288
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   289
     * This will look up appropriate values from the provider of localization data.
48251
57148c79bd75 8176841: Additional Unicode Language-Tag Extensions
naoto
parents: 47478
diff changeset
   290
     * If the locale contains "fw" (First day of week) and/or "rg"
57148c79bd75 8176841: Additional Unicode Language-Tag Extensions
naoto
parents: 47478
diff changeset
   291
     * (Region Override) <a href="../../util/Locale.html#def_locale_extension">
57148c79bd75 8176841: Additional Unicode Language-Tag Extensions
naoto
parents: 47478
diff changeset
   292
     * Unicode extensions</a>, returned instance will reflect the values specified with
57148c79bd75 8176841: Additional Unicode Language-Tag Extensions
naoto
parents: 47478
diff changeset
   293
     * those extensions. If both "fw" and "rg" are specified, the value from
57148c79bd75 8176841: Additional Unicode Language-Tag Extensions
naoto
parents: 47478
diff changeset
   294
     * the "fw" extension supersedes the implicit one from the "rg" extension.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   295
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   296
     * @param locale  the locale to use, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   297
     * @return the week-definition, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   298
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   299
    public static WeekFields of(Locale locale) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   300
        Objects.requireNonNull(locale, "locale");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   301
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   302
        int calDow = CalendarDataUtility.retrieveFirstDayOfWeek(locale);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   303
        DayOfWeek dow = DayOfWeek.SUNDAY.plus(calDow - 1);
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   304
        int minDays = CalendarDataUtility.retrieveMinimalDaysInFirstWeek(locale);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   305
        return WeekFields.of(dow, minDays);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   306
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   307
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   308
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   309
     * Obtains an instance of {@code WeekFields} from the first day-of-week and minimal days.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   310
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   311
     * The first day-of-week defines the ISO {@code DayOfWeek} that is day 1 of the week.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   312
     * The minimal number of days in the first week defines how many days must be present
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   313
     * in a month or year, starting from the first day-of-week, before the week is counted
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   314
     * as the first week. A value of 1 will count the first day of the month or year as part
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   315
     * of the first week, whereas a value of 7 will require the whole seven days to be in
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   316
     * the new month or year.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   317
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   318
     * WeekFields instances are singletons; for each unique combination
47478
438e0c9f2f17 8190382: fix small typographic errors in comments
smarks
parents: 47216
diff changeset
   319
     * of {@code firstDayOfWeek} and {@code minimalDaysInFirstWeek}
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   320
     * the same instance will be returned.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   321
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   322
     * @param firstDayOfWeek  the first day of the week, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   323
     * @param minimalDaysInFirstWeek  the minimal number of days in the first week, from 1 to 7
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   324
     * @return the week-definition, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   325
     * @throws IllegalArgumentException if the minimal days value is less than one
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   326
     *      or greater than 7
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   327
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   328
    public static WeekFields of(DayOfWeek firstDayOfWeek, int minimalDaysInFirstWeek) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   329
        String key = firstDayOfWeek.toString() + minimalDaysInFirstWeek;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   330
        WeekFields rules = CACHE.get(key);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   331
        if (rules == null) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   332
            rules = new WeekFields(firstDayOfWeek, minimalDaysInFirstWeek);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   333
            CACHE.putIfAbsent(key, rules);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   334
            rules = CACHE.get(key);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   335
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   336
        return rules;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   337
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   338
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   339
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   340
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   341
     * Creates an instance of the definition.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   342
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   343
     * @param firstDayOfWeek  the first day of the week, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   344
     * @param minimalDaysInFirstWeek  the minimal number of days in the first week, from 1 to 7
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   345
     * @throws IllegalArgumentException if the minimal days value is invalid
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   346
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   347
    private WeekFields(DayOfWeek firstDayOfWeek, int minimalDaysInFirstWeek) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   348
        Objects.requireNonNull(firstDayOfWeek, "firstDayOfWeek");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   349
        if (minimalDaysInFirstWeek < 1 || minimalDaysInFirstWeek > 7) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   350
            throw new IllegalArgumentException("Minimal number of days is invalid");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   351
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   352
        this.firstDayOfWeek = firstDayOfWeek;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   353
        this.minimalDays = minimalDaysInFirstWeek;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   354
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   355
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   356
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   357
    /**
22081
86eb26ff8f2b 8030002: Enhance deserialization using readObject
rriggs
parents: 21331
diff changeset
   358
     * Restore the state of a WeekFields from the stream.
86eb26ff8f2b 8030002: Enhance deserialization using readObject
rriggs
parents: 21331
diff changeset
   359
     * Check that the values are valid.
22566
4ebe53dd7814 8032502: java.time add @param tags to readObject
rriggs
parents: 22081
diff changeset
   360
     *
4ebe53dd7814 8032502: java.time add @param tags to readObject
rriggs
parents: 22081
diff changeset
   361
     * @param s the stream to read
22081
86eb26ff8f2b 8030002: Enhance deserialization using readObject
rriggs
parents: 21331
diff changeset
   362
     * @throws InvalidObjectException if the serialized object has an invalid
86eb26ff8f2b 8030002: Enhance deserialization using readObject
rriggs
parents: 21331
diff changeset
   363
     *     value for firstDayOfWeek or minimalDays.
22566
4ebe53dd7814 8032502: java.time add @param tags to readObject
rriggs
parents: 22081
diff changeset
   364
     * @throws ClassNotFoundException if a class cannot be resolved
22081
86eb26ff8f2b 8030002: Enhance deserialization using readObject
rriggs
parents: 21331
diff changeset
   365
     */
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 54206
diff changeset
   366
    @java.io.Serial
22081
86eb26ff8f2b 8030002: Enhance deserialization using readObject
rriggs
parents: 21331
diff changeset
   367
    private void readObject(ObjectInputStream s)
86eb26ff8f2b 8030002: Enhance deserialization using readObject
rriggs
parents: 21331
diff changeset
   368
         throws IOException, ClassNotFoundException, InvalidObjectException
86eb26ff8f2b 8030002: Enhance deserialization using readObject
rriggs
parents: 21331
diff changeset
   369
    {
86eb26ff8f2b 8030002: Enhance deserialization using readObject
rriggs
parents: 21331
diff changeset
   370
        s.defaultReadObject();
86eb26ff8f2b 8030002: Enhance deserialization using readObject
rriggs
parents: 21331
diff changeset
   371
        if (firstDayOfWeek == null) {
86eb26ff8f2b 8030002: Enhance deserialization using readObject
rriggs
parents: 21331
diff changeset
   372
            throw new InvalidObjectException("firstDayOfWeek is null");
86eb26ff8f2b 8030002: Enhance deserialization using readObject
rriggs
parents: 21331
diff changeset
   373
        }
86eb26ff8f2b 8030002: Enhance deserialization using readObject
rriggs
parents: 21331
diff changeset
   374
86eb26ff8f2b 8030002: Enhance deserialization using readObject
rriggs
parents: 21331
diff changeset
   375
        if (minimalDays < 1 || minimalDays > 7) {
86eb26ff8f2b 8030002: Enhance deserialization using readObject
rriggs
parents: 21331
diff changeset
   376
            throw new InvalidObjectException("Minimal number of days is invalid");
86eb26ff8f2b 8030002: Enhance deserialization using readObject
rriggs
parents: 21331
diff changeset
   377
        }
86eb26ff8f2b 8030002: Enhance deserialization using readObject
rriggs
parents: 21331
diff changeset
   378
    }
86eb26ff8f2b 8030002: Enhance deserialization using readObject
rriggs
parents: 21331
diff changeset
   379
86eb26ff8f2b 8030002: Enhance deserialization using readObject
rriggs
parents: 21331
diff changeset
   380
    /**
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   381
     * Return the singleton WeekFields associated with the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   382
     * {@code firstDayOfWeek} and {@code minimalDays}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   383
     * @return the singleton WeekFields for the firstDayOfWeek and minimalDays.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   384
     * @throws InvalidObjectException if the serialized object has invalid
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   385
     *     values for firstDayOfWeek or minimalDays.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   386
     */
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 54206
diff changeset
   387
    @java.io.Serial
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   388
    private Object readResolve() throws InvalidObjectException {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   389
        try {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   390
            return WeekFields.of(firstDayOfWeek, minimalDays);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   391
        } catch (IllegalArgumentException iae) {
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   392
            throw new InvalidObjectException("Invalid serialized WeekFields: " + iae.getMessage());
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   393
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   394
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   395
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   396
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   397
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   398
     * Gets the first day-of-week.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   399
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   400
     * The first day-of-week varies by culture.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   401
     * For example, the US uses Sunday, while France and the ISO-8601 standard use Monday.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   402
     * This method returns the first day using the standard {@code DayOfWeek} enum.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   403
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   404
     * @return the first day-of-week, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   405
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   406
    public DayOfWeek getFirstDayOfWeek() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   407
        return firstDayOfWeek;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   408
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   409
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   410
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   411
     * Gets the minimal number of days in the first week.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   412
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   413
     * The number of days considered to define the first week of a month or year
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   414
     * varies by culture.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   415
     * For example, the ISO-8601 requires 4 days (more than half a week) to
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   416
     * be present before counting the first week.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   417
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   418
     * @return the minimal number of days in the first week of a month or year, from 1 to 7
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   419
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   420
    public int getMinimalDaysInFirstWeek() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   421
        return minimalDays;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   422
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   423
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   424
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   425
    /**
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   426
     * Returns a field to access the day of week based on this {@code WeekFields}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   427
     * <p>
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   428
     * This is similar to {@link ChronoField#DAY_OF_WEEK} but uses values for
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   429
     * the day-of-week based on this {@code WeekFields}.
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   430
     * The days are numbered from 1 to 7 where the
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   431
     * {@link #getFirstDayOfWeek() first day-of-week} is assigned the value 1.
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   432
     * <p>
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   433
     * For example, if the first day-of-week is Sunday, then that will have the
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   434
     * value 1, with other days ranging from Monday as 2 to Saturday as 7.
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   435
     * <p>
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   436
     * In the resolving phase of parsing, a localized day-of-week will be converted
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   437
     * to a standardized {@code ChronoField} day-of-week.
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   438
     * The day-of-week must be in the valid range 1 to 7.
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   439
     * Other fields in this class build dates using the standardized day-of-week.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   440
     *
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   441
     * @return a field providing access to the day-of-week with localized numbering, not null
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   442
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   443
    public TemporalField dayOfWeek() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   444
        return dayOfWeek;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   445
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   446
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   447
    /**
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   448
     * Returns a field to access the week of month based on this {@code WeekFields}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   449
     * <p>
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   450
     * This represents the concept of the count of weeks within the month where weeks
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   451
     * start on a fixed day-of-week, such as Monday.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   452
     * This field is typically used with {@link WeekFields#dayOfWeek()}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   453
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   454
     * Week one (1) is the week starting on the {@link WeekFields#getFirstDayOfWeek}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   455
     * where there are at least {@link WeekFields#getMinimalDaysInFirstWeek()} days in the month.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   456
     * Thus, week one may start up to {@code minDays} days before the start of the month.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   457
     * If the first week starts after the start of the month then the period before is week zero (0).
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   458
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   459
     * For example:<br>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   460
     * - if the 1st day of the month is a Monday, week one starts on the 1st and there is no week zero<br>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   461
     * - if the 2nd day of the month is a Monday, week one starts on the 2nd and the 1st is in week zero<br>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   462
     * - if the 4th day of the month is a Monday, week one starts on the 4th and the 1st to 3rd is in week zero<br>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   463
     * - if the 5th day of the month is a Monday, week two starts on the 5th and the 1st to 4th is in week one<br>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   464
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   465
     * This field can be used with any calendar system.
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   466
     * <p>
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   467
     * In the resolving phase of parsing, a date can be created from a year,
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   468
     * week-of-month, month-of-year and day-of-week.
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   469
     * <p>
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   470
     * In {@linkplain ResolverStyle#STRICT strict mode}, all four fields are
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   471
     * validated against their range of valid values. The week-of-month field
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   472
     * is validated to ensure that the resulting month is the month requested.
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   473
     * <p>
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   474
     * In {@linkplain ResolverStyle#SMART smart mode}, all four fields are
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   475
     * validated against their range of valid values. The week-of-month field
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   476
     * is validated from 0 to 6, meaning that the resulting date can be in a
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   477
     * different month to that specified.
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   478
     * <p>
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   479
     * In {@linkplain ResolverStyle#LENIENT lenient mode}, the year and day-of-week
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   480
     * are validated against the range of valid values. The resulting date is calculated
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   481
     * equivalent to the following four stage approach.
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   482
     * First, create a date on the first day of the first week of January in the requested year.
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   483
     * Then take the month-of-year, subtract one, and add the amount in months to the date.
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   484
     * Then take the week-of-month, subtract one, and add the amount in weeks to the date.
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   485
     * Finally, adjust to the correct day-of-week within the localized week.
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   486
     *
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   487
     * @return a field providing access to the week-of-month, not null
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   488
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   489
    public TemporalField weekOfMonth() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   490
        return weekOfMonth;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   491
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   492
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   493
    /**
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   494
     * Returns a field to access the week of year based on this {@code WeekFields}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   495
     * <p>
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   496
     * This represents the concept of the count of weeks within the year where weeks
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   497
     * start on a fixed day-of-week, such as Monday.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   498
     * This field is typically used with {@link WeekFields#dayOfWeek()}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   499
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   500
     * Week one(1) is the week starting on the {@link WeekFields#getFirstDayOfWeek}
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   501
     * where there are at least {@link WeekFields#getMinimalDaysInFirstWeek()} days in the year.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   502
     * Thus, week one may start up to {@code minDays} days before the start of the year.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   503
     * If the first week starts after the start of the year then the period before is week zero (0).
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   504
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   505
     * For example:<br>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   506
     * - if the 1st day of the year is a Monday, week one starts on the 1st and there is no week zero<br>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   507
     * - if the 2nd day of the year is a Monday, week one starts on the 2nd and the 1st is in week zero<br>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   508
     * - if the 4th day of the year is a Monday, week one starts on the 4th and the 1st to 3rd is in week zero<br>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   509
     * - if the 5th day of the year is a Monday, week two starts on the 5th and the 1st to 4th is in week one<br>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   510
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   511
     * This field can be used with any calendar system.
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   512
     * <p>
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   513
     * In the resolving phase of parsing, a date can be created from a year,
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   514
     * week-of-year and day-of-week.
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   515
     * <p>
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   516
     * In {@linkplain ResolverStyle#STRICT strict mode}, all three fields are
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   517
     * validated against their range of valid values. The week-of-year field
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   518
     * is validated to ensure that the resulting year is the year requested.
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   519
     * <p>
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   520
     * In {@linkplain ResolverStyle#SMART smart mode}, all three fields are
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   521
     * validated against their range of valid values. The week-of-year field
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   522
     * is validated from 0 to 54, meaning that the resulting date can be in a
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   523
     * different year to that specified.
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   524
     * <p>
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   525
     * In {@linkplain ResolverStyle#LENIENT lenient mode}, the year and day-of-week
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   526
     * are validated against the range of valid values. The resulting date is calculated
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   527
     * equivalent to the following three stage approach.
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   528
     * First, create a date on the first day of the first week in the requested year.
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   529
     * Then take the week-of-year, subtract one, and add the amount in weeks to the date.
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   530
     * Finally, adjust to the correct day-of-week within the localized week.
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   531
     *
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   532
     * @return a field providing access to the week-of-year, not null
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   533
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   534
    public TemporalField weekOfYear() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   535
        return weekOfYear;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   536
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   537
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   538
    /**
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   539
     * Returns a field to access the week of a week-based-year based on this {@code WeekFields}.
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   540
     * <p>
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   541
     * This represents the concept of the count of weeks within the year where weeks
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   542
     * start on a fixed day-of-week, such as Monday and each week belongs to exactly one year.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   543
     * This field is typically used with {@link WeekFields#dayOfWeek()} and
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   544
     * {@link WeekFields#weekBasedYear()}.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   545
     * <p>
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   546
     * Week one(1) is the week starting on the {@link WeekFields#getFirstDayOfWeek}
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   547
     * where there are at least {@link WeekFields#getMinimalDaysInFirstWeek()} days in the year.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   548
     * If the first week starts after the start of the year then the period before
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   549
     * is in the last week of the previous year.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   550
     * <p>
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   551
     * For example:<br>
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   552
     * - if the 1st day of the year is a Monday, week one starts on the 1st<br>
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   553
     * - if the 2nd day of the year is a Monday, week one starts on the 2nd and
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   554
     *   the 1st is in the last week of the previous year<br>
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   555
     * - if the 4th day of the year is a Monday, week one starts on the 4th and
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   556
     *   the 1st to 3rd is in the last week of the previous year<br>
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   557
     * - if the 5th day of the year is a Monday, week two starts on the 5th and
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   558
     *   the 1st to 4th is in week one<br>
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   559
     * <p>
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   560
     * This field can be used with any calendar system.
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   561
     * <p>
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   562
     * In the resolving phase of parsing, a date can be created from a week-based-year,
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   563
     * week-of-year and day-of-week.
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   564
     * <p>
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   565
     * In {@linkplain ResolverStyle#STRICT strict mode}, all three fields are
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   566
     * validated against their range of valid values. The week-of-year field
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   567
     * is validated to ensure that the resulting week-based-year is the
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   568
     * week-based-year requested.
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   569
     * <p>
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   570
     * In {@linkplain ResolverStyle#SMART smart mode}, all three fields are
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   571
     * validated against their range of valid values. The week-of-week-based-year field
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   572
     * is validated from 1 to 53, meaning that the resulting date can be in the
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   573
     * following week-based-year to that specified.
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   574
     * <p>
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   575
     * In {@linkplain ResolverStyle#LENIENT lenient mode}, the year and day-of-week
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   576
     * are validated against the range of valid values. The resulting date is calculated
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   577
     * equivalent to the following three stage approach.
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   578
     * First, create a date on the first day of the first week in the requested week-based-year.
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   579
     * Then take the week-of-week-based-year, subtract one, and add the amount in weeks to the date.
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   580
     * Finally, adjust to the correct day-of-week within the localized week.
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   581
     *
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   582
     * @return a field providing access to the week-of-week-based-year, not null
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   583
     */
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   584
    public TemporalField weekOfWeekBasedYear() {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   585
        return weekOfWeekBasedYear;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   586
    }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   587
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   588
    /**
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   589
     * Returns a field to access the year of a week-based-year based on this {@code WeekFields}.
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   590
     * <p>
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   591
     * This represents the concept of the year where weeks start on a fixed day-of-week,
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   592
     * such as Monday and each week belongs to exactly one year.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   593
     * This field is typically used with {@link WeekFields#dayOfWeek()} and
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   594
     * {@link WeekFields#weekOfWeekBasedYear()}.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   595
     * <p>
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   596
     * Week one(1) is the week starting on the {@link WeekFields#getFirstDayOfWeek}
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   597
     * where there are at least {@link WeekFields#getMinimalDaysInFirstWeek()} days in the year.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   598
     * Thus, week one may start before the start of the year.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   599
     * If the first week starts after the start of the year then the period before
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   600
     * is in the last week of the previous year.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   601
     * <p>
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   602
     * This field can be used with any calendar system.
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   603
     * <p>
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   604
     * In the resolving phase of parsing, a date can be created from a week-based-year,
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   605
     * week-of-year and day-of-week.
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   606
     * <p>
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   607
     * In {@linkplain ResolverStyle#STRICT strict mode}, all three fields are
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   608
     * validated against their range of valid values. The week-of-year field
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   609
     * is validated to ensure that the resulting week-based-year is the
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   610
     * week-based-year requested.
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   611
     * <p>
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   612
     * In {@linkplain ResolverStyle#SMART smart mode}, all three fields are
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   613
     * validated against their range of valid values. The week-of-week-based-year field
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   614
     * is validated from 1 to 53, meaning that the resulting date can be in the
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   615
     * following week-based-year to that specified.
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   616
     * <p>
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   617
     * In {@linkplain ResolverStyle#LENIENT lenient mode}, the year and day-of-week
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   618
     * are validated against the range of valid values. The resulting date is calculated
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   619
     * equivalent to the following three stage approach.
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   620
     * First, create a date on the first day of the first week in the requested week-based-year.
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   621
     * Then take the week-of-week-based-year, subtract one, and add the amount in weeks to the date.
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   622
     * Finally, adjust to the correct day-of-week within the localized week.
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   623
     *
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   624
     * @return a field providing access to the week-based-year, not null
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   625
     */
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   626
    public TemporalField weekBasedYear() {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   627
        return weekBasedYear;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   628
    }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   629
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   630
    //-----------------------------------------------------------------------
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   631
    /**
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   632
     * Checks if this {@code WeekFields} is equal to the specified object.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   633
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   634
     * The comparison is based on the entire state of the rules, which is
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   635
     * the first day-of-week and minimal days.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   636
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   637
     * @param object  the other rules to compare to, null returns false
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   638
     * @return true if this is equal to the specified rules
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   639
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   640
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   641
    public boolean equals(Object object) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   642
        if (this == object) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   643
            return true;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   644
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   645
        if (object instanceof WeekFields) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   646
            return hashCode() == object.hashCode();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   647
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   648
        return false;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   649
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   650
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   651
    /**
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   652
     * A hash code for this {@code WeekFields}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   653
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   654
     * @return a suitable hash code
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   655
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   656
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   657
    public int hashCode() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   658
        return firstDayOfWeek.ordinal() * 7 + minimalDays;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   659
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   660
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   661
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   662
    /**
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   663
     * A string representation of this {@code WeekFields} instance.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   664
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   665
     * @return the string representation, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   666
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   667
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   668
    public String toString() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   669
        return "WeekFields[" + firstDayOfWeek + ',' + minimalDays + ']';
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   670
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   671
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   672
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   673
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   674
     * Field type that computes DayOfWeek, WeekOfMonth, and WeekOfYear
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   675
     * based on a WeekFields.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   676
     * A separate Field instance is required for each different WeekFields;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   677
     * combination of start of week and minimum number of days.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   678
     * Constructors are provided to create fields for DayOfWeek, WeekOfMonth,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   679
     * and WeekOfYear.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   680
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   681
    static class ComputedDayOfField implements TemporalField {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   682
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   683
        /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   684
         * Returns a field to access the day of week,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   685
         * computed based on a WeekFields.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   686
         * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   687
         * The WeekDefintion of the first day of the week is used with
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   688
         * the ISO DAY_OF_WEEK field to compute week boundaries.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   689
         */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   690
        static ComputedDayOfField ofDayOfWeekField(WeekFields weekDef) {
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   691
            return new ComputedDayOfField("DayOfWeek", weekDef, DAYS, WEEKS, DAY_OF_WEEK_RANGE);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   692
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   693
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   694
        /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   695
         * Returns a field to access the week of month,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   696
         * computed based on a WeekFields.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   697
         * @see WeekFields#weekOfMonth()
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   698
         */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   699
        static ComputedDayOfField ofWeekOfMonthField(WeekFields weekDef) {
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   700
            return new ComputedDayOfField("WeekOfMonth", weekDef, WEEKS, MONTHS, WEEK_OF_MONTH_RANGE);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   701
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   702
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   703
        /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   704
         * Returns a field to access the week of year,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   705
         * computed based on a WeekFields.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   706
         * @see WeekFields#weekOfYear()
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   707
         */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   708
        static ComputedDayOfField ofWeekOfYearField(WeekFields weekDef) {
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   709
            return new ComputedDayOfField("WeekOfYear", weekDef, WEEKS, YEARS, WEEK_OF_YEAR_RANGE);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   710
        }
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   711
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   712
        /**
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   713
         * Returns a field to access the week of week-based-year,
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   714
         * computed based on a WeekFields.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   715
         * @see WeekFields#weekOfWeekBasedYear()
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   716
         */
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   717
        static ComputedDayOfField ofWeekOfWeekBasedYearField(WeekFields weekDef) {
24032
a25d367576e8 8040058: IsoFields.WEEK_BASED_YEAR adjustInto incorrect and WeekFields.weekOfWeekBasedYear().range incorrect
scolebourne
parents: 22566
diff changeset
   718
            return new ComputedDayOfField("WeekOfWeekBasedYear", weekDef, WEEKS, IsoFields.WEEK_BASED_YEARS, WEEK_OF_WEEK_BASED_YEAR_RANGE);
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   719
        }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   720
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   721
        /**
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   722
         * Returns a field to access the week of week-based-year,
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   723
         * computed based on a WeekFields.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   724
         * @see WeekFields#weekBasedYear()
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   725
         */
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   726
        static ComputedDayOfField ofWeekBasedYearField(WeekFields weekDef) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   727
            return new ComputedDayOfField("WeekBasedYear", weekDef, IsoFields.WEEK_BASED_YEARS, FOREVER, ChronoField.YEAR.range());
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   728
        }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   729
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   730
        /**
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   731
         * Return a new week-based-year date of the Chronology, year, week-of-year,
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   732
         * and dow of week.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   733
         * @param chrono The chronology of the new date
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   734
         * @param yowby the year of the week-based-year
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   735
         * @param wowby the week of the week-based-year
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   736
         * @param dow the day of the week
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   737
         * @return a ChronoLocalDate for the requested year, week of year, and day of week
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   738
         */
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   739
        private ChronoLocalDate ofWeekBasedYear(Chronology chrono,
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   740
                int yowby, int wowby, int dow) {
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   741
            ChronoLocalDate date = chrono.date(yowby, 1, 1);
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   742
            int ldow = localizedDayOfWeek(date);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   743
            int offset = startOfWeekOffset(1, ldow);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   744
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   745
            // Clamp the week of year to keep it in the same year
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   746
            int yearLen = date.lengthOfYear();
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   747
            int newYearWeek = computeWeek(offset, yearLen + weekDef.getMinimalDaysInFirstWeek());
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   748
            wowby = Math.min(wowby, newYearWeek - 1);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   749
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   750
            int days = -offset + (dow - 1) + (wowby - 1) * 7;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   751
            return date.plus(days, DAYS);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   752
        }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   753
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   754
        private final String name;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   755
        private final WeekFields weekDef;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   756
        private final TemporalUnit baseUnit;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   757
        private final TemporalUnit rangeUnit;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   758
        private final ValueRange range;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   759
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   760
        private ComputedDayOfField(String name, WeekFields weekDef, TemporalUnit baseUnit, TemporalUnit rangeUnit, ValueRange range) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   761
            this.name = name;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   762
            this.weekDef = weekDef;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   763
            this.baseUnit = baseUnit;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   764
            this.rangeUnit = rangeUnit;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   765
            this.range = range;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   766
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   767
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   768
        private static final ValueRange DAY_OF_WEEK_RANGE = ValueRange.of(1, 7);
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   769
        private static final ValueRange WEEK_OF_MONTH_RANGE = ValueRange.of(0, 1, 4, 6);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   770
        private static final ValueRange WEEK_OF_YEAR_RANGE = ValueRange.of(0, 1, 52, 54);
24032
a25d367576e8 8040058: IsoFields.WEEK_BASED_YEAR adjustInto incorrect and WeekFields.weekOfWeekBasedYear().range incorrect
scolebourne
parents: 22566
diff changeset
   771
        private static final ValueRange WEEK_OF_WEEK_BASED_YEAR_RANGE = ValueRange.of(1, 52, 53);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   772
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   773
        @Override
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   774
        public long getFrom(TemporalAccessor temporal) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   775
            if (rangeUnit == WEEKS) {  // day-of-week
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   776
                return localizedDayOfWeek(temporal);
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   777
            } else if (rangeUnit == MONTHS) {  // week-of-month
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   778
                return localizedWeekOfMonth(temporal);
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   779
            } else if (rangeUnit == YEARS) {  // week-of-year
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   780
                return localizedWeekOfYear(temporal);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   781
            } else if (rangeUnit == WEEK_BASED_YEARS) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   782
                return localizedWeekOfWeekBasedYear(temporal);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   783
            } else if (rangeUnit == FOREVER) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   784
                return localizedWeekBasedYear(temporal);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   785
            } else {
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   786
                throw new IllegalStateException("unreachable, rangeUnit: " + rangeUnit + ", this: " + this);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   787
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   788
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   789
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   790
        private int localizedDayOfWeek(TemporalAccessor temporal) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   791
            int sow = weekDef.getFirstDayOfWeek().getValue();
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   792
            int isoDow = temporal.get(DAY_OF_WEEK);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   793
            return Math.floorMod(isoDow - sow, 7) + 1;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   794
        }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   795
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   796
        private int localizedDayOfWeek(int isoDow) {
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   797
            int sow = weekDef.getFirstDayOfWeek().getValue();
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   798
            return Math.floorMod(isoDow - sow, 7) + 1;
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   799
        }
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   800
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   801
        private long localizedWeekOfMonth(TemporalAccessor temporal) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   802
            int dow = localizedDayOfWeek(temporal);
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   803
            int dom = temporal.get(DAY_OF_MONTH);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   804
            int offset = startOfWeekOffset(dom, dow);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   805
            return computeWeek(offset, dom);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   806
        }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   807
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   808
        private long localizedWeekOfYear(TemporalAccessor temporal) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   809
            int dow = localizedDayOfWeek(temporal);
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   810
            int doy = temporal.get(DAY_OF_YEAR);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   811
            int offset = startOfWeekOffset(doy, dow);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   812
            return computeWeek(offset, doy);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   813
        }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   814
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   815
        /**
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   816
         * Returns the year of week-based-year for the temporal.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   817
         * The year can be the previous year, the current year, or the next year.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   818
         * @param temporal a date of any chronology, not null
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   819
         * @return the year of week-based-year for the date
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   820
         */
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   821
        private int localizedWeekBasedYear(TemporalAccessor temporal) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   822
            int dow = localizedDayOfWeek(temporal);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   823
            int year = temporal.get(YEAR);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   824
            int doy = temporal.get(DAY_OF_YEAR);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   825
            int offset = startOfWeekOffset(doy, dow);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   826
            int week = computeWeek(offset, doy);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   827
            if (week == 0) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   828
                // Day is in end of week of previous year; return the previous year
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   829
                return year - 1;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   830
            } else {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   831
                // If getting close to end of year, use higher precision logic
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   832
                // Check if date of year is in partial week associated with next year
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   833
                ValueRange dayRange = temporal.range(DAY_OF_YEAR);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   834
                int yearLen = (int)dayRange.getMaximum();
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   835
                int newYearWeek = computeWeek(offset, yearLen + weekDef.getMinimalDaysInFirstWeek());
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   836
                if (week >= newYearWeek) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   837
                    return year + 1;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   838
                }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   839
            }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   840
            return year;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   841
        }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   842
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   843
        /**
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   844
         * Returns the week of week-based-year for the temporal.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   845
         * The week can be part of the previous year, the current year,
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   846
         * or the next year depending on the week start and minimum number
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   847
         * of days.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   848
         * @param temporal  a date of any chronology
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   849
         * @return the week of the year
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   850
         * @see #localizedWeekBasedYear(java.time.temporal.TemporalAccessor)
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   851
         */
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   852
        private int localizedWeekOfWeekBasedYear(TemporalAccessor temporal) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   853
            int dow = localizedDayOfWeek(temporal);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   854
            int doy = temporal.get(DAY_OF_YEAR);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   855
            int offset = startOfWeekOffset(doy, dow);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   856
            int week = computeWeek(offset, doy);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   857
            if (week == 0) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   858
                // Day is in end of week of previous year
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   859
                // Recompute from the last day of the previous year
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   860
                ChronoLocalDate date = Chronology.from(temporal).date(temporal);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   861
                date = date.minus(doy, DAYS);   // Back down into previous year
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   862
                return localizedWeekOfWeekBasedYear(date);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   863
            } else if (week > 50) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   864
                // If getting close to end of year, use higher precision logic
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   865
                // Check if date of year is in partial week associated with next year
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   866
                ValueRange dayRange = temporal.range(DAY_OF_YEAR);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   867
                int yearLen = (int)dayRange.getMaximum();
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   868
                int newYearWeek = computeWeek(offset, yearLen + weekDef.getMinimalDaysInFirstWeek());
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   869
                if (week >= newYearWeek) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   870
                    // Overlaps with week of following year; reduce to week in following year
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   871
                    week = week - newYearWeek + 1;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   872
                }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   873
            }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   874
            return week;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   875
        }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   876
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   877
        /**
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   878
         * Returns an offset to align week start with a day of month or day of year.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   879
         *
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   880
         * @param day  the day; 1 through infinity
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   881
         * @param dow  the day of the week of that day; 1 through 7
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   882
         * @return  an offset in days to align a day with the start of the first 'full' week
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   883
         */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   884
        private int startOfWeekOffset(int day, int dow) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   885
            // offset of first day corresponding to the day of week in first 7 days (zero origin)
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   886
            int weekStart = Math.floorMod(day - dow, 7);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   887
            int offset = -weekStart;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   888
            if (weekStart + 1 > weekDef.getMinimalDaysInFirstWeek()) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   889
                // The previous week has the minimum days in the current month to be a 'week'
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   890
                offset = 7 - weekStart;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   891
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   892
            return offset;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   893
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   894
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   895
        /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   896
         * Returns the week number computed from the reference day and reference dayOfWeek.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   897
         *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   898
         * @param offset the offset to align a date with the start of week
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   899
         *     from {@link #startOfWeekOffset}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   900
         * @param day  the day for which to compute the week number
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   901
         * @return the week number where zero is used for a partial week and 1 for the first full week
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   902
         */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   903
        private int computeWeek(int offset, int day) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   904
            return ((7 + offset + (day - 1)) / 7);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   905
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   906
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   907
        @SuppressWarnings("unchecked")
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   908
        @Override
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   909
        public <R extends Temporal> R adjustInto(R temporal, long newValue) {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   910
            // Check the new value and get the old value of the field
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   911
            int newVal = range.checkValidIntValue(newValue, this);  // lenient check range
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   912
            int currentVal = temporal.get(this);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   913
            if (newVal == currentVal) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   914
                return temporal;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   915
            }
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   916
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   917
            if (rangeUnit == FOREVER) {     // replace year of WeekBasedYear
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   918
                // Create a new date object with the same chronology,
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   919
                // the desired year and the same week and dow.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   920
                int idow = temporal.get(weekDef.dayOfWeek);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   921
                int wowby = temporal.get(weekDef.weekOfWeekBasedYear);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   922
                return (R) ofWeekBasedYear(Chronology.from(temporal), (int)newValue, wowby, idow);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   923
            } else {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   924
                // Compute the difference and add that using the base unit of the field
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   925
                return (R) temporal.plus(newVal - currentVal, baseUnit);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   926
            }
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   927
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   928
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   929
        @Override
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   930
        public ChronoLocalDate resolve(
20793
5acd4b8d70a1 8025720: Separate temporal interface layer
rriggs
parents: 19030
diff changeset
   931
                Map<TemporalField, Long> fieldValues, TemporalAccessor partialTemporal, ResolverStyle resolverStyle) {
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   932
            final long value = fieldValues.get(this);
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   933
            final int newValue = Math.toIntExact(value);  // broad limit makes overflow checking lighter
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   934
            // first convert localized day-of-week to ISO day-of-week
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   935
            // doing this first handles case where both ISO and localized were parsed and might mismatch
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   936
            // day-of-week is always strict as two different day-of-week values makes lenient complex
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   937
            if (rangeUnit == WEEKS) {  // day-of-week
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   938
                final int checkedValue = range.checkValidIntValue(value, this);  // no leniency as too complex
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   939
                final int startDow = weekDef.getFirstDayOfWeek().getValue();
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   940
                long isoDow = Math.floorMod((startDow - 1) + (checkedValue - 1), 7) + 1;
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   941
                fieldValues.remove(this);
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   942
                fieldValues.put(DAY_OF_WEEK, isoDow);
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   943
                return null;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   944
            }
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   945
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   946
            // can only build date if ISO day-of-week is present
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   947
            if (fieldValues.containsKey(DAY_OF_WEEK) == false) {
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   948
                return null;
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   949
            }
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   950
            int isoDow = DAY_OF_WEEK.checkValidIntValue(fieldValues.get(DAY_OF_WEEK));
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   951
            int dow = localizedDayOfWeek(isoDow);
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   952
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   953
            // build date
20793
5acd4b8d70a1 8025720: Separate temporal interface layer
rriggs
parents: 19030
diff changeset
   954
            Chronology chrono = Chronology.from(partialTemporal);
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   955
            if (fieldValues.containsKey(YEAR)) {
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   956
                int year = YEAR.checkValidIntValue(fieldValues.get(YEAR));  // validate
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   957
                if (rangeUnit == MONTHS && fieldValues.containsKey(MONTH_OF_YEAR)) {  // week-of-month
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   958
                    long month = fieldValues.get(MONTH_OF_YEAR);  // not validated yet
20793
5acd4b8d70a1 8025720: Separate temporal interface layer
rriggs
parents: 19030
diff changeset
   959
                    return resolveWoM(fieldValues, chrono, year, month, newValue, dow, resolverStyle);
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   960
                }
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   961
                if (rangeUnit == YEARS) {  // week-of-year
20793
5acd4b8d70a1 8025720: Separate temporal interface layer
rriggs
parents: 19030
diff changeset
   962
                    return resolveWoY(fieldValues, chrono, year, newValue, dow, resolverStyle);
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   963
                }
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   964
            } else if ((rangeUnit == WEEK_BASED_YEARS || rangeUnit == FOREVER) &&
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   965
                    fieldValues.containsKey(weekDef.weekBasedYear) &&
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   966
                    fieldValues.containsKey(weekDef.weekOfWeekBasedYear)) { // week-of-week-based-year and year-of-week-based-year
20793
5acd4b8d70a1 8025720: Separate temporal interface layer
rriggs
parents: 19030
diff changeset
   967
                return resolveWBY(fieldValues, chrono, dow, resolverStyle);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   968
            }
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   969
            return null;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   970
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   971
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   972
        private ChronoLocalDate resolveWoM(
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   973
                Map<TemporalField, Long> fieldValues, Chronology chrono, int year, long month, long wom, int localDow, ResolverStyle resolverStyle) {
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   974
            ChronoLocalDate date;
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   975
            if (resolverStyle == ResolverStyle.LENIENT) {
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   976
                date = chrono.date(year, 1, 1).plus(Math.subtractExact(month, 1), MONTHS);
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   977
                long weeks = Math.subtractExact(wom, localizedWeekOfMonth(date));
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   978
                int days = localDow - localizedDayOfWeek(date);  // safe from overflow
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   979
                date = date.plus(Math.addExact(Math.multiplyExact(weeks, 7), days), DAYS);
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   980
            } else {
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   981
                int monthValid = MONTH_OF_YEAR.checkValidIntValue(month);  // validate
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   982
                date = chrono.date(year, monthValid, 1);
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   983
                int womInt = range.checkValidIntValue(wom, this);  // validate
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   984
                int weeks = (int) (womInt - localizedWeekOfMonth(date));  // safe from overflow
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   985
                int days = localDow - localizedDayOfWeek(date);  // safe from overflow
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   986
                date = date.plus(weeks * 7 + days, DAYS);
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   987
                if (resolverStyle == ResolverStyle.STRICT && date.getLong(MONTH_OF_YEAR) != month) {
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   988
                    throw new DateTimeException("Strict mode rejected resolved date as it is in a different month");
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   989
                }
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   990
            }
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   991
            fieldValues.remove(this);
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   992
            fieldValues.remove(YEAR);
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   993
            fieldValues.remove(MONTH_OF_YEAR);
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   994
            fieldValues.remove(DAY_OF_WEEK);
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   995
            return date;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   996
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   997
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   998
        private ChronoLocalDate resolveWoY(
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   999
                Map<TemporalField, Long> fieldValues, Chronology chrono, int year, long woy, int localDow, ResolverStyle resolverStyle) {
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  1000
            ChronoLocalDate date = chrono.date(year, 1, 1);
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  1001
            if (resolverStyle == ResolverStyle.LENIENT) {
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  1002
                long weeks = Math.subtractExact(woy, localizedWeekOfYear(date));
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  1003
                int days = localDow - localizedDayOfWeek(date);  // safe from overflow
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  1004
                date = date.plus(Math.addExact(Math.multiplyExact(weeks, 7), days), DAYS);
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  1005
            } else {
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  1006
                int womInt = range.checkValidIntValue(woy, this);  // validate
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  1007
                int weeks = (int) (womInt - localizedWeekOfYear(date));  // safe from overflow
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  1008
                int days = localDow - localizedDayOfWeek(date);  // safe from overflow
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  1009
                date = date.plus(weeks * 7 + days, DAYS);
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  1010
                if (resolverStyle == ResolverStyle.STRICT && date.getLong(YEAR) != year) {
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  1011
                    throw new DateTimeException("Strict mode rejected resolved date as it is in a different year");
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  1012
                }
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  1013
            }
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  1014
            fieldValues.remove(this);
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  1015
            fieldValues.remove(YEAR);
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  1016
            fieldValues.remove(DAY_OF_WEEK);
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  1017
            return date;
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  1018
        }
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  1019
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  1020
        private ChronoLocalDate resolveWBY(
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  1021
                Map<TemporalField, Long> fieldValues, Chronology chrono, int localDow, ResolverStyle resolverStyle) {
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  1022
            int yowby = weekDef.weekBasedYear.range().checkValidIntValue(
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  1023
                    fieldValues.get(weekDef.weekBasedYear), weekDef.weekBasedYear);
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  1024
            ChronoLocalDate date;
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  1025
            if (resolverStyle == ResolverStyle.LENIENT) {
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  1026
                date = ofWeekBasedYear(chrono, yowby, 1, localDow);
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  1027
                long wowby = fieldValues.get(weekDef.weekOfWeekBasedYear);
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  1028
                long weeks = Math.subtractExact(wowby, 1);
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  1029
                date = date.plus(weeks, WEEKS);
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  1030
            } else {
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  1031
                int wowby = weekDef.weekOfWeekBasedYear.range().checkValidIntValue(
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  1032
                        fieldValues.get(weekDef.weekOfWeekBasedYear), weekDef.weekOfWeekBasedYear);  // validate
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  1033
                date = ofWeekBasedYear(chrono, yowby, wowby, localDow);
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  1034
                if (resolverStyle == ResolverStyle.STRICT && localizedWeekBasedYear(date) != yowby) {
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  1035
                    throw new DateTimeException("Strict mode rejected resolved date as it is in a different week-based-year");
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  1036
                }
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  1037
            }
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  1038
            fieldValues.remove(this);
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  1039
            fieldValues.remove(weekDef.weekBasedYear);
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  1040
            fieldValues.remove(weekDef.weekOfWeekBasedYear);
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  1041
            fieldValues.remove(DAY_OF_WEEK);
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  1042
            return date;
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  1043
        }
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  1044
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  1045
        //-----------------------------------------------------------------------
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1046
        @Override
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1047
        public String getDisplayName(Locale locale) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1048
            Objects.requireNonNull(locale, "locale");
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1049
            if (rangeUnit == YEARS) {  // only have values for week-of-year
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1050
                LocaleResources lr = LocaleProviderAdapter.getResourceBundleBased()
48251
57148c79bd75 8176841: Additional Unicode Language-Tag Extensions
naoto
parents: 47478
diff changeset
  1051
                        .getLocaleResources(
57148c79bd75 8176841: Additional Unicode Language-Tag Extensions
naoto
parents: 47478
diff changeset
  1052
                            CalendarDataUtility.findRegionOverride(locale));
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1053
                ResourceBundle rb = lr.getJavaTimeFormatData();
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  1054
                return rb.containsKey("field.week") ? rb.getString("field.week") : name;
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1055
            }
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  1056
            return name;
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1057
        }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1058
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1059
        @Override
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1060
        public TemporalUnit getBaseUnit() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1061
            return baseUnit;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1062
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1063
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1064
        @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1065
        public TemporalUnit getRangeUnit() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1066
            return rangeUnit;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1067
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1068
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1069
        @Override
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1070
        public boolean isDateBased() {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1071
            return true;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1072
        }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1073
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1074
        @Override
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  1075
        public boolean isTimeBased() {
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  1076
            return false;
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  1077
        }
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  1078
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  1079
        @Override
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1080
        public ValueRange range() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1081
            return range;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1082
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1083
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1084
        //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1085
        @Override
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1086
        public boolean isSupportedBy(TemporalAccessor temporal) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1087
            if (temporal.isSupported(DAY_OF_WEEK)) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1088
                if (rangeUnit == WEEKS) {  // day-of-week
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1089
                    return true;
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1090
                } else if (rangeUnit == MONTHS) {  // week-of-month
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1091
                    return temporal.isSupported(DAY_OF_MONTH);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1092
                } else if (rangeUnit == YEARS) {  // week-of-year
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1093
                    return temporal.isSupported(DAY_OF_YEAR);
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1094
                } else if (rangeUnit == WEEK_BASED_YEARS) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1095
                    return temporal.isSupported(DAY_OF_YEAR);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1096
                } else if (rangeUnit == FOREVER) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1097
                    return temporal.isSupported(YEAR);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1098
                }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1099
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1100
            return false;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1101
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1102
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1103
        @Override
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1104
        public ValueRange rangeRefinedBy(TemporalAccessor temporal) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1105
            if (rangeUnit == ChronoUnit.WEEKS) {  // day-of-week
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1106
                return range;
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1107
            } else if (rangeUnit == MONTHS) {  // week-of-month
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1108
                return rangeByWeek(temporal, DAY_OF_MONTH);
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1109
            } else if (rangeUnit == YEARS) {  // week-of-year
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1110
                return rangeByWeek(temporal, DAY_OF_YEAR);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1111
            } else if (rangeUnit == WEEK_BASED_YEARS) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1112
                return rangeWeekOfWeekBasedYear(temporal);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1113
            } else if (rangeUnit == FOREVER) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1114
                return YEAR.range();
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1115
            } else {
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1116
                throw new IllegalStateException("unreachable, rangeUnit: " + rangeUnit + ", this: " + this);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1117
            }
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1118
        }
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1119
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1120
        /**
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1121
         * Map the field range to a week range
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1122
         * @param temporal the temporal
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1123
         * @param field the field to get the range of
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1124
         * @return the ValueRange with the range adjusted to weeks.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1125
         */
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1126
        private ValueRange rangeByWeek(TemporalAccessor temporal, TemporalField field) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1127
            int dow = localizedDayOfWeek(temporal);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1128
            int offset = startOfWeekOffset(temporal.get(field), dow);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1129
            ValueRange fieldRange = temporal.range(field);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1130
            return ValueRange.of(computeWeek(offset, (int) fieldRange.getMinimum()),
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1131
                    computeWeek(offset, (int) fieldRange.getMaximum()));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1132
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1133
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1134
        /**
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1135
         * Map the field range to a week range of a week year.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1136
         * @param temporal  the temporal
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1137
         * @return the ValueRange with the range adjusted to weeks.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1138
         */
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1139
        private ValueRange rangeWeekOfWeekBasedYear(TemporalAccessor temporal) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1140
            if (!temporal.isSupported(DAY_OF_YEAR)) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1141
                return WEEK_OF_YEAR_RANGE;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1142
            }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1143
            int dow = localizedDayOfWeek(temporal);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1144
            int doy = temporal.get(DAY_OF_YEAR);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1145
            int offset = startOfWeekOffset(doy, dow);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1146
            int week = computeWeek(offset, doy);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1147
            if (week == 0) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1148
                // Day is in end of week of previous year
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1149
                // Recompute from the last day of the previous year
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1150
                ChronoLocalDate date = Chronology.from(temporal).date(temporal);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1151
                date = date.minus(doy + 7, DAYS);   // Back down into previous year
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1152
                return rangeWeekOfWeekBasedYear(date);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1153
            }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1154
            // Check if day of year is in partial week associated with next year
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1155
            ValueRange dayRange = temporal.range(DAY_OF_YEAR);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1156
            int yearLen = (int)dayRange.getMaximum();
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1157
            int newYearWeek = computeWeek(offset, yearLen + weekDef.getMinimalDaysInFirstWeek());
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1158
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1159
            if (week >= newYearWeek) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1160
                // Overlaps with weeks of following year; recompute from a week in following year
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1161
                ChronoLocalDate date = Chronology.from(temporal).date(temporal);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1162
                date = date.plus(yearLen - doy + 1 + 7, ChronoUnit.DAYS);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1163
                return rangeWeekOfWeekBasedYear(date);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1164
            }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1165
            return ValueRange.of(1, newYearWeek-1);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1166
        }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1167
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1168
        //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1169
        @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1170
        public String toString() {
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  1171
            return name + "[" + weekDef.toString() + "]";
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1172
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1173
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1174
}