jdk/src/share/classes/java/time/format/DateTimeTextProvider.java
author sherman
Tue, 12 Feb 2013 09:25:43 -0800
changeset 15658 55b829ca2334
parent 15289 3ac550392e43
child 16852 60207b2b4b42
permissions -rw-r--r--
8007392: JSR 310: DateTime API Updates 8007520: Update date/time classes in j.util and j.sql packages 8007572: Replace existing jdk timezone data at <java.home>/lib/zi with JSR310's tzdb Summary: Integration of JSR310 Date/Time API for M7 Reviewed-by: darcy, alanb, naoto Contributed-by: scolebourne@joda.org, roger.riggs@oracle.com, masayoshi.okutsu@oracle.com, patrick.zhang@oracle.com
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;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    73
import java.time.temporal.TemporalField;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    74
import java.util.AbstractMap.SimpleImmutableEntry;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    75
import java.util.ArrayList;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    76
import java.util.Calendar;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    77
import java.util.Collections;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    78
import java.util.Comparator;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    79
import java.util.HashMap;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    80
import java.util.Iterator;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    81
import java.util.List;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    82
import java.util.Locale;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    83
import java.util.Map;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    84
import java.util.Map.Entry;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    85
import java.util.concurrent.ConcurrentHashMap;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    86
import java.util.concurrent.ConcurrentMap;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    87
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    88
import sun.util.locale.provider.CalendarDataUtility;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    89
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    90
/**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    91
 * A provider to obtain the textual form of a date-time field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    92
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    93
 * <h3>Specification for implementors</h3>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    94
 * Implementations must be thread-safe.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    95
 * Implementations should cache the textual information.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    96
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    97
 * @since 1.8
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    98
 */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    99
