Path: blob/master/src/java.base/share/classes/java/time/format/DecimalStyle.java
41159 views
/*1* Copyright (c) 2012, 2013, 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* If the locale contains "nu" (Numbering System) and/or "rg"150* (Region Override) <a href="../../util/Locale.html#def_locale_extension">151* Unicode extensions</a>, returned instance will reflect the values specified with152* those extensions. If both "nu" and "rg" are specified, the value from153* the "nu" extension supersedes the implicit one from the "rg" extension.154*155* @param locale the locale, not null156* @return the decimal style, not null157*/158public static DecimalStyle of(Locale locale) {159Objects.requireNonNull(locale, "locale");160DecimalStyle info = CACHE.get(locale);161if (info == null) {162info = create(locale);163CACHE.putIfAbsent(locale, info);164info = CACHE.get(locale);165}166return info;167}168169private static DecimalStyle create(Locale locale) {170DecimalFormatSymbols oldSymbols = DecimalFormatSymbols.getInstance(locale);171char zeroDigit = oldSymbols.getZeroDigit();172char positiveSign = '+';173char negativeSign = oldSymbols.getMinusSign();174char decimalSeparator = oldSymbols.getDecimalSeparator();175if (zeroDigit == '0' && negativeSign == '-' && decimalSeparator == '.') {176return STANDARD;177}178return new DecimalStyle(zeroDigit, positiveSign, negativeSign, decimalSeparator);179}180181//-----------------------------------------------------------------------182/**183* Restricted constructor.184*185* @param zeroChar the character to use for the digit of zero186* @param positiveSignChar the character to use for the positive sign187* @param negativeSignChar the character to use for the negative sign188* @param decimalPointChar the character to use for the decimal point189*/190private DecimalStyle(char zeroChar, char positiveSignChar, char negativeSignChar, char decimalPointChar) {191this.zeroDigit = zeroChar;192this.positiveSign = positiveSignChar;193this.negativeSign = negativeSignChar;194this.decimalSeparator = decimalPointChar;195}196197//-----------------------------------------------------------------------198/**199* Gets the character that represents zero.200* <p>201* The character used to represent digits may vary by culture.202* This method specifies the zero character to use, which implies the characters for one to nine.203*204* @return the character for zero205*/206public char getZeroDigit() {207return zeroDigit;208}209210/**211* Returns a copy of the info with a new character that represents zero.212* <p>213* The character used to represent digits may vary by culture.214* This method specifies the zero character to use, which implies the characters for one to nine.215*216* @param zeroDigit the character for zero217* @return a copy with a new character that represents zero, not null218*/219public DecimalStyle withZeroDigit(char zeroDigit) {220if (zeroDigit == this.zeroDigit) {221return this;222}223return new DecimalStyle(zeroDigit, positiveSign, negativeSign, decimalSeparator);224}225226//-----------------------------------------------------------------------227/**228* Gets the character that represents the positive sign.229* <p>230* The character used to represent a positive number may vary by culture.231* This method specifies the character to use.232*233* @return the character for the positive sign234*/235public char getPositiveSign() {236return positiveSign;237}238239/**240* Returns a copy of the info with a new character that represents the positive sign.241* <p>242* The character used to represent a positive number may vary by culture.243* This method specifies the character to use.244*245* @param positiveSign the character for the positive sign246* @return a copy with a new character that represents the positive sign, not null247*/248public DecimalStyle withPositiveSign(char positiveSign) {249if (positiveSign == this.positiveSign) {250return this;251}252return new DecimalStyle(zeroDigit, positiveSign, negativeSign, decimalSeparator);253}254255//-----------------------------------------------------------------------256/**257* Gets the character that represents the negative sign.258* <p>259* The character used to represent a negative number may vary by culture.260* This method specifies the character to use.261*262* @return the character for the negative sign263*/264public char getNegativeSign() {265return negativeSign;266}267268/**269* Returns a copy of the info with a new character that represents the negative sign.270* <p>271* The character used to represent a negative number may vary by culture.272* This method specifies the character to use.273*274* @param negativeSign the character for the negative sign275* @return a copy with a new character that represents the negative sign, not null276*/277public DecimalStyle withNegativeSign(char negativeSign) {278if (negativeSign == this.negativeSign) {279return this;280}281return new DecimalStyle(zeroDigit, positiveSign, negativeSign, decimalSeparator);282}283284//-----------------------------------------------------------------------285/**286* Gets the character that represents the decimal point.287* <p>288* The character used to represent a decimal point may vary by culture.289* This method specifies the character to use.290*291* @return the character for the decimal point292*/293public char getDecimalSeparator() {294return decimalSeparator;295}296297/**298* Returns a copy of the info with a new character that represents the decimal point.299* <p>300* The character used to represent a decimal point may vary by culture.301* This method specifies the character to use.302*303* @param decimalSeparator the character for the decimal point304* @return a copy with a new character that represents the decimal point, not null305*/306public DecimalStyle withDecimalSeparator(char decimalSeparator) {307if (decimalSeparator == this.decimalSeparator) {308return this;309}310return new DecimalStyle(zeroDigit, positiveSign, negativeSign, decimalSeparator);311}312313//-----------------------------------------------------------------------314/**315* Checks whether the character is a digit, based on the currently set zero character.316*317* @param ch the character to check318* @return the value, 0 to 9, of the character, or -1 if not a digit319*/320int convertToDigit(char ch) {321int val = ch - zeroDigit;322return (val >= 0 && val <= 9) ? val : -1;323}324325/**326* Converts the input numeric text to the internationalized form using the zero character.327*328* @param numericText the text, consisting of digits 0 to 9, to convert, not null329* @return the internationalized text, not null330*/331String convertNumberToI18N(String numericText) {332if (zeroDigit == '0') {333return numericText;334}335int diff = zeroDigit - '0';336char[] array = numericText.toCharArray();337for (int i = 0; i < array.length; i++) {338array[i] = (char) (array[i] + diff);339}340return new String(array);341}342343//-----------------------------------------------------------------------344/**345* Checks if this DecimalStyle is equal to another DecimalStyle.346*347* @param obj the object to check, null returns false348* @return true if this is equal to the other date349*/350@Override351public boolean equals(Object obj) {352if (this == obj) {353return true;354}355return (obj instanceof DecimalStyle other356&& zeroDigit == other.zeroDigit357&& positiveSign == other.positiveSign358&& negativeSign == other.negativeSign359&& decimalSeparator == other.decimalSeparator);360}361362/**363* A hash code for this DecimalStyle.364*365* @return a suitable hash code366*/367@Override368public int hashCode() {369return zeroDigit + positiveSign + negativeSign + decimalSeparator;370}371372//-----------------------------------------------------------------------373/**374* Returns a string describing this DecimalStyle.375*376* @return a string description, not null377*/378@Override379public String toString() {380return "DecimalStyle[" + zeroDigit + positiveSign + negativeSign + decimalSeparator + "]";381}382383}384385386