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