package zeenea.connector.zdf;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
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 FileItem}.
 * <p>
 * Use the builder to create immutable instances:
 * {@code ImmutableFileItem.builder()}.
 */
@Generated(from = "FileItem", generator = "Immutables")
@SuppressWarnings({"all"})
@SuppressFBWarnings
@ParametersAreNonnullByDefault
@javax.annotation.processing.Generated("org.immutables.processor.ProxyProcessor")
@Immutable
public final class ImmutableFileItem<T> implements FileItem<T> {
  private final T item;
  private final FileRef fileRef;

  private ImmutableFileItem(T item, FileRef fileRef) {
    this.item = item;
    this.fileRef = fileRef;
  }

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

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

  /**
   * Copy the current immutable object by setting a value for the {@link FileItem#item() item} 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 item
   * @return A modified copy of the {@code this} object
   */
  public final ImmutableFileItem<T> withItem(T value) {
    if (this.item == value) return this;
    T newValue = Objects.requireNonNull(value, "item");
    return new ImmutableFileItem<>(newValue, this.fileRef);
  }

  /**
   * Copy the current immutable object by setting a value for the {@link FileItem#fileRef() fileRef} 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 fileRef
   * @return A modified copy of the {@code this} object
   */
  public final ImmutableFileItem<T> withFileRef(FileRef value) {
    if (this.fileRef == value) return this;
    FileRef newValue = Objects.requireNonNull(value, "fileRef");
    return new ImmutableFileItem<>(this.item, newValue);
  }

  /**
   * This instance is equal to all instances of {@code ImmutableFileItem} 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 ImmutableFileItem<?>
        && equalTo(0, (ImmutableFileItem<?>) another);
  }

  private boolean equalTo(int synthetic, ImmutableFileItem<?> another) {
    return item.equals(another.item)
        && fileRef.equals(another.fileRef);
  }

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

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

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

  /**
   * Creates a builder for {@link ImmutableFileItem ImmutableFileItem}.
   * <pre>
   * ImmutableFileItem.&amp;lt;T&amp;gt;builder()
   *    .item(T) // required {@link FileItem#item() item}
   *    .fileRef(zeenea.connector.zdf.FileRef) // required {@link FileItem#fileRef() fileRef}
   *    .build();
   * </pre>
   * @param <T> generic parameter T
   * @return A new ImmutableFileItem builder
   */
  public static <T> ImmutableFileItem.Builder<T> builder() {
    return new ImmutableFileItem.Builder<>();
  }

  /**
   * Builds instances of type {@link ImmutableFileItem ImmutableFileItem}.
   * 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 = "FileItem", generator = "Immutables")
  @NotThreadSafe
  public static final class Builder<T> {
    private static final long INIT_BIT_ITEM = 0x1L;
    private static final long INIT_BIT_FILE_REF = 0x2L;
    private long initBits = 0x3L;

    private @Nullable T item;
    private @Nullable FileRef fileRef;

    private Builder() {
    }

    /**
     * Fill a builder with attribute values from the provided {@code FileItem} 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<T> from(FileItem<T> instance) {
      Objects.requireNonNull(instance, "instance");
      this.item(instance.item());
      this.fileRef(instance.fileRef());
      return this;
    }

    /**
     * Initializes the value for the {@link FileItem#item() item} attribute.
     * @param item The value for item 
     * @return {@code this} builder for use in a chained invocation
     */
    public final Builder<T> item(T item) {
      this.item = Objects.requireNonNull(item, "item");
      initBits &= ~INIT_BIT_ITEM;
      return this;
    }

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

    /**
     * Builds a new {@link ImmutableFileItem ImmutableFileItem}.
     * @return An immutable instance of FileItem
     * @throws java.lang.IllegalStateException if any required attributes are missing
     */
    public ImmutableFileItem<T> build() {
      if (initBits != 0) {
        throw new IllegalStateException(formatRequiredAttributesMessage());
      }
      return new ImmutableFileItem<>(item, fileRef);
    }

    private String formatRequiredAttributesMessage() {
      List<String> attributes = new ArrayList<>();
      if ((initBits & INIT_BIT_ITEM) != 0) attributes.add("item");
      if ((initBits & INIT_BIT_FILE_REF) != 0) attributes.add("fileRef");
      return "Cannot build FileItem, some of required attributes are not set " + attributes;
    }
  }
}
