1 /* 2 * Copyright (c) 2012, 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 23 * questions. 24 */ 25 26 /* 27 * (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved 28 * (C) Copyright IBM Corp. 1996 - 1998 - All Rights Reserved 29 * 30 * The original version of this source code and documentation 31 * is copyrighted and owned by Taligent, Inc., a wholly-owned 32 * subsidiary of IBM. These materials are provided under terms 33 * of a License Agreement between Taligent and Sun. This technology 34 * is protected by multiple US and International patents. 35 * 36 * This notice and attribution to Taligent may not be removed. 37 * Taligent is a registered trademark of Taligent, Inc. 38 * 39 */ 40 41 package sun.util.locale.provider; 42 43 import java.text.MessageFormat; 44 import java.util.Calendar; 45 import java.util.Locale; 46 import java.util.ResourceBundle; 47 import java.util.concurrent.ConcurrentHashMap; 48 import java.util.concurrent.ConcurrentMap; 49 import sun.util.resources.TimeZoneNamesBundle; 50 51 /** 52 * Central accessor to locale-dependent resources. 53 * 54 * @author Masayoshi Okutsu 55 */ 56 public class LocaleResources { 57 58 private final LocaleProviderAdapter adapter; 59 private final Locale locale; 60 61 // Resource cache 62 private ConcurrentMap<String, Object> cache = new ConcurrentHashMap<>(); 63 64 65 LocaleResources(LocaleProviderAdapter adapter, Locale locale) { 66 this.adapter = adapter; 67 this.locale = locale; 68 } 69 70 public TimeZoneNamesBundle getTimeZoneNames() { 71 TimeZoneNamesBundle tznames = (TimeZoneNamesBundle) cache.get("TimeZoneNames"); 72 if (tznames == null) { 73 tznames = adapter.getLocaleData().getTimeZoneNames(locale); 74 TimeZoneNamesBundle tznb = (TimeZoneNamesBundle) cache.putIfAbsent("TimeZoneNames", tznames); 75 if (tznb != null) { 76 tznames = tznb; 77 } 78 } 79 return tznames; 80 } 81 82 public String getDateTimePattern(int timeStyle, int dateStyle, Calendar cal) { 83 String pattern; 84 85 if (cal == null) { 86 cal = Calendar.getInstance(locale); 87 } 88 String calType = cal.getCalendarType(); 89 String timePattern = null; 90 String datePattern = null; 91 if (timeStyle >= 0) { 92 timePattern = getDateTimePattern("TimePatterns", timeStyle, calType); 93 } 94 if (dateStyle >= 0) { 95 datePattern = getDateTimePattern("DatePatterns", dateStyle, calType); 96 } 97 if (timeStyle >= 0) { 98 if (dateStyle >= 0) { 99 String dateTimePattern = getDateTimePattern("DateTimePatterns", 0, calType); 100 switch (dateTimePattern) { 101 case "{1} {0}": 102 pattern = datePattern + " " + timePattern; 103 break; 104 case "{0} {1}": 105 pattern = timePattern + " " + datePattern; 106 break; 107 default: 108 pattern = MessageFormat.format(dateTimePattern, timePattern, datePattern); 109 break; 110 } 111 } else { 112 pattern = timePattern; 113 } 114 } else if (dateStyle >= 0) { 115 pattern = datePattern; 116 } else { 117 throw new IllegalArgumentException("No date or time style specified"); 118 } 119 return pattern; 120 } 121 122 public String[] getNumberPatterns() { 123 /* try the cache first */ 124 String[] numberPatterns = (String[]) cache.get("NumberPatterns"); 125 if (numberPatterns == null) { /* cache miss */ 126 ResourceBundle resource = adapter.getLocaleData().getNumberFormatData(locale); 127 numberPatterns = resource.getStringArray("NumberPatterns"); 128 /* update cache */ 129 cache.put("NumberPatterns", numberPatterns); 130 } 131 return numberPatterns; 132 } 133 134 private String getDateTimePattern(String key, int styleIndex, String calendarType) { 135 String resourceKey = "gregory".equals(calendarType) ? key : calendarType + "." + key; 136 /* try the cache first */ 137 String[] patterns = (String[]) cache.get(resourceKey); 138 if (patterns == null) { /* cache miss */ 139 ResourceBundle r = adapter.getLocaleData().getDateFormatData(locale); 140 if (r.containsKey(resourceKey)) { 141 patterns = r.getStringArray(resourceKey); 142 } else { 143 assert !resourceKey.equals(key); 144 patterns = r.getStringArray(key); 145 } 146 /* update cache */ 147 cache.putIfAbsent(resourceKey, patterns); 148 } 149 return patterns[styleIndex]; 150 } 151 }