class FhirUriUtils {

    @Nonnull
    public static IIdType createIdFromUri( @Nonnull String uri, @Nullable FhirResourceType fhirResourceType ) throws IllegalArgumentException
    {
        final Matcher matcher = CANONICAL_PATTERN.matcher( uri );

        if ( !matcher.matches() )
        {
            throw new IllegalArgumentException( "URI is invalid: " + uri );
        }

        String resourceTypeName = matcher.group( 1 );
        final String id = matcher.group( 2 );

        if ( resourceTypeName == null && fhirResourceType != null )
        {
            resourceTypeName = fhirResourceType.getResourceTypeName();
        }

        if ( resourceTypeName == null )
        {
            throw new IllegalArgumentException( "URI must include a valid FHIR resource type: " + uri );
        }

        if ( fhirResourceType != null && !fhirResourceType.getResourceTypeName().equals( resourceTypeName ) )
        {
            throw new IllegalArgumentException( "Resource type in URI differs from expected resource type " + fhirResourceType + ": " + uri );
        }

        return new IdDt( resourceTypeName, id );
    }

    private  FhirUriUtils();

}