All Downloads are FREE. Search and download functionalities are using the official Maven repository.

dev.orne.beans.StringIdentity Maven / Gradle / Ivy

package dev.orne.beans;

/*-
 * #%L
 * Orne Beans
 * %%
 * Copyright (C) 2020 Orne Developments
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Lesser Public License for more details.
 * 
 * You should have received a copy of the GNU General Lesser Public
 * License along with this program.  If not, see
 * .
 * #L%
 */

import javax.validation.constraints.NotNull;

/**
 * Implementation for {@code Identity} for identities composed
 * of a single inner {@code String} value.
 * 
 * @author (w) Iker Hernaez
 * @version 1.0, 2020-05
 * @since 0.1
 */
public class StringIdentity
extends AbstractSimpleIdentity {

    /** The serial version UID. */
    private static final long serialVersionUID = 5660267975323191055L;

    /**
     * Creates a new instance.
     * 
     * @param value The identity value
     */
    public StringIdentity(
            final String value) {
        super(value);
    }

    /**
     * Copy constructor.
     * 
     * @param copy The instance to copy
     */
    public StringIdentity(
            final @NotNull StringIdentity copy) {
        super(copy);
    }

    /**
     * Resolves the specified identity token to a valid {@code StringIdentity}.
     * 
     * @param token The identity token
     * @return The resolved identity token
     * @throws NullPointerException If the identity token is {@code null}
     * @throws UnrecognizedIdentityTokenException If the identity token is not
     * a valid identity token or it doesn't start with the expected prefix
     */
    @IdentityTokenResolver
    public static @NotNull StringIdentity fromIdentityToken(
            final @NotNull String token) {
        return new StringIdentity(extractTokenValue(
                IdentityTokenFormatter.DEFAULT_PREFIX,
                token));
    }

    /**
     * Extracts the {@code String} value of a token generated by
     * {@code StringIdentity}.
     * 

* Note that the resulting value can be {@code null}. If a non-null * value is expected use{@code extractRequiredTokenValue(String, String)}. * * @param prefix The expected identity token prefix. * @param token The identity token. * @return The extracted {@code String} value. * @throws NullPointerException If the identity token is {@code null} * @throws UnrecognizedIdentityTokenException If the identity token is not * a valid simple identity token or if it doesn't start with the expected * prefix. * @see #extractRequiredTokenValue(String, String) */ public static String extractTokenValue( final @NotNull String prefix, final @NotNull String token) { return IdentityTokenFormatter.parse(prefix, token); } /** * Extracts the {@code String} value of a token generated by * {@code StringIdentity}. *

* If the resulting value is {@code null} an exception is thrown. * * @param prefix The expected identity token prefix. * @param token The identity token. * @return The extracted {@code String} value. * @throws NullPointerException If the identity token is {@code null} * @throws UnrecognizedIdentityTokenException If the identity token is not * a valid simple identity token, if it doesn't start with the expected * prefix or if the extracted value is null. * @see #extractTokenValue(String, String) */ public static @NotNull String extractRequiredTokenValue( final @NotNull String prefix, final @NotNull String token) { final String result = extractTokenValue(prefix, token); if (result == null) { throw new UnrecognizedIdentityTokenException( "Unrecognized identity token: " + token); } return result; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy