All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.time4j.tz.spi.WinZoneProviderSPI Maven / Gradle / Ivy

/*
 * -----------------------------------------------------------------------
 * Copyright © 2013-2015 Meno Hochschild, 
 * -----------------------------------------------------------------------
 * This file (WinZoneProviderSPI.java) is part of project Time4J.
 *
 * Time4J is free software: You can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation, either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * Time4J is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Time4J. If not, see .
 * -----------------------------------------------------------------------
 */

package net.time4j.tz.spi;

import net.time4j.base.ResourceLoader;
import net.time4j.tz.NameStyle;
import net.time4j.tz.TZID;
import net.time4j.tz.Timezone;
import net.time4j.tz.TransitionHistory;
import net.time4j.tz.ZoneProvider;
import net.time4j.tz.other.WindowsZone;

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.net.URI;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Locale;
import java.util.Map;
import java.util.Set;


/**
 * 

SPI-implementation for support of Windows timezones.

* * @author Meno Hochschild * @since 3.1 */ public class WinZoneProviderSPI implements ZoneProvider { //~ Statische Felder/Initialisierungen -------------------------------- // Map> private static final Map> REPOSITORY; // Map> private static final Map> PREFERRED_KEYS; // Map>> public static final Map>> NAME_BASED_MAP; // Version of windowsZones.xml public static final String WIN_NAME_VERSION; private static final String VKEY = "VERSION"; static { Map> map = loadData(); WIN_NAME_VERSION = map.get(VKEY).keySet().iterator().next(); map.remove(VKEY); REPOSITORY = Collections.unmodifiableMap(map); PREFERRED_KEYS = prepareSmartMode(); NAME_BASED_MAP = prepareResolvers(); } //~ Methoden ---------------------------------------------------------- @Override public Set getAvailableIDs() { Set zones = new HashSet<>(); for (TZID tzid : Timezone.getAvailableIDs("DEFAULT")) { zones.add("WINDOWS~" + tzid.canonical()); } return Collections.unmodifiableSet(zones); } @Override public Map getAliases() { return Collections.emptyMap(); } @Override public String getFallback() { return "DEFAULT"; } @Override public String getName() { return "WINDOWS"; } @Override public String getLocation() { return ""; } @Override public String getVersion() { return ""; } @Override public TransitionHistory load(String zoneID) { return null; // uses fallback } @Override public Set getPreferredIDs( Locale locale, boolean smart ) { return getPreferredIDs(locale.getCountry(), smart); } @Override public String getDisplayName( String tzid, NameStyle style, Locale locale ) { if (tzid.isEmpty()) { return ""; } Map map = idsToNames(locale.getCountry()); String name = map.get("WINDOWS~" + tzid); return ((name == null) ? "" : name); } private static Map idsToNames(String country) { Map map = REPOSITORY.get(country); if (map == null) { return Collections.emptyMap(); } else { return Collections.unmodifiableMap(map); } } private static Set getPreferredIDs( String country, boolean smart ) { return ( smart ? getPreferences(country) : idsToNames(country).keySet()); } private static Set getPreferences(String country) { Set preferences = PREFERRED_KEYS.get(country); if (preferences == null) { return Collections.emptySet(); } else { return Collections.unmodifiableSet(preferences); } } private static Map> loadData() { ObjectInputStream ois = null; try { String source = "data/winzone.ser"; URI uri = ResourceLoader.getInstance().locate("misc", WindowsZone.class, source); InputStream is = ResourceLoader.getInstance().load(uri, true); ois = new ObjectInputStream(is); String version = ois.readUTF(); Map> data = cast(ois.readObject()); Map> map = new HashMap<>(data); map.put(VKEY, Collections.singletonMap(version, version)); return map; } catch (ClassNotFoundException | IOException ex) { throw new IllegalStateException(ex); } finally { if (ois != null) { try { ois.close(); } catch (IOException ex) { // ignore } } } } @SuppressWarnings("unchecked") private static T cast(Object obj) { return (T) obj; } private static Map> prepareSmartMode() { Map> preferredKeys = new HashMap<>(); for (String country : REPOSITORY.keySet()) { Map map = idsToNames(country); Set keys = map.keySet(); if (keys.size() >= 2) { keys = new HashSet<>(); Set names = new HashSet<>(map.values()); for (String name : names) { for (Map.Entry e : getFallbackSet()) { if (e.getValue().equals(name)) { keys.add(e.getKey()); } } } } preferredKeys.put(country, keys); } return Collections.unmodifiableMap(preferredKeys); } private static Set> getFallbackSet() { return idsToNames("001").entrySet(); } private static Map>> prepareResolvers() { Map>> nameBasedMap = new HashMap<>(); for (String country : REPOSITORY.keySet()) { Map idsToNames = REPOSITORY.get(country); for (Map.Entry e : idsToNames.entrySet()) { String id = e.getKey(); String name = e.getValue(); Map> countryToIds = nameBasedMap.get(name); if (countryToIds == null) { countryToIds = new HashMap<>(); nameBasedMap.put(name, countryToIds); } Set ids = countryToIds.get(country); if (ids == null) { ids = new HashSet<>(); countryToIds.put(country, ids); } ids.add(new WinZoneID(id)); } } return Collections.unmodifiableMap(nameBasedMap); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy