001 /* 002 * Copyright 2001-2009 Stephen Colebourne 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.joda.time.tz; 017 018 import java.text.DateFormatSymbols; 019 import java.util.HashMap; 020 import java.util.Locale; 021 import java.util.Map; 022 023 import org.joda.time.DateTimeUtils; 024 025 /** 026 * The default name provider acquires localized names from 027 * {@link DateFormatSymbols java.text.DateFormatSymbols}. 028 * <p> 029 * DefaultNameProvider is thread-safe and immutable. 030 * 031 * @author Brian S O'Neill 032 * @since 1.0 033 */ 034 @SuppressWarnings("unchecked") 035 public class DefaultNameProvider implements NameProvider { 036 // locale -> (id -> (nameKey -> [shortName, name])) 037 private HashMap<Locale, Map<String, Map<String, Object>>> iByLocaleCache = createCache(); 038 039 public DefaultNameProvider() { 040 } 041 042 public String getShortName(Locale locale, String id, String nameKey) { 043 String[] nameSet = getNameSet(locale, id, nameKey); 044 return nameSet == null ? null : nameSet[0]; 045 } 046 047 public String getName(Locale locale, String id, String nameKey) { 048 String[] nameSet = getNameSet(locale, id, nameKey); 049 return nameSet == null ? null : nameSet[1]; 050 } 051 052 private synchronized String[] getNameSet(Locale locale, String id, String nameKey) { 053 if (locale == null || id == null || nameKey == null) { 054 return null; 055 } 056 057 Map<String, Map<String, Object>> byIdCache = iByLocaleCache.get(locale); 058 if (byIdCache == null) { 059 iByLocaleCache.put(locale, byIdCache = createCache()); 060 } 061 062 Map<String, Object> byNameKeyCache = byIdCache.get(id); 063 if (byNameKeyCache == null) { 064 byIdCache.put(id, byNameKeyCache = createCache()); 065 String[][] zoneStrings = DateTimeUtils.getDateFormatSymbols(locale).getZoneStrings(); 066 for (int i=0; i<zoneStrings.length; i++) { 067 String[] set = zoneStrings[i]; 068 if (set != null && set.length == 5 && id.equals(set[0])) { 069 byNameKeyCache.put(set[2], new String[] {set[2], set[1]}); 070 // need to handle case where summer and winter have the same 071 // abbreviation, such as EST in Australia [1716305] 072 // we handle this by appending "-Summer", cf ZoneInfoCompiler 073 if (set[2].equals(set[4])) { 074 byNameKeyCache.put(set[4] + "-Summer", new String[] {set[4], set[3]}); 075 } else { 076 byNameKeyCache.put(set[4], new String[] {set[4], set[3]}); 077 } 078 break; 079 } 080 } 081 } 082 083 return (String[])byNameKeyCache.get(nameKey); 084 } 085 086 private HashMap createCache() { 087 return new HashMap(7); 088 } 089 }