001/*
002 * oauth2-oidc-sdk
003 *
004 * Copyright 2012-2016, 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 com.nimbusds.oauth2.sdk.ParseException;
022import com.nimbusds.oauth2.sdk.id.Identifier;
023
024
025/**
026 * Abstract class for country codes.
027 */
028public abstract class CountryCode extends Identifier {
029        
030        
031        private static final long serialVersionUID = -6171424661935191539L;
032        
033        
034        /**
035         * Creates a new country code.
036         *
037         * @param value The country code value.
038         */
039        protected CountryCode(final String value) {
040                super(value);
041        }
042        
043        
044        /**
045         * Returns the length of this country code.
046         *
047         * @return The length.
048         */
049        public int length() {
050                
051                return getValue().length();
052        }
053        
054        
055        /**
056         * Casts this code to an ISO 3166-1 alpha-2 (two-letter) country code.
057         *
058         * @return The ISO 3166-1 alpha-2 (two-letter) country code.
059         */
060        public ISO3166_1Alpha2CountryCode toISO3166_1Alpha2CountryCode() {
061                
062                return (ISO3166_1Alpha2CountryCode)this;
063        }
064        
065        
066        /**
067         * Casts this code to an ISO 3166-1 alpha-3 (three-letter) country
068         * code.
069         *
070         * @return The ISO 3166-1 alpha-3 (three-letter) country code.
071         */
072        public ISO3166_1Alpha3CountryCode toISO3166_1Alpha3CountryCode() {
073                
074                return (ISO3166_1Alpha3CountryCode)this;
075        }
076        
077        
078        /**
079         * Casts this code to an ISO 3166-3 country code.
080         *
081         * @return The ISO 3166-3 country code.
082         */
083        public ISO3166_3CountryCode toISO3166_3CountryCode() {
084                
085                return (ISO3166_3CountryCode)this;
086        }
087        
088        
089        /**
090         * Parses a country code.
091         *
092         * @param s The string to parse. Must not be {@code null}.
093         *
094         * @return The country code.
095         *
096         * @throws ParseException If parsing failed.
097         */
098        public static CountryCode parse(final String s)
099                throws ParseException {
100                
101                if (3 == s.length()) {
102                        return ISO3166_1Alpha3CountryCode.parse(s);
103                } else if (2 == s.length()) {
104                        return ISO3166_1Alpha2CountryCode.parse(s);
105                } else if (4 == s.length()) {
106                        return ISO3166_3CountryCode.parse(s);
107                } else {
108                        throw new ParseException("The country code must be 3, 2 or 4 letters");
109                }
110        }
111        
112        
113        @Override
114        public abstract boolean equals(final Object other);
115}