001/*
002 * oauth2-oidc-sdk
003 *
004 * Copyright 2012-2021, Connect2id Ltd and contributors.
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use
007 * this file except in compliance with the License. You may obtain a copy of the
008 * License at
009 *
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software distributed
013 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
014 * CONDITIONS OF ANY KIND, either express or implied. See the License for the
015 * specific language governing permissions and limitations under the License.
016 */
017
018package com.nimbusds.openid.connect.sdk.assurance.claims;
019
020
021import java.io.IOException;
022import java.io.InputStream;
023import java.util.Properties;
024
025
026/**
027 * Utility for mapping between ISO 3166-1 alpha-2 and alpha-3 country codes.
028 */
029public class ISO3166_1AlphaCountryCodeMapper {
030        
031        
032        /**
033         * The map resource.
034         */
035        public static final String RESOURCE_FILE_NAME = "iso3166_1alpha-2-3-map.properties";
036        
037        
038        /**
039         * Maps 2 to 3 letter codes.
040         */
041        private static final Properties MAP_2_3 = new Properties();
042        
043        
044        /**
045         * Maps 3 to 2 letter codes (reverse map).
046         */
047        private static final Properties MAP_3_2 = new Properties();
048        
049        
050        private static void lazyLoadMap_2_3() {
051                
052                if (! MAP_2_3.isEmpty()) {
053                        return;
054                }
055                
056                // Resource based on https://en.wikipedia.org/w/index.php?title=ISO_3166-1&action=edit&section=7
057                InputStream is = ISO3166_1AlphaCountryCodeMapper.class.getClassLoader().getResourceAsStream(RESOURCE_FILE_NAME);
058                try {
059                        MAP_2_3.load(is);
060                } catch (IOException e) {
061                        // Ignore
062                }
063        }
064        
065        
066        private static void lazyLoadMap_3_2() {
067                
068                if (! MAP_3_2.isEmpty()) {
069                        return;
070                }
071                
072                if (MAP_2_3.isEmpty()) {
073                        lazyLoadMap_2_3();
074                }
075                
076                for (String code2: MAP_2_3.stringPropertyNames()) {
077                        String code3 = MAP_2_3.getProperty(code2);
078                        MAP_3_2.put(code3, code2);
079                }
080        }
081        
082        
083        /**
084         * Maps the specified ISO 3166-1 alpha-2 (two letter) country code to
085         * its matching alpha-3 code, based on the {@link #RESOURCE_FILE_NAME}
086         * resource.
087         *
088         * @param alpha2Code The ISO 3166-1 alpha-2 country code. Must not be
089         *                   {@code null}.
090         *
091         * @return The matching alpha-3 code, {@code null} if no mapping is
092         *         present.
093         */
094        public static ISO3166_1Alpha3CountryCode toAlpha3CountryCode(final ISO3166_1Alpha2CountryCode alpha2Code) {
095                
096                lazyLoadMap_2_3();
097                String alpha3Code = MAP_2_3.getProperty(alpha2Code.getValue());
098                return alpha3Code != null ? new ISO3166_1Alpha3CountryCode(alpha3Code) : null;
099        }
100        
101        
102        /**
103         * Maps the specified ISO 3166-1 alpha-3 (three letter) country code to
104         * its matching alpha-2 code, based on the {@link #RESOURCE_FILE_NAME}
105         * resource.
106         *
107         * @param alpha3Code The ISO 3166-1 alpha-3 country code. Must not be
108         *                   {@code null}.
109         *
110         * @return The matching alpha-2 code, {@code null} if no mapping is
111         *         present.
112         */
113        public static ISO3166_1Alpha2CountryCode toAlpha2CountryCode(final ISO3166_1Alpha3CountryCode alpha3Code) {
114                
115                lazyLoadMap_3_2();
116                String alpha2Code = MAP_3_2.getProperty(alpha3Code.getValue());
117                return alpha2Code != null ? new ISO3166_1Alpha2CountryCode(alpha2Code) : null;
118        }
119}