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 net.jcip.annotations.Immutable;
022
023import com.nimbusds.oauth2.sdk.ParseException;
024import com.nimbusds.oauth2.sdk.id.Identifier;
025import com.nimbusds.oauth2.sdk.util.StringUtils;
026
027
028/**
029 * Mobile subscriber ISDN number.
030 *
031 * <p>Example where 91 is the country code, 8369 is the national destination
032 * code and 110173 is the subscriber number:
033 *
034 * <pre>
035 * 919825098250
036 * </pre>
037 *
038 * <p>Related specifications:
039 *
040 * <ul>
041 *     <li>OpenID Connect for Identity Assurance 1.0, section 4.1.
042 *     <li>ITU-T E.164
043 * </ul>
044 */
045@Immutable
046public final class MSISDN extends Identifier {
047        
048        
049        private static final long serialVersionUID = 6919844477369481587L;
050        
051        
052        /**
053         * The maximum length of an MSISDN.
054         */
055        public static final int MAX_LENGTH = 15;
056        
057        
058        /**
059         * Creates a new mobile subscriber ISDN number with the specified
060         * value.
061         *
062         * @param value The MSISDN value. Must not be {@code null}.
063         */
064        public MSISDN(final String value) {
065                super(value);
066                if (! StringUtils.isNumeric(value)) {
067                        throw new IllegalArgumentException("The MSISDN must be a numeric string");
068                }
069                if (value.length() > MAX_LENGTH) {
070                        throw new IllegalArgumentException("The MSISDN must not contain more than " + MAX_LENGTH + " digits");
071                }
072        }
073        
074        
075        @Override
076        public boolean equals(final Object object) {
077                
078                return object instanceof MSISDN &&
079                        this.toString().equals(object.toString());
080        }
081        
082        
083        /**
084         * Parses an mobile subscriber ISDN number.
085         *
086         * @param s The string to parse. Must not be {@code null}.
087         *
088         * @return The mobile subscriber ISDN number.
089         *
090         * @throws ParseException If parsing failed.
091         */
092        public static MSISDN parse(final String s)
093                throws ParseException {
094                
095                try {
096                        return new MSISDN(s);
097                } catch (IllegalArgumentException e) {
098                        throw new ParseException(e.getMessage());
099                }
100        }
101}