Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/time/format/DecimalStyle.java
38918 views
/*1* Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/*26* This file is available under and governed by the GNU General Public27* License version 2 only, as published by the Free Software Foundation.28* However, the following notice accompanied the original version of this29* file:30*31* Copyright (c) 2008-2012, Stephen Colebourne & Michael Nascimento Santos32*33* All rights reserved.34*35* Redistribution and use in source and binary forms, with or without36* modification, are permitted provided that the following conditions are met:37*38* * Redistributions of source code must retain the above copyright notice,39* this list of conditions and the following disclaimer.40*41* * Redistributions in binary form must reproduce the above copyright notice,42* this list of conditions and the following disclaimer in the documentation43* and/or other materials provided with the distribution.44*45* * Neither the name of JSR-310 nor the names of its contributors46* may be used to endorse or promote products derived from this software47* without specific prior written permission.48*49* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS50* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT51* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR52* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR53* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,54* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,55* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR56* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF57* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING58* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS59* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.60*/61package java.time.format;6263import java.text.DecimalFormatSymbols;64import java.util.Collections;65import java.util.HashSet;66import java.util.Locale;67import java.util.Objects;68import java.util.Set;69import java.util.concurrent.ConcurrentHashMap;70import java.util.concurrent.ConcurrentMap;7172/**73* Localized decimal style used in date and time formatting.74* <p>75* A significant part of dealing with dates and times is the localization.76* This class acts as a central point for accessing the information.77*78* @implSpec79* This class is immutable and thread-safe.80*81* @since 1.882*/83public final class DecimalStyle {8485/**86* The standard set of non-localized decimal style symbols.87* <p>88* This uses standard ASCII characters for zero, positive, negative and a dot for the decimal point.89*/90public static final DecimalStyle STANDARD = new DecimalStyle('0', '+', '-', '.');91/**92* The cache of DecimalStyle instances.93*/94private static final ConcurrentMap<Locale, DecimalStyle> CACHE = new ConcurrentHashMap<>(16, 0.75f, 2);9596/**97* The zero digit.98*/99private final char zeroDigit;100/**101* The positive sign.102*/103private final char positiveSign;104/**105* The negative sign.106*/107private final char negativeSign;108/**109* The decimal separator.110*/111private final char decimalSeparator;112113//-----------------------------------------------------------------------114/**115* Lists all the locales that are supported.116* <p>117* The locale 'en_US' will always be present.118*119* @return a Set of Locales for which localization is supported120*/121public static Set<Locale> getAvailableLocales() {122Locale[] l = DecimalFormatSymbols.getAvailableLocales();123Set<Locale> locales = new HashSet<>(l.length);124Collections.addAll(locales, l);125return locales;126}127128/**129* Obtains the DecimalStyle for the default130* {@link java.util.Locale.Category#FORMAT FORMAT} locale.131* <p>132* This method provides access to locale sensitive decimal style symbols.133* <p>134* This is equivalent to calling135* {@link #of(Locale)136* of(Locale.getDefault(Locale.Category.FORMAT))}.137*138* @see java.util.Locale.Category#FORMAT139* @return the decimal style, not null140*/141public static DecimalStyle ofDefaultLocale() {142return of(Locale.getDefault(Locale.Category.FORMAT));143}144145/**146* Obtains the DecimalStyle for the specified locale.147* <p>148* This method provides access to locale sensitive decimal style symbols.149*150* @param locale the locale, not null151* @return the decimal style, not null152*/153public static DecimalStyle of(Locale locale) {154Objects.requireNonNull(locale, "locale");155DecimalStyle info = CACHE.get(locale);156if (info == null) {157info = create(locale);158CACHE.putIfAbsent(locale, info);159info = CACHE.get(locale);160}161return info;162}163164private static DecimalStyle create(Locale locale) {165DecimalFormatSymbols oldSymbols = DecimalFormatSymbols.getInstance(locale);166char zeroDigit = oldSymbols.getZeroDigit();167char positiveSign = '+';168char negativeSign = oldSymbols.getMinusSign();169char decimalSeparator = oldSymbols.getDecimalSeparator();170if (zeroDigit == '0' && negativeSign == '-' && decimalSeparator == '.') {171return STANDARD;172}173return new DecimalStyle(zeroDigit, positiveSign, negativeSign, decimalSeparator);174}175176//-----------------------------------------------------------------------177/**178* Restricted constructor.179*180* @param zeroChar the character to use for the digit of zero181* @param positiveSignChar the character to use for the positive sign182* @param negativeSignChar the character to use for the negative sign183* @param decimalPointChar the character to use for the decimal point184*/185private DecimalStyle(char zeroChar, char positiveSignChar, char negativeSignChar, char decimalPointChar) {186this.zeroDigit = zeroChar;187this.positiveSign = positiveSignChar;188this.negativeSign = negativeSignChar;189this.decimalSeparator = decimalPointChar;190}191192//-----------------------------------------------------------------------193/**194* Gets the character that represents zero.195* <p>196* The character used to represent digits may vary by culture.197* This method specifies the zero character to use, which implies the characters for one to nine.198*199* @return the character for zero200*/201public char getZeroDigit() {202return zeroDigit;203}204205/**206* Returns a copy of the info with a new character that represents zero.207* <p>208* The character used to represent digits may vary by culture.209* This method specifies the zero character to use, which implies the characters for one to nine.210*211* @param zeroDigit the character for zero212* @return a copy with a new character that represents zero, not null213214*/215public DecimalStyle withZeroDigit(char zeroDigit) {216if (zeroDigit == this.zeroDigit) {217return this;218}219return new DecimalStyle(zeroDigit, positiveSign, negativeSign, decimalSeparator);220}221222//-----------------------------------------------------------------------223/**224* Gets the character that represents the positive sign.225* <p>226* The character used to represent a positive number may vary by culture.227* This method specifies the character to use.228*229* @return the character for the positive sign230*/231public char getPositiveSign() {232return positiveSign;233}234235/**236* Returns a copy of the info with a new character that represents the positive sign.237* <p>238* The character used to represent a positive number may vary by culture.239* This method specifies the character to use.240*241* @param positiveSign the character for the positive sign242* @return a copy with a new character that represents the positive sign, not null243*/244public DecimalStyle withPositiveSign(char positiveSign) {245if (positiveSign == this.positiveSign) {246return this;247}248return new DecimalStyle(zeroDigit, positiveSign, negativeSign, decimalSeparator);249}250251//-----------------------------------------------------------------------252/**253* Gets the character that represents the negative sign.254* <p>255* The character used to represent a negative number may vary by culture.256* This method specifies the character to use.257*258* @return the character for the negative sign259*/260public char getNegativeSign() {261return negativeSign;262}263264/**265* Returns a copy of the info with a new character that represents the negative sign.266* <p>267* The character used to represent a negative number may vary by culture.268* This method specifies the character to use.269*270* @param negativeSign the character for the negative sign271* @return a copy with a new character that represents the negative sign, not null272*/273public DecimalStyle withNegativeSign(char negativeSign) {274if (negativeSign == this.negativeSign) {275return this;276}277return new DecimalStyle(zeroDigit, positiveSign, negativeSign, decimalSeparator);278}279280//-----------------------------------------------------------------------281/**282* Gets the character that represents the decimal point.283* <p>284* The character used to represent a decimal point may vary by culture.285* This method specifies the character to use.286*287* @return the character for the decimal point288*/289public char getDecimalSeparator() {290return decimalSeparator;291}292293/**294* Returns a copy of the info with a new character that represents the decimal point.295* <p>296* The character used to represent a decimal point may vary by culture.297* This method specifies the character to use.298*299* @param decimalSeparator the character for the decimal point300* @return a copy with a new character that represents the decimal point, not null301*/302public DecimalStyle withDecimalSeparator(char decimalSeparator) {303if (decimalSeparator == this.decimalSeparator) {304return this;305}306return new DecimalStyle(zeroDigit, positiveSign, negativeSign, decimalSeparator);307}308309//-----------------------------------------------------------------------310/**311* Checks whether the character is a digit, based on the currently set zero character.312*313* @param ch the character to check314* @return the value, 0 to 9, of the character, or -1 if not a digit315*/316int convertToDigit(char ch) {317int val = ch - zeroDigit;318return (val >= 0 && val <= 9) ? val : -1;319}320321/**322* Converts the input numeric text to the internationalized form using the zero character.323*324* @param numericText the text, consisting of digits 0 to 9, to convert, not null325* @return the internationalized text, not null326*/327String convertNumberToI18N(String numericText) {328if (zeroDigit == '0') {329return numericText;330}331int diff = zeroDigit - '0';332char[] array = numericText.toCharArray();333for (int i = 0; i < array.length; i++) {334array[i] = (char) (array[i] + diff);335}336return new String(array);337}338339//-----------------------------------------------------------------------340/**341* Checks if this DecimalStyle is equal to another DecimalStyle.342*343* @param obj the object to check, null returns false344* @return true if this is equal to the other date345*/346@Override347public boolean equals(Object obj) {348if (this == obj) {349return true;350}351if (obj instanceof DecimalStyle) {352DecimalStyle other = (DecimalStyle) obj;353return (zeroDigit == other.zeroDigit && positiveSign == other.positiveSign &&354negativeSign == other.negativeSign && decimalSeparator == other.decimalSeparator);355}356return false;357}358359/**360* A hash code for this DecimalStyle.361*362* @return a suitable hash code363*/364@Override365public int hashCode() {366return zeroDigit + positiveSign + negativeSign + decimalSeparator;367}368369//-----------------------------------------------------------------------370/**371* Returns a string describing this DecimalStyle.372*373* @return a string description, not null374*/375@Override376public String toString() {377return "DecimalStyle[" + zeroDigit + positiveSign + negativeSign + decimalSeparator + "]";378}379380}381382383