Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/PluggableLocale/providersrc/CalendarNameProviderImpl.java
47311 views
/*1* Copyright (c) 2012, 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.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223package com.bar;2425import com.foobar.Utils;26import java.util.Arrays;27import static java.util.Calendar.*;28import java.util.HashMap;29import java.util.Locale;30import java.util.Map;31import java.util.spi.CalendarNameProvider;3233public class CalendarNameProviderImpl extends CalendarNameProvider {34static final char FULLWIDTH_ZERO = '\uff10';35static final Locale[] avail = {36new Locale("ja", "JP", "kids"),37};3839@Override40public String getDisplayName(String calendarType, int field, int value, int style, Locale locale) {41if (calendarType == null || locale == null) {42throw new NullPointerException();43}44if (!Utils.supportsLocale(Arrays.asList(avail), locale)) {45throw new IllegalArgumentException("locale is not one of available locales: "+ locale);46}47if (field != MONTH) {48return null;49}50return toMonthName(value + 1, style);51}5253@Override54public Map<String, Integer> getDisplayNames(String calendarType, int field, int style, Locale locale) {55if (calendarType == null || locale == null) {56throw new NullPointerException();57}58if (!Utils.supportsLocale(Arrays.asList(avail), locale)) {59throw new IllegalArgumentException("locale is not one of available locales: " + locale);60}61if (field != MONTH) {62return null;63}64Map<String, Integer> map = new HashMap<>();65if (style == LONG_STANDALONE) {66style = LONG;67} else if (style == SHORT_STANDALONE) {68style = SHORT;69}70for (int month = JANUARY; month <= DECEMBER; month++) {71if (style == ALL_STYLES || style == LONG) {72map.put(toMonthName(month + 1, LONG), month);73}74if (style == ALL_STYLES || style == SHORT) {75map.put(toMonthName(month + 1, SHORT), month);76}77}78return map;79}8081@Override82public Locale[] getAvailableLocales() {83return avail.clone();84}8586// month is 1-based.87public static String toMonthName(int month, int style) {88StringBuilder sb = new StringBuilder();89if (month >= 10) {90sb.append((char)(FULLWIDTH_ZERO + 1));91sb.appendCodePoint((char)(FULLWIDTH_ZERO + (month % 10)));92} else {93sb.appendCodePoint((char)(FULLWIDTH_ZERO + month));94}95if (style == SHORT || style == SHORT_STANDALONE) {96return sb.toString(); // full-width digit(s)97}98sb.append("\u304c\u3064"); // + "gatsu" in Hiragana99return sb.toString();100}101}102103104