alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Java example source code file (DefaultNameProvider.java)

This example Java source code file (DefaultNameProvider.java) is included in the alvinalexander.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Learn more about this Java project at its project page.

Java - Java tags/keywords

defaultnameprovider, etc, hashmap, map, object, string, summer, suppresswarnings, text, util

The DefaultNameProvider.java Java example source code

/*
 *  Copyright 2001-2011 Stephen Colebourne
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.joda.time.tz;

import java.text.DateFormatSymbols;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import org.joda.time.DateTimeUtils;

/**
 * The default name provider acquires localized names from
 * {@link DateFormatSymbols java.text.DateFormatSymbols}.
 * <p>
 * DefaultNameProvider is thread-safe and immutable.
 *
 * @author Brian S O'Neill
 * @since 1.0
 */
@SuppressWarnings("unchecked")
public class DefaultNameProvider implements NameProvider {
    // locale -> (id -> (nameKey -> [shortName, name]))
    private HashMap<Locale, Map> iByLocaleCache = createCache();
    private HashMap<Locale, Map> iByLocaleCache2 = createCache();

    public DefaultNameProvider() {
    }

    //-----------------------------------------------------------------------
    // retained original code for name lookup, not used in normal code
    // this code could be refactored to avoid duplication, but leaving it as is ensures backward compatibility
    public String getShortName(Locale locale, String id, String nameKey) {
        String[] nameSet = getNameSet(locale, id, nameKey);
        return nameSet == null ? null : nameSet[0];
    }
    
    public String getName(Locale locale, String id, String nameKey) {
        String[] nameSet = getNameSet(locale, id, nameKey);
        return nameSet == null ? null : nameSet[1];
    }

    private synchronized String[] getNameSet(Locale locale, String id, String nameKey) {
        if (locale == null || id == null || nameKey == null) {
            return null;
        }

        Map<String, Map byIdCache = iByLocaleCache.get(locale);
        if (byIdCache == null) {
            iByLocaleCache.put(locale, byIdCache = createCache());
        }

        Map<String, Object> byNameKeyCache = byIdCache.get(id);
        if (byNameKeyCache == null) {
            byIdCache.put(id, byNameKeyCache = createCache());
            
            String[][] zoneStringsEn = DateTimeUtils.getDateFormatSymbols(Locale.ENGLISH).getZoneStrings();
            String[] setEn = null;
            for (String[] strings : zoneStringsEn) {
                if (strings != null && strings.length >= 5 && id.equals(strings[0])) {
                    setEn = strings;
                    break;
                }
            }
            String[][] zoneStringsLoc = DateTimeUtils.getDateFormatSymbols(locale).getZoneStrings();
            String[] setLoc = null;
            for (String[] strings : zoneStringsLoc) {
                if (strings != null && strings.length >= 5 && id.equals(strings[0])) {
                    setLoc = strings;
                    break;
                }
            }
            
            if (setEn != null && setLoc != null) {
                byNameKeyCache.put(setEn[2], new String[] {setLoc[2], setLoc[1]});
                // need to handle case where summer and winter have the same
                // abbreviation, such as EST in Australia [1716305]
                // we handle this by appending "-Summer", cf ZoneInfoCompiler
                if (setEn[2].equals(setEn[4])) {
                    byNameKeyCache.put(setEn[4] + "-Summer", new String[] {setLoc[4], setLoc[3]});
                } else {
                    byNameKeyCache.put(setEn[4], new String[] {setLoc[4], setLoc[3]});
                }
            }
        }
        return (String[]) byNameKeyCache.get(nameKey);
    }

    //-----------------------------------------------------------------------
    // change lookup to operate on boolean standard/summer time flag
    // handles changes to the nameKey better
    public String getShortName(Locale locale, String id, String nameKey, boolean standardTime) {
        String[] nameSet = getNameSet(locale, id, nameKey, standardTime);
        return nameSet == null ? null : nameSet[0];
    }
    
    public String getName(Locale locale, String id, String nameKey, boolean standardTime) {
        String[] nameSet = getNameSet(locale, id, nameKey, standardTime);
        return nameSet == null ? null : nameSet[1];
    }

    private synchronized String[] getNameSet(Locale locale, String id, String nameKey, boolean standardTime) {
        if (locale == null || id == null || nameKey == null) {
            return null;
        }
        if (id.startsWith("Etc/")) {
            id = id.substring(4);
        }

        Map<String, Map byIdCache = iByLocaleCache2.get(locale);
        if (byIdCache == null) {
            iByLocaleCache2.put(locale, byIdCache = createCache());
        }

        Map<Boolean, Object> byNameKeyCache = byIdCache.get(id);
        if (byNameKeyCache == null) {
            byIdCache.put(id, byNameKeyCache = createCache());
            
            String[][] zoneStringsEn = DateTimeUtils.getDateFormatSymbols(Locale.ENGLISH).getZoneStrings();
            String[] setEn = null;
            for (String[] strings : zoneStringsEn) {
                if (strings != null && strings.length >= 5 && id.equals(strings[0])) {
                    setEn = strings;
                    break;
                }
            }
            String[][] zoneStringsLoc = DateTimeUtils.getDateFormatSymbols(locale).getZoneStrings();
            String[] setLoc = null;
            for (String[] strings : zoneStringsLoc) {
                if (strings != null && strings.length >= 5 && id.equals(strings[0])) {
                    setLoc = strings;
                    break;
                }
            }
            
            if (setEn != null && setLoc != null) {
                byNameKeyCache.put(Boolean.TRUE, new String[] {setLoc[2], setLoc[1]});
                byNameKeyCache.put(Boolean.FALSE, new String[] {setLoc[4], setLoc[3]});
            }
        }
        return (String[]) byNameKeyCache.get(Boolean.valueOf(standardTime));
    }

    //-----------------------------------------------------------------------
    private HashMap createCache() {
        return new HashMap(7);
    }
}

Other Java examples (source code examples)

Here is a short list of links related to this Java DefaultNameProvider.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.