Repository: GoogleContainerTools/jib
File: jib-cli/src/main/java/com/google/cloud/tools/jib/cli/cli2/CacheDirectories.java (Java)

Code before:

package com.google.cloud.tools.jib.cli.cli2;

import com.google.cloud.tools.jib.filesystem.XdgDirectories;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Charsets;
import java.io.IOException;
import java.nio.file.Path;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Optional;
import javax.annotation.Nullable;

/** A class to determine cache locations for any cli commands. */
public class CacheDirectories {

  @Nullable private final Path baseImageCache;
  private final Path projectCache;

  /**
   * Create a caches helper for cli cache locations.
   *
   * @param commonCliOptions cli options
   * @param contextRoot the context root, if a single file, use the parent directory, this context
   *     root must exist
   * @return an instance of Caches with cli specific cache locations
   */
  public static CacheDirectories from(CommonCliOptions commonCliOptions, Path contextRoot) {
    return new CacheDirectories(
        commonCliOptions.getBaseImageCache().orElse(null),
        commonCliOptions
            .getProjectCache()
            .orElse(
                XdgDirectories.getCacheHome()
                    .resolve("cli")
                    .resolve("projects")
                    .resolve(getProjectCacheDirectoryFromProject(contextRoot))));
  }

  @VisibleForTesting
  static String getProjectCacheDirectoryFromProject(Path path) {
    try {
      byte[] hashedBytes =
          MessageDigest.getInstance("SHA-256")
              .digest(path.toFile().getCanonicalPath().getBytes(Charsets.UTF_8));
      StringBuilder stringBuilder = new StringBuilder(2 * hashedBytes.length);
      for (byte b : hashedBytes) {
        stringBuilder.append(String.format("%02x", b));
      }
      return stringBuilder.toString();
    } catch (IOException | SecurityException ex) {

Review comment (performance):
```suggestion
   * @return an instance of CacheDirectories with cli specific cache locations
```

Code after:

package com.google.cloud.tools.jib.cli.cli2;

import com.google.cloud.tools.jib.filesystem.XdgDirectories;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Charsets;
import java.io.IOException;
import java.nio.file.Path;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Optional;
import javax.annotation.Nullable;

/** A class to determine cache locations for any cli commands. */
public class CacheDirectories {

  @Nullable private final Path baseImageCache;
  private final Path projectCache;

  /**
   * Create a caches helper for cli cache locations.
   *
   * @param commonCliOptions cli options
   * @param contextRoot the context root, if a single file, use the parent directory, this context
   *     root must exist
   * @return an instance of CacheDirectories with cli specific cache locations
   */
  public static CacheDirectories from(CommonCliOptions commonCliOptions, Path contextRoot) {
    return new CacheDirectories(
        commonCliOptions.getBaseImageCache().orElse(null),
        commonCliOptions
            .getProjectCache()
            .orElse(
                XdgDirectories.getCacheHome()
                    .resolve("cli")
                    .resolve("projects")
                    .resolve(getProjectCacheDirectoryFromProject(contextRoot))));
  }

  @VisibleForTesting
  static String getProjectCacheDirectoryFromProject(Path path) {
    try {
      byte[] hashedBytes =
          MessageDigest.getInstance("SHA-256")
              .digest(path.toFile().getCanonicalPath().getBytes(Charsets.UTF_8));
      StringBuilder stringBuilder = new StringBuilder(2 * hashedBytes.length);
      for (byte b : hashedBytes) {
        stringBuilder.append(String.format("%02x", b));
      }
      return stringBuilder.toString();
    } catch (IOException | SecurityException ex) {