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 net.jcip.annotations.Immutable;
022
023import com.nimbusds.oauth2.sdk.ParseException;
024
025
026/**
027 * ISO 3166-1 alpha-2 (two-letter) country code.
028 */
029@Immutable
030public final class ISO3166_1Alpha2CountryCode extends CountryCode {
031        
032        
033        /**
034         * Creates a new ISO 3166-1 alpha-2 country code. Normalises the code
035         * to upper case.
036         *
037         * @param value The country code value, must be two-letter.
038         */
039        public ISO3166_1Alpha2CountryCode(final String value) {
040                super(value.toUpperCase());
041                if (value.length() != 2) {
042                        throw new IllegalArgumentException("The ISO 3166-1 alpha-2 country code must be two letters");
043                }
044        }
045        
046        
047        @Override
048        public boolean equals(final Object object) {
049                
050                return object instanceof ISO3166_1Alpha2CountryCode &&
051                        this.toString().equals(object.toString());
052        }
053        
054        
055        /**
056         * Parses an ISO 3166-1 alpha-2 (two-letter) country code.
057         *
058         * @param s The string to parse. Must not be {@code null}.
059         *
060         * @return The ISO 3166-1 alpha-2 (two-letter) country code.
061         *
062         * @throws ParseException If parsing failed.
063         */
064        public static ISO3166_1Alpha2CountryCode parse(final String s)
065                throws ParseException {
066                
067                try {
068                        return new ISO3166_1Alpha2CountryCode(s);
069                } catch (IllegalArgumentException e) {
070                        throw new ParseException(e.getMessage());
071                }
072        }
073}