package zeenea.connector.zdf;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import javax.annotation.concurrent.Immutable;
import javax.annotation.concurrent.NotThreadSafe;
import org.immutables.value.Generated;

/**
 * Immutable implementation of {@link FileRef}.
 * <p>
 * Use the builder to create immutable instances:
 * {@code ImmutableFileRef.builder()}.
 */
@Generated(from = "FileRef", generator = "Immutables")
@SuppressWarnings({"all"})
@SuppressFBWarnings
@ParametersAreNonnullByDefault
@javax.annotation.processing.Generated("org.immutables.processor.ProxyProcessor")
@Immutable
public final class ImmutableFileRef implements FileRef {
  private final Path path;
  private final Path workDir;

  private ImmutableFileRef(Path path, Path workDir) {
    this.path = path;
    this.workDir = workDir;
  }

  /**
   * @return The value of the {@code path} attribute
   */
  @Override
  public Path path() {
    return path;
  }

  /**
   * @return The value of the {@code workDir} attribute
   */
  @Override
  public Path workDir() {
    return workDir;
  }

  /**
   * Copy the current immutable object by setting a value for the {@link FileRef#path() path} attribute.
   * A shallow reference equality check is used to prevent copying of the same value by returning {@code this}.
   * @param value A new value for path
   * @return A modified copy of the {@code this} object
   */
  public final ImmutableFileRef withPath(Path value) {
    if (this.path == value) return this;
    Path newValue = Objects.requireNonNull(value, "path");
    return new ImmutableFileRef(newValue, this.workDir);
  }

  /**
   * Copy the current immutable object by setting a value for the {@link FileRef#workDir() workDir} attribute.
   * A shallow reference equality check is used to prevent copying of the same value by returning {@code this}.
   * @param value A new value for workDir
   * @return A modified copy of the {@code this} object
   */
  public final ImmutableFileRef withWorkDir(Path value) {
    if (this.workDir == value) return this;
    Path newValue = Objects.requireNonNull(value, "workDir");
    return new ImmutableFileRef(this.path, newValue);
  }

  /**
   * This instance is equal to all instances of {@code ImmutableFileRef} that have equal attribute values.
   * @return {@code true} if {@code this} is equal to {@code another} instance
   */
  @Override
  public boolean equals(@Nullable Object another) {
    if (this == another) return true;
    return another instanceof ImmutableFileRef
        && equalTo(0, (ImmutableFileRef) another);
  }

  private boolean equalTo(int synthetic, ImmutableFileRef another) {
    return path.equals(another.path)
        && workDir.equals(another.workDir);
  }

  /**
   * Computes a hash code from attributes: {@code path}, {@code workDir}.
   * @return hashCode value
   */
  @Override
  public int hashCode() {
    int h = 5381;
    h += (h << 5) + path.hashCode();
    h += (h << 5) + workDir.hashCode();
    return h;
  }

  /**
   * Prints the immutable value {@code FileRef} with attribute values.
   * @return A string representation of the value
   */
  @Override
  public String toString() {
    return "FileRef{"
        + "path=" + path
        + ", workDir=" + workDir
        + "}";
  }

  /**
   * Creates an immutable copy of a {@link FileRef} value.
   * Uses accessors to get values to initialize the new immutable instance.
   * If an instance is already immutable, it is returned as is.
   * @param instance The instance to copy
   * @return A copied immutable FileRef instance
   */
  public static ImmutableFileRef copyOf(FileRef instance) {
    if (instance instanceof ImmutableFileRef) {
      return (ImmutableFileRef) instance;
    }
    return ImmutableFileRef.builder()
        .from(instance)
        .build();
  }

  /**
   * Creates a builder for {@link ImmutableFileRef ImmutableFileRef}.
   * <pre>
   * ImmutableFileRef.builder()
   *    .path(java.nio.file.Path) // required {@link FileRef#path() path}
   *    .workDir(java.nio.file.Path) // required {@link FileRef#workDir() workDir}
   *    .build();
   * </pre>
   * @return A new ImmutableFileRef builder
   */
  public static ImmutableFileRef.Builder builder() {
    return new ImmutableFileRef.Builder();
  }

  /**
   * Builds instances of type {@link ImmutableFileRef ImmutableFileRef}.
   * Initialize attributes and then invoke the {@link #build()} method to create an
   * immutable instance.
   * <p><em>{@code Builder} is not thread-safe and generally should not be stored in a field or collection,
   * but instead used immediately to create instances.</em>
   */
  @Generated(from = "FileRef", generator = "Immutables")
  @NotThreadSafe
  public static final class Builder {
    private static final long INIT_BIT_PATH = 0x1L;
    private static final long INIT_BIT_WORK_DIR = 0x2L;
    private long initBits = 0x3L;

    private @Nullable Path path;
    private @Nullable Path workDir;

    private Builder() {
    }

    /**
     * Fill a builder with attribute values from the provided {@code FileRef} instance.
     * Regular attribute values will be replaced with those from the given instance.
     * Absent optional values will not replace present values.
     * @param instance The instance from which to copy values
     * @return {@code this} builder for use in a chained invocation
     */
    public final Builder from(FileRef instance) {
      Objects.requireNonNull(instance, "instance");
      this.path(instance.path());
      this.workDir(instance.workDir());
      return this;
    }

    /**
     * Initializes the value for the {@link FileRef#path() path} attribute.
     * @param path The value for path 
     * @return {@code this} builder for use in a chained invocation
     */
    public final Builder path(Path path) {
      this.path = Objects.requireNonNull(path, "path");
      initBits &= ~INIT_BIT_PATH;
      return this;
    }

    /**
     * Initializes the value for the {@link FileRef#workDir() workDir} attribute.
     * @param workDir The value for workDir 
     * @return {@code this} builder for use in a chained invocation
     */
    public final Builder workDir(Path workDir) {
      this.workDir = Objects.requireNonNull(workDir, "workDir");
      initBits &= ~INIT_BIT_WORK_DIR;
      return this;
    }

    /**
     * Builds a new {@link ImmutableFileRef ImmutableFileRef}.
     * @return An immutable instance of FileRef
     * @throws java.lang.IllegalStateException if any required attributes are missing
     */
    public ImmutableFileRef build() {
      if (initBits != 0) {
        throw new IllegalStateException(formatRequiredAttributesMessage());
      }
      return new ImmutableFileRef(path, workDir);
    }

    private String formatRequiredAttributesMessage() {
      List<String> attributes = new ArrayList<>();
      if ((initBits & INIT_BIT_PATH) != 0) attributes.add("path");
      if ((initBits & INIT_BIT_WORK_DIR) != 0) attributes.add("workDir");
      return "Cannot build FileRef, some of required attributes are not set " + attributes;
    }
  }
}