class DateTimeTextProvider {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   100
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   101
    /** Cache. */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   102
    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
   103
    /** Comparator. */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   104
    private static final Comparator<Entry<String, Long>> COMPARATOR = new Comparator<Entry<String, Long>>() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   105
        @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   106
        public int compare(Entry<String, Long> obj1, Entry<String, Long> obj2) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   107
            return obj2.getKey().length() - obj1.getKey().length();  // longest to shortest
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   108
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   109
    };
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   110
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   111
    DateTimeTextProvider() {}
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
     * Gets the provider of text.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   115
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   116
     * @return the provider, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   117
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   118
    static DateTimeTextProvider getInstance() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   119
        return new DateTimeTextProvider();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   120
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   121
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   122
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   123
     * Gets the text for the specified field, locale and style
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   124
     * for the purpose of formatting.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   125
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   126
     * The text associated with the value is returned.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   127
     * 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
   128
     * if the text would be a numeric representation of the value.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   129
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   130
     * @param field  the field to get text for, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   131
     * @param value  the field value to get text for, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   132
     * @param style  the style to get text for, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   133
     * @param locale  the locale to get text for, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   134
     * @return the text for the field value, null if no text found
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   135
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   136
    public String getText(TemporalField field, long value, TextStyle style, Locale locale) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   137
        Object store = findStore(field, locale);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   138
        if (store instanceof LocaleStore) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   139
            return ((LocaleStore) store).getText(value, style);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   140
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   141
        return null;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   142
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   143
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   144
    private static int toStyle(TextStyle style) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   145
        if (style == TextStyle.FULL) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   146
            return Calendar.LONG_FORMAT;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   147
        } else if (style == TextStyle.SHORT) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   148
            return Calendar.SHORT_FORMAT;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   149
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   150
        return Calendar.NARROW_STANDALONE;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   151
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   152
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   153
    /**
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   154
     * Gets the text for the specified chrono, field, locale and style
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   155
     * for the purpose of formatting.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   156
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   157
     * The text associated with the value is returned.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   158
     * 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
   159
     * if the text would be a numeric representation of the value.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   160
     *
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   161
     * @param chrono the Chronology to get text for, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   162
     * @param field  the field to get text for, not null
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   163
     * @param value  the field value to get text for, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   164
     * @param style  the style to get text for, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   165
     * @param locale  the locale to get text for, not null
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   166
     * @return the text for the field value, null if no text found
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   167
     */
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   168
    public String getText(Chronology chrono, TemporalField field, long value,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   169
                                    TextStyle style, Locale locale) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   170
        if (chrono == IsoChronology.INSTANCE
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   171
                || !(field instanceof ChronoField)) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   172
            return getText(field, value, style, locale);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   173
        }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   174
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   175
        int fieldIndex;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   176
        int fieldValue;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   177
        if (field == ERA) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   178
            fieldIndex = Calendar.ERA;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   179
            if (chrono == JapaneseChronology.INSTANCE) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   180
                if (value == -999) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   181
                    fieldValue = 0;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   182
                } else {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   183
                    fieldValue = (int) value + 2;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   184
                }
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   185
            } else {
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   186
                fieldValue = (int) value;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   187
            }
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   188
        } else if (field == MONTH_OF_YEAR) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   189
            fieldIndex = Calendar.MONTH;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   190
            fieldValue = (int) value - 1;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   191
        } else if (field == DAY_OF_WEEK) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   192
            fieldIndex = Calendar.DAY_OF_WEEK;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   193
            fieldValue = (int) value + 1;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   194
            if (fieldValue > 7) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   195
                fieldValue = Calendar.SUNDAY;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   196
            }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   197
        } else if (field == AMPM_OF_DAY) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   198
            fieldIndex = Calendar.AM_PM;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   199
            fieldValue = (int) value;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   200
        } else {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   201
            return null;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   202
        }
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   203
        return CalendarDataUtility.retrieveCldrFieldValueName(
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   204
                chrono.getCalendarType(), fieldIndex, fieldValue, toStyle(style), locale);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   205
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   206
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   207
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   208
     * 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
   209
     * for the purpose of parsing.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   210
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   211
     * 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
   212
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   213
     * 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
   214
     * if the text would be a numeric representation of the value.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   215
     * 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
   216
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   217
     * @param field  the field to get text for, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   218
     * @param style  the style to get text for, null for all parsable text
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   219
     * @param locale  the locale to get text for, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   220
     * @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
   221
     *  null if the field or style is not parsable
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   222
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   223
    public Iterator<Entry<String, Long>> getTextIterator(TemporalField field, TextStyle style, Locale locale) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   224
        Object store = findStore(field, locale);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   225
        if (store instanceof LocaleStore) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   226
            return ((LocaleStore) store).getTextIterator(style);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   227
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   228
        return null;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   229
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   230
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   231
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   232
     * 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
   233
     * for the purpose of parsing.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   234
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   235
     * 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
   236
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   237
     * 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
   238
     * if the text would be a numeric representation of the value.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   239
     * 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
   240
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   241
     * @param chrono the Chronology to get text for, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   242
     * @param field  the field to get text for, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   243
     * @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
   244
     * @param locale  the locale to get text for, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   245
     * @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
   246
     *  null if the field or style is not parsable
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   247
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   248
    public Iterator<Entry<String, Long>> getTextIterator(Chronology chrono, TemporalField field,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   249
                                                         TextStyle style, Locale locale) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   250
        if (chrono == IsoChronology.INSTANCE
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   251
                || !(field instanceof ChronoField)) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   252
            return getTextIterator(field, style, locale);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   253
        }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   254
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   255
        int fieldIndex;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   256
        switch ((ChronoField)field) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   257
        case ERA:
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   258
            fieldIndex = Calendar.ERA;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   259
            break;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   260
        case MONTH_OF_YEAR:
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   261
            fieldIndex = Calendar.MONTH;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   262
            break;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   263
        case DAY_OF_WEEK:
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   264
            fieldIndex = Calendar.DAY_OF_WEEK;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   265
            break;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   266
        case AMPM_OF_DAY:
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   267
            fieldIndex = Calendar.AM_PM;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   268
            break;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   269
        default:
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   270
            return null;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   271
        }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   272
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   273
        Map<String, Integer> map = CalendarDataUtility.retrieveCldrFieldValueNames(
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   274
                chrono.getCalendarType(), fieldIndex, toStyle(style), locale);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   275
        if (map == null) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   276
            return null;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   277
        }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   278
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   279
        List<Entry<String, Long>> list = new ArrayList<>(map.size());
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   280
        switch (fieldIndex) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   281
        case Calendar.ERA:
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   282
            for (String key : map.keySet()) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   283
                int era = map.get(key);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   284
                if (chrono == JapaneseChronology.INSTANCE) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   285
                    if (era == 0) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   286
                        era = -999;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   287
                    } else {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   288
                        era -= 2;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   289
                    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   290
                }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   291
                list.add(createEntry(key, (long) era));
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.MONTH:
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   295
            for (String key : map.keySet()) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   296
                list.add(createEntry(key, (long)(map.get(key) + 1)));
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
        case Calendar.DAY_OF_WEEK:
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   300
            for (String key : map.keySet()) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   301
                list.add(createEntry(key, (long)toWeekDay(map.get(key))));
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
        default:
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   305
            for (String key : map.keySet()) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   306
                list.add(createEntry(key, (long)map.get(key)));
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   307
            }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   308
            break;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   309
        }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   310
        return list.iterator();
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   311
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   312
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   313
    private Object findStore(TemporalField field, Locale locale) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   314
        Entry<TemporalField, Locale> key = createEntry(field, locale);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   315
        Object store = CACHE.get(key);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   316
        if (store == null) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   317
            store = createStore(field, locale);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   318
            CACHE.putIfAbsent(key, store);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   319
            store = CACHE.get(key);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   320
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   321
        return store;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   322
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   323
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   324
    private static int toWeekDay(int calWeekDay) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   325
        if (calWeekDay == Calendar.SUNDAY) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   326
            return 7;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   327
        } else {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   328
            return calWeekDay - 1;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   329
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   330
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   331
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   332
    private Object createStore(TemporalField field, Locale locale) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   333
        Map<TextStyle, Map<Long, String>> styleMap = new HashMap<>();
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   334
        if (field == ERA) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   335
            for (TextStyle textStyle : TextStyle.values()) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   336
                Map<Long, String> map = new HashMap<>();
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   337
                for (Entry<String, Integer> entry :
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   338
                        CalendarDataUtility.retrieveCldrFieldValueNames(
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   339
                        "gregory", Calendar.ERA, toStyle(textStyle), locale).entrySet()) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   340
                    map.put((long) entry.getValue(), entry.getKey());
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   341
                }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   342
                if (!map.isEmpty()) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   343
                    styleMap.put(textStyle, map);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   344
                }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   345
            }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   346
            return new LocaleStore(styleMap);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   347
        }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   348
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   349
        if (field == MONTH_OF_YEAR) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   350
            Map<Long, String> map = new HashMap<>();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   351
            for (Entry<String, Integer> entry :
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   352
                    CalendarDataUtility.retrieveCldrFieldValueNames(
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   353
                    "gregory", Calendar.MONTH, Calendar.LONG_FORMAT, locale).entrySet()) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   354
                map.put((long) (entry.getValue() + 1), entry.getKey());
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   355
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   356
            styleMap.put(TextStyle.FULL, map);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   357
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   358
            map = new HashMap<>();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   359
            for (Entry<String, Integer> entry :
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   360
                    CalendarDataUtility.retrieveCldrFieldValueNames(
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   361
                    "gregory", Calendar.MONTH, Calendar.SHORT_FORMAT, locale).entrySet()) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   362
                map.put((long) (entry.getValue() + 1), entry.getKey());
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   363
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   364
            styleMap.put(TextStyle.SHORT, map);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   365
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   366
            map = new HashMap<>();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   367
            for (int month = Calendar.JANUARY; month <= Calendar.DECEMBER; month++) {
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   368
                String name;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   369
                name = CalendarDataUtility.retrieveCldrFieldValueName(
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   370
                        "gregory", Calendar.MONTH, month, Calendar.NARROW_STANDALONE, locale);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   371
                if (name != null) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   372
                    map.put((long)(month + 1), name);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   373
                }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   374
            }
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   375
            if (!map.isEmpty()) {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   376
                styleMap.put(TextStyle.NARROW, map);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   377
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   378
            return new LocaleStore(styleMap);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   379
        }
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   380
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   381
        if (field == DAY_OF_WEEK) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   382
            Map<Long, String> map = new HashMap<>();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   383
            for (Entry<String, Integer> entry :
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   384
                 CalendarDataUtility.retrieveCldrFieldValueNames(
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   385
                    "gregory", Calendar.DAY_OF_WEEK, Calendar.LONG_FORMAT, locale).entrySet()) {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   386
                map.put((long)toWeekDay(entry.getValue()), entry.getKey());
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   387
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   388
            styleMap.put(TextStyle.FULL, map);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   389
            map = new HashMap<>();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   390
            for (Entry<String, Integer> entry :
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   391
                    CalendarDataUtility.retrieveCldrFieldValueNames(
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   392
                    "gregory", Calendar.DAY_OF_WEEK, Calendar.SHORT_FORMAT, locale).entrySet()) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   393
                map.put((long) toWeekDay(entry.getValue()), entry.getKey());
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   394
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   395
            styleMap.put(TextStyle.SHORT, map);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   396
            map = new HashMap<>();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   397
            for (int wday = Calendar.SUNDAY; wday <= Calendar.SATURDAY; wday++) {
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   398
                map.put((long) toWeekDay(wday),
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   399
                        CalendarDataUtility.retrieveCldrFieldValueName(
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   400
                        "gregory", Calendar.DAY_OF_WEEK, wday, Calendar.NARROW_FORMAT, locale));
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   401
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   402
            styleMap.put(TextStyle.NARROW, map);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   403
            return new LocaleStore(styleMap);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   404
        }
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   405
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   406
        if (field == AMPM_OF_DAY) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   407
            Map<Long, String> map = new HashMap<>();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   408
            for (Entry<String, Integer> entry :
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   409
                    CalendarDataUtility.retrieveCldrFieldValueNames(
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   410
                    "gregory", Calendar.AM_PM, Calendar.LONG_FORMAT, locale).entrySet()) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   411
                map.put((long) entry.getValue(), entry.getKey());
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   412
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   413
            styleMap.put(TextStyle.FULL, map);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   414
            styleMap.put(TextStyle.SHORT, map);  // re-use, as we don't have different data
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   415
            return new LocaleStore(styleMap);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   416
        }
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   417
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   418
        return "";  // null marker for map
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   419
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   420
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   421
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   422
     * Helper method to create an immutable entry.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   423
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   424
     * @param text  the text, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   425
     * @param field  the field, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   426
     * @return the entry, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   427
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   428
    private static <A, B> Entry<A, B> createEntry(A text, B field) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   429
        return new SimpleImmutableEntry<>(text, field);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   430
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   431
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   432
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   433
     * Stores the text for a single locale.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   434
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   435
     * 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
   436
     * These textual representations can be captured in this class for printing
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   437
     * and parsing.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   438
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   439
     * This class is immutable and thread-safe.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   440
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   441
    static final class LocaleStore {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   442
        /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   443
         * Map of value to text.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   444
         */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   445
        private final Map<TextStyle, Map<Long, String>> valueTextMap;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   446
        /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   447
         * Parsable data.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   448
         */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   449
        private final Map<TextStyle, List<Entry<String, Long>>> parsable;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   450
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   451
        /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   452
         * Constructor.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   453
         *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   454
         * @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
   455
         */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   456
        LocaleStore(Map<TextStyle, Map<Long, String>> valueTextMap) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   457
            this.valueTextMap = valueTextMap;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   458
            Map<TextStyle, List<Entry<String, Long>>> map = new HashMap<>();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   459
            List<Entry<String, Long>> allList = new ArrayList<>();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   460
            for (TextStyle style : valueTextMap.keySet()) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   461
                Map<String, Entry<String, Long>> reverse = new HashMap<>();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   462
                for (Map.Entry<Long, String> entry : valueTextMap.get(style).entrySet()) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   463
                    if (reverse.put(entry.getValue(), createEntry(entry.getValue(), entry.getKey())) != null) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   464
                        // TODO: BUG: this has no effect
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   465
                        continue;  // not parsable, try next style
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
                List<Entry<String, Long>> list = new ArrayList<>(reverse.values());
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   469
                Collections.sort(list, COMPARATOR);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   470
                map.put(style, list);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   471
                allList.addAll(list);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   472
                map.put(null, allList);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   473
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   474
            Collections.sort(allList, COMPARATOR);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   475
            this.parsable = map;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   476
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   477
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   478
        /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   479
         * Gets the text for the specified field value, locale and style
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   480
         * for the purpose of printing.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   481
         *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   482
         * @param value  the value to get text for, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   483
         * @param style  the style to get text for, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   484
         * @return the text for the field value, null if no text found
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   485
         */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   486
        String getText(long value, TextStyle style) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   487
            Map<Long, String> map = valueTextMap.get(style);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   488
            return map != null ? map.get(value) : null;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   489
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   490
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   491
        /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   492
         * 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
   493
         * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   494
         * 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
   495
         *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   496
         * @param style  the style to get text for, null for all parsable text
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   497
         * @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
   498
         *  null if the style is not parsable
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   499
         */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   500
        Iterator<Entry<String, Long>> getTextIterator(TextStyle style) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   501
            List<Entry<String, Long>> list = parsable.get(style);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   502
            return list != null ? list.iterator() : null;
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
}