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.oauth2.sdk.id;
019
020
021import java.net.URI;
022import java.net.URISyntaxException;
023
024/**
025 * The base class for representing identifiers with an optional URI
026 * representation.
027 *
028 * <p>Extending classes must override the {@link #equals} method.
029 */
030public class IdentifierWithOptionalURIRepresentation extends Identifier {
031
032
033        private static final long serialVersionUID = 1003164205665683809L;
034
035
036        /**
037         * Creates a new identifier with the specified URI.
038         *
039         * @param uri The URI. Must not be {@code null}.
040         */
041        public IdentifierWithOptionalURIRepresentation(final URI uri) {
042                super(uri.toString());
043        }
044
045
046        /**
047         * Creates a new identifier with the specified value.
048         *
049         * @param value The value. Must not be {@code null} or empty string.
050         */
051        public IdentifierWithOptionalURIRepresentation(final String value) {
052                super(value);
053        }
054
055
056        /**
057         * Returns the URI representation.
058         *
059         * @return The URI, {@code null} if the identifier value cannot be
060         *         parsed to a URI.
061         */
062        public URI getURI() {
063                try {
064                        return new URI(getValue());
065                } catch (URISyntaxException e) {
066                        return null;
067                }
068        }
069}