1 16 package org.joda.time.tz; 17 18 import java.text.DateFormatSymbols ; 19 import java.util.HashMap ; 20 import java.util.Locale ; 21 22 31 public class DefaultNameProvider implements NameProvider { 32 private HashMap iByLocaleCache = createCache(); 34 35 public DefaultNameProvider() { 36 } 37 38 public String getShortName(Locale locale, String id, String nameKey) { 39 String [] nameSet = getNameSet(locale, id, nameKey); 40 return nameSet == null ? null : nameSet[0]; 41 } 42 43 public String getName(Locale locale, String id, String nameKey) { 44 String [] nameSet = getNameSet(locale, id, nameKey); 45 return nameSet == null ? null : nameSet[1]; 46 } 47 48 private synchronized String [] getNameSet(Locale locale, String id, String nameKey) { 49 if (locale == null || id == null || nameKey == null) { 50 return null; 51 } 52 53 HashMap byIdCache = (HashMap )iByLocaleCache.get(locale); 54 if (byIdCache == null) { 55 iByLocaleCache.put(locale, byIdCache = createCache()); 56 } 57 58 HashMap byNameKeyCache = (HashMap )byIdCache.get(id); 59 if (byNameKeyCache == null) { 60 byIdCache.put(id, byNameKeyCache = createCache()); 61 String [][] zoneStrings = new DateFormatSymbols (locale).getZoneStrings(); 62 for (int i=0; i<zoneStrings.length; i++) { 63 String [] set = zoneStrings[i]; 64 if (set != null && set.length == 5 && id.equals(set[0])) { 65 byNameKeyCache.put(set[2], new String [] {set[2], set[1]}); 66 byNameKeyCache.put(set[4], new String [] {set[4], set[3]}); 67 } 68 } 69 } 70 71 return (String [])byNameKeyCache.get(nameKey); 72 } 73 74 private HashMap createCache() { 75 return new HashMap (7); 76 } 77 } 78 | Popular Tags |