jdk/src/share/classes/java/time/format/DateTimeTextProvider.java
author sherman
Tue, 22 Jan 2013 20:59:21 -0800
changeset 15289 3ac550392e43
child 15658 55b829ca2334
permissions -rw-r--r--
8003680: JSR 310 Date/Time API Summary: Integration of JSR310 Date/Time API for M6 Reviewed-by: alanb, naoto, dholmes Contributed-by: scolebourne@joda.org, roger.riggs@oracle.com, richard.warburton@gmail.com, misterm@gmail.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;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    66
import static java.time.temporal.ChronoField.MONTH_OF_YEAR;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    67
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    68
import java.time.temporal.Chrono;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    69
import java.time.temporal.ISOChrono;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    70
import java.time.calendar.JapaneseChrono;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    71
import java.time.calendar.ThaiBuddhistChrono;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    72
import java.time.temporal.TemporalField;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    73
import java.util.AbstractMap.SimpleImmutableEntry;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    74
import java.util.ArrayList;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    75
import java.util.Calendar;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    76
import java.util.Collections;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    77
import java.util.Comparator;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    78
import java.util.HashMap;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    79
import java.util.Iterator;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    80
import java.util.List;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    81
import java.util.Locale;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    82
import java.util.Map;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    83
import java.util.Map.Entry;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    84
import java.util.concurrent.ConcurrentHashMap;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    85
import java.util.concurrent.ConcurrentMap;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    86
import java.util.spi.CalendarNameProvider;
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.LocaleProviderAdapter;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    89
import sun.util.locale.provider.CalendarDataUtility;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    90
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    91
/**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    92
 * A provider to obtain the textual form of a date-time field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    93
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    94
 * <h3>Specification for implementors</h3>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    95
 * Implementations must be thread-safe.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    96
 * Implementations should cache the textual information.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    97
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    98
 * @since 1.8
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    99
 */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   100
