jdk/src/share/classes/java/time/format/DateTimeTextProvider.java
author scolebourne
Thu, 06 Mar 2014 16:51:30 +0000
changeset 24256 da9a41004459
parent 17474 8c100beabcc0
permissions -rw-r--r--
8034906: Fix typos, errors and Javadoc differences in java.time Reviewed-by: psandoz
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     1
/*
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     2
 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
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.format;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    63
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    64
import static java.time.temporal.ChronoField.AMPM_OF_DAY;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    65
import static java.time.temporal.ChronoField.DAY_OF_WEEK;
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    66
import static java.time.temporal.ChronoField.ERA;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    67
import static java.time.temporal.ChronoField.MONTH_OF_YEAR;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    68
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    69
import java.time.chrono.Chronology;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    70
import java.time.chrono.IsoChronology;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    71
import java.time.chrono.JapaneseChronology;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    72
import java.time.temporal.ChronoField;
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
    73
import java.time.temporal.IsoFields;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    74
import java.time.temporal.TemporalField;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    75
import java.util.AbstractMap.SimpleImmutableEntry;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    76
import java.util.ArrayList;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    77
import java.util.Calendar;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    78
import java.util.Collections;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    79
import java.util.Comparator;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    80
import java.util.HashMap;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    81
import java.util.Iterator;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    82
import java.util.List;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    83
import java.util.Locale;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    84
import java.util.Map;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    85
import java.util.Map.Entry;
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
    86
import java.util.ResourceBundle;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    87
import java.util.concurrent.ConcurrentHashMap;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    88
import java.util.concurrent.ConcurrentMap;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    89
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
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
 * A provider to obtain the textual form of a date-time field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    96
 *
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
    97
 * @implSpec
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    98
 * Implementations must be thread-safe.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    99
 * Implementations should cache the textual information.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   100
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   101
 * @since 1.8
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   102
 */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   103
class DateTimeTextProvider {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   104
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   105
    /** Cache. */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   106
    private static final ConcurrentMap<Entry<TemporalField, Locale>, Object> CACHE = new ConcurrentHashMap<>(16, 0.75f, 2);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   107
    /** Comparator. */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   108
    private static final Comparator<Entry<String, Long>> COMPARATOR = new Comparator<Entry<String, Long>>() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   109
        @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   110
        public int compare(Entry<String, Long> obj1, Entry<String, Long> obj2) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   111
            return obj2.getKey().length() - obj1.getKey().length();  // longest to shortest
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   112
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   113
    };
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   114
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   115
    DateTimeTextProvider() {}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   116
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   117
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   118
     * Gets the provider of text.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   119
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   120
     * @return the provider, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   121
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   122
    static DateTimeTextProvider getInstance() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   123
        return new DateTimeTextProvider();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   124
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   125
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   126
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   127
     * Gets the text for the specified field, locale and style
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   128
     * for the purpose of formatting.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   129
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   130
     * The text associated with the value is returned.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   131
     * The null return value should be used if there is no applicable text, or
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   132
     * if the text would be a numeric representation of the value.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   133
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   134
     * @param field  the field to get text for, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   135
     * @param value  the field value to get text for, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   136
     * @param style  the style to get text for, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   137
     * @param locale  the locale to get text for, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   138
     * @return the text for the field value, null if no text found
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   139
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   140
    public String getText(TemporalField field, long value, TextStyle style, Locale locale) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   141
        Object store = findStore(field, locale);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   142
        if (store instanceof LocaleStore) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   143
            return ((LocaleStore) store).getText(value, style);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   144
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   145
        return null;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   146
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   147
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   148
    /**
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   149
     * Gets the text for the specified chrono, field, locale and style
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   150
     * for the purpose of formatting.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   151
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   152
     * The text associated with the value is returned.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   153
     * The null return value should be used if there is no applicable text, or
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   154
     * if the text would be a numeric representation of the value.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   155
     *
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   156
     * @param chrono  the Chronology to get text for, not null
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   157
     * @param field  the field to get text for, not null
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   158
     * @param value  the field value to get text for, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   159
     * @param style  the style to get text for, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   160
     * @param locale  the locale to get text for, not null
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   161
     * @return the text for the field value, null if no text found
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   162
     */
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   163
    public String getText(Chronology chrono, TemporalField field, long value,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   164
                                    TextStyle style, Locale locale) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   165
        if (chrono == IsoChronology.INSTANCE
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   166
                || !(field instanceof ChronoField)) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   167
            return getText(field, value, style, locale);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   168
        }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   169
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   170
        int fieldIndex;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   171
        int fieldValue;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   172
        if (field == ERA) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   173
            fieldIndex = Calendar.ERA;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   174
            if (chrono == JapaneseChronology.INSTANCE) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   175
                if (value == -999) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   176
                    fieldValue = 0;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   177
                } else {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   178
                    fieldValue = (int) value + 2;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   179
                }
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   180
            } else {
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   181
                fieldValue = (int) value;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   182
            }
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   183
        } else if (field == MONTH_OF_YEAR) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   184
            fieldIndex = Calendar.MONTH;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   185
            fieldValue = (int) value - 1;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   186
        } else if (field == DAY_OF_WEEK) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   187
            fieldIndex = Calendar.DAY_OF_WEEK;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   188
            fieldValue = (int) value + 1;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   189
            if (fieldValue > 7) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   190
                fieldValue = Calendar.SUNDAY;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   191
            }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   192
        } else if (field == AMPM_OF_DAY) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   193
            fieldIndex = Calendar.AM_PM;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   194
            fieldValue = (int) value;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   195
        } else {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   196
            return null;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   197
        }
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   198
        return CalendarDataUtility.retrieveJavaTimeFieldValueName(
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   199
                chrono.getCalendarType(), fieldIndex, fieldValue, style.toCalendarStyle(), locale);
