package zeenea.connector.zdf.lineage;

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.ZdfProcess;

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

  private ImmutableLineageRoot(ImmutableLineageRoot.Builder builder) {
    this.lineage = builder.lineageIsSet()
        ? List.copyOf(builder.lineage)
        : LineageRoot.super.getLineage() instanceof Collection<?>
       ? List.copyOf((Collection<? extends ZdfProcess>) LineageRoot.super.getLineage())
       : StreamSupport.stream(LineageRoot.super.getLineage().spliterator(), false)
             .collect(Collectors.toUnmodifiableList());
  }

  private ImmutableLineageRoot(List<ZdfProcess> lineage) {
    this.lineage = lineage;
  }

  /**
   * @return The value of the {@code lineage} attribute
   */
  @JsonProperty("lineage")
  @Override
  public List<ZdfProcess> getLineage() {
    return lineage;
  }

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

  /**
   * Copy the current immutable object with elements that replace the content of {@link LineageRoot#getLineage() 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 ImmutableLineageRoot 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 ImmutableLineageRoot(newValue);
  }

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

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

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

  /**
   * Prints the immutable value {@code LineageRoot} with attribute values.
   * @return A string representation of the value
   */
  @Override
  public String toString() {
    return "LineageRoot{"
        + "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 = "LineageRoot", generator = "Immutables")
  @Deprecated
  @JsonDeserialize
  @JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.NONE)
  static final class Json implements LineageRoot {
    @Nullable List<ZdfProcess> lineage = List.of();
    boolean lineageIsSet;
    @JsonProperty("lineage")
    public void setLineage(List<ZdfProcess> lineage) {
      this.lineage = lineage;
      this.lineageIsSet = null != lineage;
    }
    @Override
    public List<ZdfProcess> getLineage() { 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 ImmutableLineageRoot fromJson(Json json) {
    ImmutableLineageRoot.Builder builder = ImmutableLineageRoot.builder();
    if (json.lineageIsSet) {
      builder.addAllLineage(json.lineage);
    }
    return builder.build();
  }

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

  /**
   * Creates a builder for {@link ImmutableLineageRoot ImmutableLineageRoot}.
   * <pre>
   * ImmutableLineageRoot.builder()
   *    .addLineage|addAllLineage(zeenea.connector.zdf.model.ZdfProcess) // {@link LineageRoot#getLineage() lineage} elements
   *    .build();
   * </pre>
   * @return A new ImmutableLineageRoot builder
   */
  public static ImmutableLineageRoot.Builder builder() {
    return new ImmutableLineageRoot.Builder();
  }

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

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

    private Builder() {
    }

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

    /**
     * Adds one element to {@link LineageRoot#getLineage() 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 LineageRoot#getLineage() 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 LineageRoot#getLineage() 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 LineageRoot#getLineage() 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 ImmutableLineageRoot ImmutableLineageRoot}.
     * @return An immutable instance of LineageRoot
     * @throws java.lang.IllegalStateException if any required attributes are missing
     */
    public ImmutableLineageRoot build() {
      return new ImmutableLineageRoot(this);
    }

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