class DateTimeTextProvider {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   101
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   102
    /** Cache. */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   103
    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
   104
    /** Comparator. */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   105
    private static final Comparator<Entry<String, Long>> COMPARATOR = new Comparator<Entry<String, Long>>() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   106
        @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   107
        public int compare(Entry<String, Long> obj1, Entry<String, Long> obj2) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   108
            return obj2.getKey().length() - obj1.getKey().length();  // longest to shortest
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
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   112
    DateTimeTextProvider() {}
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
     * Gets the provider of text.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   116
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   117
     * @return the provider, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   118
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   119
    static DateTimeTextProvider getInstance() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   120
        return new DateTimeTextProvider();
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
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   124
     * Gets the text for the specified field, locale and style
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   125
     * for the purpose of printing.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   126
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   127
     * The text associated with the value is returned.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   128
     * 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
   129
     * if the text would be a numeric representation of the value.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   130
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   131
     * @param field  the field to get text for, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   132
     * @param value  the field value to get text for, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   133
     * @param style  the style to get text for, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   134
     * @param locale  the locale to get text for, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   135
     * @return the text for the field value, null if no text found
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   136
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   137
    public String getText(TemporalField field, long value, TextStyle style, Locale locale) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   138
        Object store = findStore(field, locale);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   139
        if (store instanceof LocaleStore) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   140
            return ((LocaleStore) store).getText(value, style);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   141
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   142
        return null;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   143
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   144
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   145
    private static int toStyle(TextStyle style) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   146
        if (style == TextStyle.FULL) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   147
            return Calendar.LONG_FORMAT;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   148
        } else if (style == TextStyle.SHORT) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   149
            return Calendar.SHORT_FORMAT;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   150
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   151
        return Calendar.NARROW_STANDALONE;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   152
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   153
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   154
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   155
     * Gets the era text for the specified chrono, value, style and locale
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   156
     * for the purpose of printing.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   157
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   158
     * The era text associated with the value is returned.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   159
     * 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
   160
     * if the text would be a numeric representation of the value.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   161
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   162
     * @param chrono the chrono to get text for, not null
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
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   166
     * @return the era text for the value, null if no text found
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   167
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   168
    public String getEraText(Chrono chrono, long value, TextStyle style, Locale locale) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   169
        String type = null;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   170
        if (chrono == ISOChrono.INSTANCE) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   171
            type = "gregory";
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   172
        } else if (chrono == JapaneseChrono.INSTANCE) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   173
            type = "japanese";
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   174
            if (value == -999) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   175
                value = 0;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   176
            } else {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   177
                value += 2;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   178
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   179
        } else if (chrono == ThaiBuddhistChrono.INSTANCE) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   180
            type = "buddhist";
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   181
        } else {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   182
            return null;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   183
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   184
        return CalendarDataUtility.retrieveFieldValueName(
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   185
            type, Calendar.ERA, (int)value, toStyle(style), locale);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   186
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   187
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   188
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   189
     * 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
   190
     * for the purpose of parsing.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   191
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   192
     * 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
   193
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   194
     * 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
   195
     * if the text would be a numeric representation of the value.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   196
     * 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
   197
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   198
     * @param field  the field to get text for, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   199
     * @param style  the style to get text for, null for all parsable text
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   200
     * @param locale  the locale to get text for, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   201
     * @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
   202
     *  null if the field or style is not parsable
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   203
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   204
    public Iterator<Entry<String, Long>> getTextIterator(TemporalField field, TextStyle style, Locale locale) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   205
        Object store = findStore(field, locale);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   206
        if (store instanceof LocaleStore) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   207
            return ((LocaleStore) store).getTextIterator(style);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   208
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   209
        return null;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   210
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   211
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   212
    private Object findStore(TemporalField field, Locale locale) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   213
        Entry<TemporalField, Locale> key = createEntry(field, locale);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   214
        Object store = CACHE.get(key);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   215
        if (store == null) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   216
            store = createStore(field, locale);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   217
            CACHE.putIfAbsent(key, store);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   218
            store = CACHE.get(key);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   219
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   220
        return store;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   221
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   222
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   223
    private static int toWeekDay(int calWeekDay) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   224
        if (calWeekDay == Calendar.SUNDAY) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   225
            return 7;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   226
        } else {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   227
            return calWeekDay - 1;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   228
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   229
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   230
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   231
    private Object createStore(TemporalField field, Locale locale) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   232
        CalendarNameProvider provider = LocaleProviderAdapter.getAdapter(CalendarNameProvider.class, locale)
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   233
                                                             .getCalendarNameProvider();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   234
        Map<TextStyle, Map<Long, String>> styleMap = new HashMap<>();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   235
        if (field == MONTH_OF_YEAR) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   236
            Map<Long, String> map = new HashMap<>();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   237
            for (Entry<String, Integer> entry :
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   238
                 provider.getDisplayNames("gregory", Calendar.MONTH, Calendar.LONG_FORMAT, locale).entrySet()) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   239
                map.put((long)(entry.getValue() + 1), entry.getKey());
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   240
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   241
            styleMap.put(TextStyle.FULL, map);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   242
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   243
            map = new HashMap<>();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   244
            for (Entry<String, Integer> entry :
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   245
                 provider.getDisplayNames("gregory", Calendar.MONTH, Calendar.SHORT_FORMAT, locale).entrySet()) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   246
                map.put((long)(entry.getValue() + 1), entry.getKey());
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   247
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   248
            styleMap.put(TextStyle.SHORT, map);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   249
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   250
            map = new HashMap<>();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   251
            for (int month = Calendar.JANUARY; month <= Calendar.DECEMBER; month++) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   252
                String name = provider.getDisplayName("gregory", Calendar.MONTH, month, Calendar.NARROW_STANDALONE, locale);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   253
                if (name != null) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   254
                    map.put((long)(month + 1), name);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   255
                }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   256
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   257
            if (map.size() != 0) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   258
                styleMap.put(TextStyle.NARROW, map);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   259
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   260
            return new LocaleStore(styleMap);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   261
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   262
        if (field == DAY_OF_WEEK) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   263
            Map<Long, String> map = new HashMap<>();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   264
            for (Entry<String, Integer> entry :
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   265
                 provider.getDisplayNames("gregory", Calendar.DAY_OF_WEEK, Calendar.LONG_FORMAT, locale).entrySet()) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   266
                map.put((long)toWeekDay(entry.getValue()), entry.getKey());
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   267
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   268
            styleMap.put(TextStyle.FULL, map);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   269
            map = new HashMap<>();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   270
            for (Entry<String, Integer> entry :
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   271
                 provider.getDisplayNames("gregory", Calendar.DAY_OF_WEEK, Calendar.SHORT_FORMAT, locale).entrySet()) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   272
                map.put((long)toWeekDay(entry.getValue()), entry.getKey());
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   273
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   274
            styleMap.put(TextStyle.SHORT, map);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   275
            map = new HashMap<>();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   276
            for (int wday = Calendar.SUNDAY; wday <= Calendar.SATURDAY; wday++) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   277
                map.put((long)toWeekDay(wday),
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   278
                        provider.getDisplayName("gregory", Calendar.DAY_OF_WEEK, wday, Calendar.NARROW_FORMAT, locale));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   279
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   280
            styleMap.put(TextStyle.NARROW, map);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   281
            return new LocaleStore(styleMap);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   282
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   283
        if (field == AMPM_OF_DAY) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   284
            Map<Long, String> map = new HashMap<>();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   285
            for (Entry<String, Integer> entry :
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   286
                 provider.getDisplayNames("gregory", Calendar.AM_PM, Calendar.LONG_FORMAT, locale).entrySet()) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   287
                map.put((long)entry.getValue(), entry.getKey());
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   288
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   289
            styleMap.put(TextStyle.FULL, map);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   290
            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
   291
            return new LocaleStore(styleMap);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   292
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   293
        return "";  // null marker for map
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   294
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   295
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   296
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   297
     * Helper method to create an immutable entry.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   298
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   299
     * @param text  the text, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   300
     * @param field  the field, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   301
     * @return the entry, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   302
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   303
    private static <A, B> Entry<A, B> createEntry(A text, B field) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   304
        return new SimpleImmutableEntry<>(text, field);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   305
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   306
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   307
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   308
     * Stores the text for a single locale.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   309
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   310
     * 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
   311
     * These textual representations can be captured in this class for printing
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   312
     * and parsing.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   313
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   314
     * This class is immutable and thread-safe.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   315
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   316
    static final class LocaleStore {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   317
        /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   318
         * Map of value to text.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   319
         */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   320
        private final Map<TextStyle, Map<Long, String>> valueTextMap;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   321
        /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   322
         * Parsable data.
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 final Map<TextStyle, List<Entry<String, Long>>> parsable;
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
         * Constructor.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   328
         *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   329
         * @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
   330
         */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   331
        LocaleStore(Map<TextStyle, Map<Long, String>> valueTextMap) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   332
            this.valueTextMap = valueTextMap;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   333
            Map<TextStyle, List<Entry<String, Long>>> map = new HashMap<>();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   334
            List<Entry<String, Long>> allList = new ArrayList<>();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   335
            for (TextStyle style : valueTextMap.keySet()) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   336
                Map<String, Entry<String, Long>> reverse = new HashMap<>();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   337
                for (Map.Entry<Long, String> entry : valueTextMap.get(style).entrySet()) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   338
                    if (reverse.put(entry.getValue(), createEntry(entry.getValue(), entry.getKey())) != null) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   339
                        // TODO: BUG: this has no effect
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   340
                        continue;  // not parsable, try next style
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   341
                    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   342
                }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   343
                List<Entry<String, Long>> list = new ArrayList<>(reverse.values());
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   344
                Collections.sort(list, COMPARATOR);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   345
                map.put(style, list);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   346
                allList.addAll(list);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   347
                map.put(null, allList);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   348
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   349
            Collections.sort(allList, COMPARATOR);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   350
            this.parsable = map;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   351
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   352
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   353
        /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   354
         * Gets the text for the specified field value, locale and style
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   355
         * for the purpose of printing.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   356
         *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   357
         * @param value  the value to get text for, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   358
         * @param style  the style to get text for, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   359
         * @return the text for the field value, null if no text found
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   360
         */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   361
        String getText(long value, TextStyle style) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   362
            Map<Long, String> map = valueTextMap.get(style);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   363
            return map != null ? map.get(value) : null;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   364
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   365
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   366
        /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   367
         * 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
   368
         * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   369
         * 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
   370
         *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   371
         * @param style  the style to get text for, null for all parsable text
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   372
         * @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
   373
         *  null if the style is not parsable
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   374
         */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   375
        Iterator<Entry<String, Long>> getTextIterator(TextStyle style) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   376
            List<Entry<String, Long>> list = parsable.get(style);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   377
            return list != null ? list.iterator() : null;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   378
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   379
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   380
}