public static ImageReference parse(String reference) throws InvalidImageReferenceException {
    if (reference.equals(SCRATCH)) {
      return ImageReference.scratch();
    }

    Matcher matcher = REFERENCE_PATTERN.matcher(reference);

    if (!matcher.find() || matcher.groupCount() < 4) {
      throw new InvalidImageReferenceException(reference);
    }

    String registry = matcher.group(1);
    String repository = matcher.group(2);
    String tag = matcher.group(3);
    String digest = matcher.group(4);

    // If no registry was matched, use Docker Hub by default.
    if (Strings.isNullOrEmpty(registry)) {
      registry = DOCKER_HUB_REGISTRY;
    }

    if (Strings.isNullOrEmpty(repository)) {
      throw new InvalidImageReferenceException(reference);
    }
    /*
     * If a registry was matched but it does not contain any dots or colons, it should actually be
     * part of the repository unless it is "localhost".
     *
     * See https://github.com/docker/distribution/blob/245ca4659e09e9745f3cc1217bf56e946509220c/reference/normalize.go#L62
     */
    if (!registry.contains(".") && !registry.contains(":") && !"localhost".equals(registry)) {
      repository = registry + "/" + repository;
      registry = DOCKER_HUB_REGISTRY;
    }

    /*
     * For Docker Hub, if the repository is only one component, then it should be prefixed with
     * 'library/'.
     *
     * See https://docs.docker.com/engine/reference/commandline/pull/#pull-an-image-from-docker-hub
     */
    if (DOCKER_HUB_REGISTRY.equals(registry) && repository.indexOf('/') < 0) {
      repository = LIBRARY_REPOSITORY_PREFIX + repository;
    }

    if (Strings.isNullOrEmpty(tag) && Strings.isNullOrEmpty(digest)) {
      tag = DEFAULT_TAG;
    }
    if (Strings.isNullOrEmpty(tag)) {
      tag = null;
    }
    if (Strings.isNullOrEmpty(digest)) {
      digest = null;
    }

    return new ImageReference(registry, repository, tag, digest);
  }
Parses a string {@code reference} into an {@link ImageReference}.

<p>Image references should generally be in the form: {@code <registry>/<repository>:<tag>} For
example, an image reference could be {@code gcr.io/k8s-skaffold/skaffold:v1.20.0}.

<p>See <a
href="https://docs.docker.com/engine/reference/commandline/tag/#extended-description">https://docs.docker.com/engine/reference/commandline/tag/#extended-description</a>
for a description of valid image reference format. Note, however, that the image reference is
referred confusingly as {@code tag} on that page.

@param reference the string to parse
@return an {@link ImageReference} parsed from the string
@throws InvalidImageReferenceException if {@code reference} is formatted incorrectly
jib-core/src/main/java/com/google/cloud/tools/jib/api/ImageReference.java