< prev index next >

src/java.base/share/classes/java/time/format/DateTimeTextProvider.java

Print this page
rev 17279 : [mq]: 8181586
   1 /*
   2  * Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


  95  * A provider to obtain the textual form of a date-time field.
  96  *
  97  * @implSpec
  98  * Implementations must be thread-safe.
  99  * Implementations should cache the textual information.
 100  *
 101  * @since 1.8
 102  */
 103 class DateTimeTextProvider {
 104 
 105     /** Cache. */
 106     private static final ConcurrentMap<Entry<TemporalField, Locale>, Object> CACHE = new ConcurrentHashMap<>(16, 0.75f, 2);
 107     /** Comparator. */
 108     private static final Comparator<Entry<String, Long>> COMPARATOR = new Comparator<Entry<String, Long>>() {
 109         @Override
 110         public int compare(Entry<String, Long> obj1, Entry<String, Long> obj2) {
 111             return obj2.getKey().length() - obj1.getKey().length();  // longest to shortest
 112         }
 113     };
 114 



 115     DateTimeTextProvider() {}
 116 
 117     /**
 118      * Gets the provider of text.
 119      *
 120      * @return the provider, not null
 121      */
 122     static DateTimeTextProvider getInstance() {
 123         return new DateTimeTextProvider();
 124     }
 125 
 126     /**
 127      * Gets the text for the specified field, locale and style
 128      * for the purpose of formatting.
 129      * <p>
 130      * The text associated with the value is returned.
 131      * The null return value should be used if there is no applicable text, or
 132      * if the text would be a numeric representation of the value.
 133      *
 134      * @param field  the field to get text for, not null
 135      * @param value  the field value to get text for, not null
 136      * @param style  the style to get text for, not null
 137      * @param locale  the locale to get text for, not null
 138      * @return the text for the field value, null if no text found
 139      */
 140     public String getText(TemporalField field, long value, TextStyle style, Locale locale) {
 141         Object store = findStore(field, locale);
 142         if (store instanceof LocaleStore) {
 143             return ((LocaleStore) store).getText(value, style);


   1 /*
   2  * Copyright (c) 2012, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


  95  * A provider to obtain the textual form of a date-time field.
  96  *
  97  * @implSpec
  98  * Implementations must be thread-safe.
  99  * Implementations should cache the textual information.
 100  *
 101  * @since 1.8
 102  */
 103 class DateTimeTextProvider {
 104 
 105     /** Cache. */
 106     private static final ConcurrentMap<Entry<TemporalField, Locale>, Object> CACHE = new ConcurrentHashMap<>(16, 0.75f, 2);
 107     /** Comparator. */
 108     private static final Comparator<Entry<String, Long>> COMPARATOR = new Comparator<Entry<String, Long>>() {
 109         @Override
 110         public int compare(Entry<String, Long> obj1, Entry<String, Long> obj2) {
 111             return obj2.getKey().length() - obj1.getKey().length();  // longest to shortest
 112         }
 113     };
 114 
 115     // Singleton instance
 116     private static final DateTimeTextProvider INSTANCE = new DateTimeTextProvider();
 117 
 118     DateTimeTextProvider() {}
 119 
 120     /**
 121      * Gets the provider of text.
 122      *
 123      * @return the provider, not null
 124      */
 125     static DateTimeTextProvider getInstance() {
 126         return INSTANCE;
 127     }
 128 
 129     /**
 130      * Gets the text for the specified field, locale and style
 131      * for the purpose of formatting.
 132      * <p>
 133      * The text associated with the value is returned.
 134      * The null return value should be used if there is no applicable text, or
 135      * if the text would be a numeric representation of the value.
 136      *
 137      * @param field  the field to get text for, not null
 138      * @param value  the field value to get text for, not null
 139      * @param style  the style to get text for, not null
 140      * @param locale  the locale to get text for, not null
 141      * @return the text for the field value, null if no text found
 142      */
 143     public String getText(TemporalField field, long value, TextStyle style, Locale locale) {
 144         Object store = findStore(field, locale);
 145         if (store instanceof LocaleStore) {
 146             return ((LocaleStore) store).getText(value, style);


< prev index next >