package zeenea.connector.zdf.dataset;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import javax.annotation.concurrent.Immutable;
import javax.annotation.concurrent.NotThreadSafe;
import org.immutables.value.Generated;
import zeenea.connector.zdf.model.ZdfDataset;
import zeenea.connector.zdf.model.ZdfProcess;

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

  private ImmutableDatasetRoot(ImmutableDatasetRoot.Builder builder) {
    if (builder.datasetsIsSet()) {
      initShim.datasets(List.copyOf(builder.datasets));
    }
    if (builder.lineageIsSet()) {
      initShim.lineage(List.copyOf(builder.lineage));
    }
    this.datasets = initShim.datasets();
    this.lineage = initShim.lineage();
    this.initShim = null;
  }

  private ImmutableDatasetRoot(
      List<ZdfDataset> datasets,
      List<ZdfProcess> lineage) {
    this.datasets = datasets;
    this.lineage = lineage;
    this.initShim = null;
  }

  private static final byte STAGE_INITIALIZING = -1;
  private static final byte STAGE_UNINITIALIZED = 0;
  private static final byte STAGE_INITIALIZED = 1;
  private transient volatile InitShim initShim = new InitShim();

  @Generated(from = "DatasetRoot", generator = "Immutables")
  private final class InitShim {
    private byte datasetsBuildStage = STAGE_UNINITIALIZED;
    private List<ZdfDataset> datasets;

    List<ZdfDataset> datasets() {
      if (datasetsBuildStage == STAGE_INITIALIZING) throw new IllegalStateException(formatInitCycleMessage());
      if (datasetsBuildStage == STAGE_UNINITIALIZED) {
        datasetsBuildStage = STAGE_INITIALIZING;
        this.datasets = datasetsInitialize() instanceof Collection<?>
       ? List.copyOf((Collection<? extends ZdfDataset>) datasetsInitialize())
       : StreamSupport.stream(datasetsInitialize().spliterator(), false)
             .collect(Collectors.toUnmodifiableList());
        datasetsBuildStage = STAGE_INITIALIZED;
      }
      return this.datasets;
    }

    void datasets(List<ZdfDataset> datasets) {
      this.datasets = datasets;
      datasetsBuildStage = STAGE_INITIALIZED;
    }

    private byte lineageBuildStage = STAGE_UNINITIALIZED;
    private List<ZdfProcess> lineage;

    List<ZdfProcess> lineage() {
      if (lineageBuildStage == STAGE_INITIALIZING) throw new IllegalStateException(formatInitCycleMessage());
      if (lineageBuildStage == STAGE_UNINITIALIZED) {
        lineageBuildStage = STAGE_INITIALIZING;
        this.lineage = lineageInitialize() instanceof Collection<?>
       ? List.copyOf((Collection<? extends ZdfProcess>) lineageInitialize())
       : StreamSupport.stream(lineageInitialize().spliterator(), false)
             .collect(Collectors.toUnmodifiableList());
        lineageBuildStage = STAGE_INITIALIZED;
      }
      return this.lineage;
    }

    void lineage(List<ZdfProcess> lineage) {
      this.lineage = lineage;
      lineageBuildStage = STAGE_INITIALIZED;
    }

    private String formatInitCycleMessage() {
      List<String> attributes = new ArrayList<>();
      if (datasetsBuildStage == STAGE_INITIALIZING) attributes.add("datasets");
      if (lineageBuildStage == STAGE_INITIALIZING) attributes.add("lineage");
      return "Cannot build DatasetRoot, attribute initializers form cycle " + attributes;
    }
  }

  private List<ZdfDataset> datasetsInitialize() {
    return DatasetRoot.super.datasets();
  }

  private List<ZdfProcess> lineageInitialize() {
    return DatasetRoot.super.lineage();
  }

  /**
   * @return The value of the {@code datasets} attribute
   */
  @JsonProperty("datasets")
  @Override
  public List<ZdfDataset> datasets() {
    InitShim shim = this.initShim;
    return shim != null
        ? shim.datasets()
        : this.datasets;
  }

  /**
   * @return The value of the {@code lineage} attribute
   */
  @JsonProperty("lineage")
  @Override
  public List<ZdfProcess> lineage() {
    InitShim shim = this.initShim;
    return shim != null
        ? shim.lineage()
        : this.lineage;
  }

  /**
   * Copy the current immutable object with elements that replace the content of {@link DatasetRoot#datasets() datasets}.
   * @param elements The elements to set
   * @return A modified copy of {@code this} object
   */
  public final ImmutableDatasetRoot withDatasets(ZdfDataset... elements) {
    List<ZdfDataset> newValue = List.of(elements);
    return new ImmutableDatasetRoot(newValue, this.lineage);
  }

  /**
   * Copy the current immutable object with elements that replace the content of {@link DatasetRoot#datasets() datasets}.
   * A shallow reference equality check is used to prevent copying of the same value by returning {@code this}.
   * @param elements An iterable of datasets elements to set
   * @return A modified copy of {@code this} object
   */
  public final ImmutableDatasetRoot withDatasets(Iterable<? extends ZdfDataset> elements) {
    if (this.datasets == elements) return this;
    List<ZdfDataset> newValue = elements instanceof Collection<?>
        ? List.copyOf((Collection<? extends ZdfDataset>) elements)
        : StreamSupport.stream(elements.spliterator(), false)
            .collect(Collectors.toUnmodifiableList());
    return new ImmutableDatasetRoot(newValue, this.lineage);
  }

  /**
   * Copy the current immutable object with elements that replace the content of {@link DatasetRoot#lineage() lineage}.
   * @param elements The elements to set
   * @return A modified copy of {@code this} object
   */
  public final ImmutableDatasetRoot withLineage(ZdfProcess... elements) {
    List<ZdfProcess> newValue = List.of(elements);
    return new ImmutableDatasetRoot(this.datasets, newValue);
  }

  /**
   * Copy the current immutable object with elements that replace the content of {@link DatasetRoot#lineage() lineage}.
   * A shallow reference equality check is used to prevent copying of the same value by returning {@code this}.
   * @param elements An iterable of lineage elements to set
   * @return A modified copy of {@code this} object
   */
  public final ImmutableDatasetRoot withLineage(Iterable<? extends ZdfProcess> elements) {
    if (this.lineage == elements) return this;
    List<ZdfProcess> newValue = elements instanceof Collection<?>
        ? List.copyOf((Collection<? extends ZdfProcess>) elements)
        : StreamSupport.stream(elements.spliterator(), false)
            .collect(Collectors.toUnmodifiableList());
    return new ImmutableDatasetRoot(this.datasets, newValue);
  }

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

  private boolean equalTo(int synthetic, ImmutableDatasetRoot another) {
    return datasets.equals(another.datasets)
        && lineage.equals(another.lineage);
  }

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

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

  /**
   * Utility type used to correctly read immutable object from JSON representation.
   * @deprecated Do not use this type directly, it exists only for the <em>Jackson</em>-binding infrastructure
   */
  @Generated(from = "DatasetRoot", generator = "Immutables")
  @Deprecated
  @JsonDeserialize
  @JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.NONE)
  static final class Json implements DatasetRoot {
    @Nullable List<ZdfDataset> datasets = List.of();
    boolean datasetsIsSet;
    @Nullable List<ZdfProcess> lineage = List.of();
    boolean lineageIsSet;
    @JsonProperty("datasets")
    public void setDatasets(List<ZdfDataset> datasets) {
      this.datasets = datasets;
      this.datasetsIsSet = null != datasets;
    }
    @JsonProperty("lineage")
    public void setLineage(List<ZdfProcess> lineage) {
      this.lineage = lineage;
      this.lineageIsSet = null != lineage;
    }
    @Override
    public List<ZdfDataset> datasets() { throw new UnsupportedOperationException(); }
    @Override
    public List<ZdfProcess> lineage() { throw new UnsupportedOperationException(); }
  }

  /**
   * @param json A JSON-bindable data structure
   * @return An immutable value type
   * @deprecated Do not use this method directly, it exists only for the <em>Jackson</em>-binding infrastructure
   */
  @Deprecated
  @JsonCreator(mode = JsonCreator.Mode.DELEGATING)
  static ImmutableDatasetRoot fromJson(Json json) {
    ImmutableDatasetRoot.Builder builder = ImmutableDatasetRoot.builder();
    if (json.datasetsIsSet) {
      builder.addAllDatasets(json.datasets);
    }
    if (json.lineageIsSet) {
      builder.addAllLineage(json.lineage);
    }
    return builder.build();
  }

  /**
   * Creates an immutable copy of a {@link DatasetRoot} 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 DatasetRoot instance
   */
  public static ImmutableDatasetRoot copyOf(DatasetRoot instance) {
    if (instance instanceof ImmutableDatasetRoot) {
      return (ImmutableDatasetRoot) instance;
    }
    return ImmutableDatasetRoot.builder()
        .from(instance)
        .build();
  }

  /**
   * Creates a builder for {@link ImmutableDatasetRoot ImmutableDatasetRoot}.
   * <pre>
   * ImmutableDatasetRoot.builder()
   *    .addDatasets|addAllDatasets(zeenea.connector.zdf.model.ZdfDataset) // {@link DatasetRoot#datasets() datasets} elements
   *    .addLineage|addAllLineage(zeenea.connector.zdf.model.ZdfProcess) // {@link DatasetRoot#lineage() lineage} elements
   *    .build();
   * </pre>
   * @return A new ImmutableDatasetRoot builder
   */
  public static ImmutableDatasetRoot.Builder builder() {
    return new ImmutableDatasetRoot.Builder();
  }

  /**
   * Builds instances of type {@link ImmutableDatasetRoot ImmutableDatasetRoot}.
   * 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 = "DatasetRoot", generator = "Immutables")
  @NotThreadSafe
  public static final class Builder {
    private static final long OPT_BIT_DATASETS = 0x1L;
    private static final long OPT_BIT_LINEAGE = 0x2L;
    private long optBits;

    private List<ZdfDataset> datasets = new ArrayList<ZdfDataset>();
    private List<ZdfProcess> lineage = new ArrayList<ZdfProcess>();

    private Builder() {
    }

    /**
     * Fill a builder with attribute values from the provided {@code DatasetRoot} instance.
     * Regular attribute values will be replaced with those from the given instance.
     * Absent optional values will not replace present values.
     * Collection elements and entries will be added, not replaced.
     * @param instance The instance from which to copy values
     * @return {@code this} builder for use in a chained invocation
     */
    public final Builder from(DatasetRoot instance) {
      Objects.requireNonNull(instance, "instance");
      addAllDatasets(instance.datasets());
      addAllLineage(instance.lineage());
      return this;
    }

    /**
     * Adds one element to {@link DatasetRoot#datasets() datasets} list.
     * @param element A datasets element
     * @return {@code this} builder for use in a chained invocation
     */
    public final Builder addDatasets(ZdfDataset element) {
      this.datasets.add(Objects.requireNonNull(element, "datasets element"));
      optBits |= OPT_BIT_DATASETS;
      return this;
    }

    /**
     * Adds elements to {@link DatasetRoot#datasets() datasets} list.
     * @param elements An array of datasets elements
     * @return {@code this} builder for use in a chained invocation
     */
    public final Builder addDatasets(ZdfDataset... elements) {
      for (ZdfDataset element : elements) {
        this.datasets.add(Objects.requireNonNull(element, "datasets element"));
      }
      optBits |= OPT_BIT_DATASETS;
      return this;
    }


    /**
     * Sets or replaces all elements for {@link DatasetRoot#datasets() datasets} list.
     * @param elements An iterable of datasets elements
     * @return {@code this} builder for use in a chained invocation
     */
    @JsonProperty("datasets")
    public final Builder datasets(Iterable<? extends ZdfDataset> elements) {
      this.datasets.clear();
      return addAllDatasets(elements);
    }

    /**
     * Adds elements to {@link DatasetRoot#datasets() datasets} list.
     * @param elements An iterable of datasets elements
     * @return {@code this} builder for use in a chained invocation
     */
    public final Builder addAllDatasets(Iterable<? extends ZdfDataset> elements) {
      for (ZdfDataset element : elements) {
        this.datasets.add(Objects.requireNonNull(element, "datasets element"));
      }
      optBits |= OPT_BIT_DATASETS;
      return this;
    }

    /**
     * Adds one element to {@link DatasetRoot#lineage() lineage} list.
     * @param element A lineage element
     * @return {@code this} builder for use in a chained invocation
     */
    public final Builder addLineage(ZdfProcess element) {
      this.lineage.add(Objects.requireNonNull(element, "lineage element"));
      optBits |= OPT_BIT_LINEAGE;
      return this;
    }

    /**
     * Adds elements to {@link DatasetRoot#lineage() lineage} list.
     * @param elements An array of lineage elements
     * @return {@code this} builder for use in a chained invocation
     */
    public final Builder addLineage(ZdfProcess... elements) {
      for (ZdfProcess element : elements) {
        this.lineage.add(Objects.requireNonNull(element, "lineage element"));
      }
      optBits |= OPT_BIT_LINEAGE;
      return this;
    }


    /**
     * Sets or replaces all elements for {@link DatasetRoot#lineage() lineage} list.
     * @param elements An iterable of lineage elements
     * @return {@code this} builder for use in a chained invocation
     */
    @JsonProperty("lineage")
    public final Builder lineage(Iterable<? extends ZdfProcess> elements) {
      this.lineage.clear();
      return addAllLineage(elements);
    }

    /**
     * Adds elements to {@link DatasetRoot#lineage() lineage} list.
     * @param elements An iterable of lineage elements
     * @return {@code this} builder for use in a chained invocation
     */
    public final Builder addAllLineage(Iterable<? extends ZdfProcess> elements) {
      for (ZdfProcess element : elements) {
        this.lineage.add(Objects.requireNonNull(element, "lineage element"));
      }
      optBits |= OPT_BIT_LINEAGE;
      return this;
    }

    /**
     * Builds a new {@link ImmutableDatasetRoot ImmutableDatasetRoot}.
     * @return An immutable instance of DatasetRoot
     * @throws java.lang.IllegalStateException if any required attributes are missing
     */
    public ImmutableDatasetRoot build() {
      return new ImmutableDatasetRoot(this);
    }

    private boolean datasetsIsSet() {
      return (optBits & OPT_BIT_DATASETS) != 0;
    }

    private boolean lineageIsSet() {
      return (optBits & OPT_BIT_LINEAGE) != 0;
    }
  }
}