15289
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
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   203
     * Gets an iterator of text to field for the specified field, locale and style
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   204
     * for the purpose of parsing.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   205
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   206
     * The iterator must be returned in order from the longest text to the shortest.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   207
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   208
     * The null return value should be used if there is no applicable parsable text, or
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   209
     * if the text would be a numeric representation of the value.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   210
     * Text can only be parsed if all the values for that field-style-locale combination are unique.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   211
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   212
     * @param field  the field to get text for, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   213
     * @param style  the style to get text for, null for all parsable text
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   214
     * @param locale  the locale to get text for, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   215
     * @return the iterator of text to field pairs, in order from longest text to shortest text,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   216
     *  null if the field or style is not parsable
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   217
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   218
    public Iterator<Entry<String, Long>> getTextIterator(TemporalField field, TextStyle style, Locale locale) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   219
        Object store = findStore(field, locale);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   220
        if (store instanceof LocaleStore) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   221
            return ((LocaleStore) store).getTextIterator(style);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   222
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   223
        return null;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   224
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   225
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   226
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   227
     * Gets an iterator of text to field for the specified chrono, field, locale and style
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   228
     * for the purpose of parsing.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   229
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   230
     * The iterator must be returned in order from the longest text to the shortest.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   231
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   232
     * The null return value should be used if there is no applicable parsable text, or
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   233
     * if the text would be a numeric representation of the value.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   234
     * Text can only be parsed if all the values for that field-style-locale combination are unique.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   235
     *
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   236
     * @param chrono  the Chronology to get text for, not null
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   237
     * @param field  the field to get text for, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   238
     * @param style  the style to get text for, null for all parsable text
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   239
     * @param locale  the locale to get text for, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   240
     * @return the iterator of text to field pairs, in order from longest text to shortest text,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   241
     *  null if the field or style is not parsable
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   242
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   243
    public Iterator<Entry<String, Long>> getTextIterator(Chronology chrono, TemporalField field,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   244
                                                         TextStyle style, Locale locale) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   245
        if (chrono == IsoChronology.INSTANCE
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   246
                || !(field instanceof ChronoField)) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   247
            return getTextIterator(field, style, locale);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   248
        }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   249
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   250
        int fieldIndex;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   251
        switch ((ChronoField)field) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   252
        case ERA:
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   253
            fieldIndex = Calendar.ERA;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   254
            break;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   255
        case MONTH_OF_YEAR:
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   256
            fieldIndex = Calendar.MONTH;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   257
            break;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   258
        case DAY_OF_WEEK:
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   259
            fieldIndex = Calendar.DAY_OF_WEEK;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   260
            break;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   261
        case AMPM_OF_DAY:
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   262
            fieldIndex = Calendar.AM_PM;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   263
            break;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   264
        default:
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   265
            return null;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   266
        }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   267
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   268
        int calendarStyle = (style == null) ? Calendar.ALL_STYLES : style.toCalendarStyle();
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   269
        Map<String, Integer> map = CalendarDataUtility.retrieveJavaTimeFieldValueNames(
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   270
                chrono.getCalendarType(), fieldIndex, calendarStyle, locale);
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   271
        if (map == null) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   272
            return null;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   273
        }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   274
        List<Entry<String, Long>> list = new ArrayList<>(map.size());
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   275
        switch (fieldIndex) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   276
        case Calendar.ERA:
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   277
            for (Map.Entry<String, Integer> entry : map.entrySet()) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   278
                int era = entry.getValue();
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   279
                if (chrono == JapaneseChronology.INSTANCE) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   280
                    if (era == 0) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   281
                        era = -999;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   282
                    } else {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   283
                        era -= 2;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   284
                    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   285
                }
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   286
                list.add(createEntry(entry.getKey(), (long)era));
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   287
            }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   288
            break;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   289
        case Calendar.MONTH:
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   290
            for (Map.Entry<String, Integer> entry : map.entrySet()) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   291
                list.add(createEntry(entry.getKey(), (long)(entry.getValue() + 1)));
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   292
            }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   293
            break;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   294
        case Calendar.DAY_OF_WEEK:
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   295
            for (Map.Entry<String, Integer> entry : map.entrySet()) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   296
                list.add(createEntry(entry.getKey(), (long)toWeekDay(entry.getValue())));
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   297
            }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   298
            break;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   299
        default:
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   300
            for (Map.Entry<String, Integer> entry : map.entrySet()) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   301
                list.add(createEntry(entry.getKey(), (long)entry.getValue()));
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   302
            }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   303
            break;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   304
        }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   305
        return list.iterator();
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   306
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   307
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   308
    private Object findStore(TemporalField field, Locale locale) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   309
        Entry<TemporalField, Locale> key = createEntry(field, locale);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   310
        Object store = CACHE.get(key);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   311
        if (store == null) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   312
            store = createStore(field, locale);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   313
            CACHE.putIfAbsent(key, store);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   314
            store = CACHE.get(key);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   315
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   316
        return store;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   317
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   318
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   319
    private static int toWeekDay(int calWeekDay) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   320
        if (calWeekDay == Calendar.SUNDAY) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   321
            return 7;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   322
        } else {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   323
            return calWeekDay - 1;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   324
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   325
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   326
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   327
    private Object createStore(TemporalField field, Locale locale) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   328
        Map<TextStyle, Map<Long, String>> styleMap = new HashMap<>();
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   329
        if (field == ERA) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   330
            for (TextStyle textStyle : TextStyle.values()) {
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   331
                if (textStyle.isStandalone()) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   332
                    // Stand-alone isn't applicable to era names.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   333
                    continue;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   334
                }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   335
                Map<String, Integer> displayNames = CalendarDataUtility.retrieveJavaTimeFieldValueNames(
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   336
                        "gregory", Calendar.ERA, textStyle.toCalendarStyle(), locale);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   337
                if (displayNames != null) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   338
                    Map<Long, String> map = new HashMap<>();
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   339
                    for (Entry<String, Integer> entry : displayNames.entrySet()) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   340
                        map.put((long) entry.getValue(), entry.getKey());
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   341
                    }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   342
                    if (!map.isEmpty()) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   343
                        styleMap.put(textStyle, map);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   344
                    }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   345
                }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   346
            }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   347
            return new LocaleStore(styleMap);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   348
        }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   349
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   350
        if (field == MONTH_OF_YEAR) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   351
            for (TextStyle textStyle : TextStyle.values()) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   352
                Map<String, Integer> displayNames = CalendarDataUtility.retrieveJavaTimeFieldValueNames(
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   353
                        "gregory", Calendar.MONTH, textStyle.toCalendarStyle(), locale);
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   354
                Map<Long, String> map = new HashMap<>();
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   355
                if (displayNames != null) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   356
                    for (Entry<String, Integer> entry : displayNames.entrySet()) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   357
                        map.put((long) (entry.getValue() + 1), entry.getKey());
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   358
                    }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   359
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   360
                } else {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   361
                    // Narrow names may have duplicated names, such as "J" for January, Jun, July.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   362
                    // Get names one by one in that case.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   363
                    for (int month = Calendar.JANUARY; month <= Calendar.DECEMBER; month++) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   364
                        String name;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   365
                        name = CalendarDataUtility.retrieveJavaTimeFieldValueName(
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   366
                                "gregory", Calendar.MONTH, month, textStyle.toCalendarStyle(), locale);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   367
                        if (name == null) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   368
                            break;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   369
                        }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   370
                        map.put((long) (month + 1), name);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   371
                    }
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   372
                }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   373
                if (!map.isEmpty()) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   374
                    styleMap.put(textStyle, map);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   375
                }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   376
            }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   377
            return new LocaleStore(styleMap);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   378
        }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   379
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   380
        if (field == DAY_OF_WEEK) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   381
            for (TextStyle textStyle : TextStyle.values()) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   382
                Map<String, Integer> displayNames = CalendarDataUtility.retrieveJavaTimeFieldValueNames(
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   383
                        "gregory", Calendar.DAY_OF_WEEK, textStyle.toCalendarStyle(), locale);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   384
                Map<Long, String> map = new HashMap<>();
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   385
                if (displayNames != null) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   386
                    for (Entry<String, Integer> entry : displayNames.entrySet()) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   387
                        map.put((long)toWeekDay(entry.getValue()), entry.getKey());
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   388
                    }
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   389
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   390
                } else {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   391
                    // Narrow names may have duplicated names, such as "S" for Sunday and Saturday.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   392
                    // Get names one by one in that case.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   393
                    for (int wday = Calendar.SUNDAY; wday <= Calendar.SATURDAY; wday++) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   394
                        String name;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   395
                        name = CalendarDataUtility.retrieveJavaTimeFieldValueName(
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   396
                            "gregory", Calendar.DAY_OF_WEEK, wday, textStyle.toCalendarStyle(), locale);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   397
                        if (name == null) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   398
                            break;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   399
                        }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   400
                        map.put((long)toWeekDay(wday), name);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   401
                    }
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   402
                }
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   403
                if (!map.isEmpty()) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   404
                    styleMap.put(textStyle, map);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   405
                }
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   406
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   407
            return new LocaleStore(styleMap);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   408
        }
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   409
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   410
        if (field == AMPM_OF_DAY) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   411
            for (TextStyle textStyle : TextStyle.values()) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   412
                if (textStyle.isStandalone()) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   413
                    // Stand-alone isn't applicable to AM/PM.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   414
                    continue;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   415
                }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   416
                Map<String, Integer> displayNames = CalendarDataUtility.retrieveJavaTimeFieldValueNames(
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   417
                        "gregory", Calendar.AM_PM, textStyle.toCalendarStyle(), locale);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   418
                if (displayNames != null) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   419
                    Map<Long, String> map = new HashMap<>();
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   420
                    for (Entry<String, Integer> entry : displayNames.entrySet()) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   421
                        map.put((long) entry.getValue(), entry.getKey());
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   422
                    }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   423
                    if (!map.isEmpty()) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   424
                        styleMap.put(textStyle, map);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   425
                    }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   426
                }
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   427
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   428
            return new LocaleStore(styleMap);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   429
        }
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   430
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   431
        if (field == IsoFields.QUARTER_OF_YEAR) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   432
            // The order of keys must correspond to the TextStyle.values() order.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   433
            final String[] keys = {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   434
                "QuarterNames",
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   435
                "standalone.QuarterNames",
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   436
                "QuarterAbbreviations",
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   437
                "standalone.QuarterAbbreviations",
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   438
                "QuarterNarrows",
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   439
                "standalone.QuarterNarrows",
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   440
            };
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   441
            for (int i = 0; i < keys.length; i++) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   442
                String[] names = getLocalizedResource(keys[i], locale);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   443
                if (names != null) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   444
                    Map<Long, String> map = new HashMap<>();
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   445
                    for (int q = 0; q < names.length; q++) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   446
                        map.put((long) (q + 1), names[q]);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   447
                    }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   448
                    styleMap.put(TextStyle.values()[i], map);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   449
                }
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   450
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   451
            return new LocaleStore(styleMap);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   452
        }
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   453
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   454
        return "";  // null marker for map
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   455
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   456
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   457
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   458
     * Helper method to create an immutable entry.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   459
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   460
     * @param text  the text, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   461
     * @param field  the field, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   462
     * @return the entry, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   463
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   464
    private static <A, B> Entry<A, B> createEntry(A text, B field) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   465
        return new SimpleImmutableEntry<>(text, field);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   466
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   467
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   468
    /**
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   469
     * Returns the localized resource of the given key and locale, or null
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   470
     * if no localized resource is available.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   471
     *
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   472
     * @param key  the key of the localized resource, not null
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   473
     * @param locale  the locale, not null
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   474
     * @return the localized resource, or null if not available
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   475
     * @throws NullPointerException if key or locale is null
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   476
     */
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   477
    @SuppressWarnings("unchecked")
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   478
    static <T> T getLocalizedResource(String key, Locale locale) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   479
        LocaleResources lr = LocaleProviderAdapter.getResourceBundleBased()
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   480
                                    .getLocaleResources(locale);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   481
        ResourceBundle rb = lr.getJavaTimeFormatData();
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   482
        return rb.containsKey(key) ? (T) rb.getObject(key) : null;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   483
    }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   484
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   485
    /**
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   486
     * Stores the text for a single locale.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   487
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   488
     * Some fields have a textual representation, such as day-of-week or month-of-year.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   489
     * These textual representations can be captured in this class for printing
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   490
     * and parsing.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   491
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   492
     * This class is immutable and thread-safe.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   493
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   494
    static final class LocaleStore {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   495
        /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   496
         * Map of value to text.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   497
         */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   498
        private final Map<TextStyle, Map<Long, String>> valueTextMap;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   499
        /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   500
         * Parsable data.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   501
         */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   502
        private final Map<TextStyle, List<Entry<String, Long>>> parsable;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   503
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   504
        /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   505
         * Constructor.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   506
         *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   507
         * @param valueTextMap  the map of values to text to store, assigned and not altered, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   508
         */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   509
        LocaleStore(Map<TextStyle, Map<Long, String>> valueTextMap) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   510
            this.valueTextMap = valueTextMap;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   511
            Map<TextStyle, List<Entry<String, Long>>> map = new HashMap<>();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   512
            List<Entry<String, Long>> allList = new ArrayList<>();
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   513
            for (Map.Entry<TextStyle, Map<Long, String>> vtmEntry : valueTextMap.entrySet()) {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   514
                Map<String, Entry<String, Long>> reverse = new HashMap<>();
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   515
                for (Map.Entry<Long, String> entry : vtmEntry.getValue().entrySet()) {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   516
                    if (reverse.put(entry.getValue(), createEntry(entry.getValue(), entry.getKey())) != null) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   517
                        // TODO: BUG: this has no effect
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   518
                        continue;  // not parsable, try next style
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   519
                    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   520
                }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   521
                List<Entry<String, Long>> list = new ArrayList<>(reverse.values());
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   522
                Collections.sort(list, COMPARATOR);
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   523
                map.put(vtmEntry.getKey(), list);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   524
                allList.addAll(list);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   525
                map.put(null, allList);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   526
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   527
            Collections.sort(allList, COMPARATOR);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   528
            this.parsable = map;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   529
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   530
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   531
        /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   532
         * Gets the text for the specified field value, locale and style
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   533
         * for the purpose of printing.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   534
         *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   535
         * @param value  the value to get text for, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   536
         * @param style  the style to get text for, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   537
         * @return the text for the field value, null if no text found
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   538
         */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   539
        String getText(long value, TextStyle style) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   540
            Map<Long, String> map = valueTextMap.get(style);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   541
            return map != null ? map.get(value) : null;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   542
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   543
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   544
        /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   545
         * Gets an iterator of text to field for the specified style for the purpose of parsing.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   546
         * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   547
         * The iterator must be returned in order from the longest text to the shortest.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   548
         *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   549
         * @param style  the style to get text for, null for all parsable text
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   550
         * @return the iterator of text to field pairs, in order from longest text to shortest text,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   551
         *  null if the style is not parsable
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   552
         */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   553
        Iterator<Entry<String, Long>> getTextIterator(TextStyle style) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   554
            List<Entry<String, Long>> list = parsable.get(style);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   555
            return list != null ? list.iterator() : null;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   556
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   557
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   558
}