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

io.citrine.jpif.util.Orcid Maven / Gradle / Ivy

Go to download

This package includes java objects for all items in the Physical Information File (PIF), http://www.citrine.io/pif. Files formatted in the PIF schema can be serialized and deserialized using included methods.

There is a newer version: 2.7.2
Show newest version
package io.citrine.jpif.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Class used to work with ORCID identifiers.
 *
 * @author Kyle Michel
 */
public class Orcid {

    /**
     * Get the ID string.
     *
     * @return String with the ID for this object.
     */
    public String getId() {
        return this.id;
    }

    /**
     * Generate a {@link Orcid} object from an input string. This will use regular expression to try to determine
     * the format of the input string and extract the id.
     *
     * @param input String to convert into and ORCID id.
     * @return New {@link Orcid} object with the value extracted from the input string.
     * @throws IllegalArgumentException if the input string is not a valid ORCID string.
     */
    public static Orcid valueOf(final String input) {
        final Matcher matcher = ORCID_PATTERN.matcher(input);
        if (matcher.matches()) {
            return new Orcid(matcher.group(0) + "-" + matcher.group(1) + "-"
                    + matcher.group(2) + "-" + matcher.group(3));
        }
        else {
            throw new IllegalArgumentException("Input string is not a valid ORCID: " + input);
        }
    }

    /**
     * Determine whether an input string is a valid ORCID identifier string.
     *
     * @param input String to check as an ORCID identifier.
     * @return True if the input string is a valid ORCID identifier.
     */
    public static boolean isValid(final String input) {
        try {
            valueOf(input);
            return true;
        }
        catch (IllegalArgumentException e) {
            return false;
        }
    }

    /**
     * Constructor.
     *
     * @param id String with the formatted ORCID id.
     */
    private Orcid(final String id) {
        this.id = id;
    }

    /** String with the value of the ORCID id. */
    private final String id;

    /** Regular expression to check whether an input string is a valid ORCID id. */
    private static final String ORCID_REGEX =
            "^\\s*(?:(?:https?://)?orcid.org/)?([0-9]{4})\\-?([0-9]{4})\\-?([0-9]{4})\\-?([0-9]{4})\\s*$";

    /** Regular expression to check whether an input string is a valid ORCID id. */
    private static final Pattern ORCID_PATTERN = Pattern.compile(ORCID_REGEX);
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy