package zeenea.connector.zdf.model;

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.ParametersAreNonnullByDefault;
import javax.annotation.concurrent.Immutable;
import javax.annotation.concurrent.NotThreadSafe;
import org.immutables.value.Generated;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
 * Immutable implementation of {@link ZdfForeignKey}.
 * <p>
 * Use the builder to create immutable instances:
 * {@code ImmutableZdfForeignKey.builder()}.
 */
@Generated(from = "ZdfForeignKey", generator = "Immutables")
@SuppressWarnings({"all"})
@SuppressFBWarnings
@ParametersAreNonnullByDefault
@javax.annotation.processing.Generated("org.immutables.processor.ProxyProcessor")
@Immutable
public final class ImmutableZdfForeignKey implements ZdfForeignKey {
  @NotNull
  private final String dataset;
  @NotNull
  private final List<String> sourceFields;
  @NotNull
  private final List<String> targetFields;
  private final @Nullable String name;

  private ImmutableZdfForeignKey(
      String dataset,
      List<String> sourceFields,
      List<String> targetFields,
      @Nullable String name) {
    this.dataset = dataset;
    this.sourceFields = sourceFields;
    this.targetFields = targetFields;
    this.name = name;
  }

  /**
   * @return The value of the {@code dataset} attribute
   */
  @JsonProperty("dataset")
  @NotNull
  @Override
  public String dataset() {
    return dataset;
  }

  /**
   * @return The value of the {@code sourceFields} attribute
   */
  @JsonProperty("sourceFields")
  @NotNull
  @Override
  public List<String> sourceFields() {
    return sourceFields;
  }

  /**
   * @return The value of the {@code targetFields} attribute
   */
  @JsonProperty("targetFields")
  @NotNull
  @Override
  public List<String> targetFields() {
    return targetFields;
  }

  /**
   * @return The value of the {@code name} attribute
   */
  @JsonProperty("name")
  @Override
  public @Nullable String name() {
    return name;
  }

  /**
   * Copy the current immutable object by setting a value for the {@link ZdfForeignKey#dataset() dataset} attribute.
   * An equals check used to prevent copying of the same value by returning {@code this}.
   * @param value A new value for dataset
   * @return A modified copy of the {@code this} object
   */
  public final ImmutableZdfForeignKey withDataset(String value) {
    String newValue = Objects.requireNonNull(value, "dataset");
    if (this.dataset.equals(newValue)) return this;
    return new ImmutableZdfForeignKey(newValue, this.sourceFields, this.targetFields, this.name);
  }

  /**
   * Copy the current immutable object with elements that replace the content of {@link ZdfForeignKey#sourceFields() sourceFields}.
   * @param elements The elements to set
   * @return A modified copy of {@code this} object
   */
  public final ImmutableZdfForeignKey withSourceFields(String... elements) {
    List<String> newValue = List.of(elements);
    return new ImmutableZdfForeignKey(this.dataset, newValue, this.targetFields, this.name);
  }

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

  /**
   * Copy the current immutable object with elements that replace the content of {@link ZdfForeignKey#targetFields() targetFields}.
   * @param elements The elements to set
   * @return A modified copy of {@code this} object
   */
  public final ImmutableZdfForeignKey withTargetFields(String... elements) {
    List<String> newValue = List.of(elements);
    return new ImmutableZdfForeignKey(this.dataset, this.sourceFields, newValue, this.name);
  }

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

  /**
   * Copy the current immutable object by setting a value for the {@link ZdfForeignKey#name() name} attribute.
   * An equals check used to prevent copying of the same value by returning {@code this}.
   * @param value A new value for name (can be {@code null})
   * @return A modified copy of the {@code this} object
   */
  public final ImmutableZdfForeignKey withName(@Nullable String value) {
    if (Objects.equals(this.name, value)) return this;
    return new ImmutableZdfForeignKey(this.dataset, this.sourceFields, this.targetFields, value);
  }

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

  private boolean equalTo(int synthetic, ImmutableZdfForeignKey another) {
    return dataset.equals(another.dataset)
        && sourceFields.equals(another.sourceFields)
        && targetFields.equals(another.targetFields)
        && Objects.equals(name, another.name);
  }

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

  /**
   * Prints the immutable value {@code ZdfForeignKey} with attribute values.
   * @return A string representation of the value
   */
  @Override
  public String toString() {
    return "ZdfForeignKey{"
        + "dataset=" + dataset
        + ", sourceFields=" + sourceFields
        + ", targetFields=" + targetFields
        + ", name=" + name
        + "}";
  }

  /**
   * 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 = "ZdfForeignKey", generator = "Immutables")
  @Deprecated
  @JsonDeserialize
  @JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.NONE)
  static final class Json implements ZdfForeignKey {
    @javax.annotation.Nullable String dataset;
    @javax.annotation.Nullable List<String> sourceFields = List.of();
    @javax.annotation.Nullable List<String> targetFields = List.of();
    @javax.annotation.Nullable String name;
    @JsonProperty("dataset")
    public void setDataset(String dataset) {
      this.dataset = dataset;
    }
    @JsonProperty("sourceFields")
    public void setSourceFields(List<String> sourceFields) {
      this.sourceFields = sourceFields;
    }
    @JsonProperty("targetFields")
    public void setTargetFields(List<String> targetFields) {
      this.targetFields = targetFields;
    }
    @JsonProperty("name")
    public void setName(@Nullable String name) {
      this.name = name;
    }
    @Override
    public String dataset() { throw new UnsupportedOperationException(); }
    @Override
    public List<String> sourceFields() { throw new UnsupportedOperationException(); }
    @Override
    public List<String> targetFields() { throw new UnsupportedOperationException(); }
    @Override
    public String name() { 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 ImmutableZdfForeignKey fromJson(Json json) {
    ImmutableZdfForeignKey.Builder builder = ImmutableZdfForeignKey.builder();
    if (json.dataset != null) {
      builder.dataset(json.dataset);
    }
    if (json.sourceFields != null) {
      builder.addAllSourceFields(json.sourceFields);
    }
    if (json.targetFields != null) {
      builder.addAllTargetFields(json.targetFields);
    }
    if (json.name != null) {
      builder.name(json.name);
    }
    return builder.build();
  }

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

  /**
   * Creates a builder for {@link ImmutableZdfForeignKey ImmutableZdfForeignKey}.
   * <pre>
   * ImmutableZdfForeignKey.builder()
   *    .dataset(String) // required {@link ZdfForeignKey#dataset() dataset}
   *    .addSourceFields|addAllSourceFields(String) // {@link ZdfForeignKey#sourceFields() sourceFields} elements
   *    .addTargetFields|addAllTargetFields(String) // {@link ZdfForeignKey#targetFields() targetFields} elements
   *    .name(String | null) // nullable {@link ZdfForeignKey#name() name}
   *    .build();
   * </pre>
   * @return A new ImmutableZdfForeignKey builder
   */
  public static ImmutableZdfForeignKey.Builder builder() {
    return new ImmutableZdfForeignKey.Builder();
  }

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

    private @javax.annotation.Nullable String dataset;
    private List<String> sourceFields = new ArrayList<String>();
    private List<String> targetFields = new ArrayList<String>();
    private @javax.annotation.Nullable String name;

    private Builder() {
    }

    /**
     * Fill a builder with attribute values from the provided {@code ZdfForeignKey} 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(ZdfForeignKey instance) {
      Objects.requireNonNull(instance, "instance");
      this.dataset(instance.dataset());
      addAllSourceFields(instance.sourceFields());
      addAllTargetFields(instance.targetFields());
      @Nullable String nameValue = instance.name();
      if (nameValue != null) {
        name(nameValue);
      }
      return this;
    }

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

    /**
     * Adds one element to {@link ZdfForeignKey#sourceFields() sourceFields} list.
     * @param element A sourceFields element
     * @return {@code this} builder for use in a chained invocation
     */
    public final Builder addSourceFields(String element) {
      this.sourceFields.add(Objects.requireNonNull(element, "sourceFields element"));
      return this;
    }

    /**
     * Adds elements to {@link ZdfForeignKey#sourceFields() sourceFields} list.
     * @param elements An array of sourceFields elements
     * @return {@code this} builder for use in a chained invocation
     */
    public final Builder addSourceFields(String... elements) {
      for (String element : elements) {
        this.sourceFields.add(Objects.requireNonNull(element, "sourceFields element"));
      }
      return this;
    }


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

    /**
     * Adds elements to {@link ZdfForeignKey#sourceFields() sourceFields} list.
     * @param elements An iterable of sourceFields elements
     * @return {@code this} builder for use in a chained invocation
     */
    public final Builder addAllSourceFields(Iterable<String> elements) {
      for (String element : elements) {
        this.sourceFields.add(Objects.requireNonNull(element, "sourceFields element"));
      }
      return this;
    }

    /**
     * Adds one element to {@link ZdfForeignKey#targetFields() targetFields} list.
     * @param element A targetFields element
     * @return {@code this} builder for use in a chained invocation
     */
    public final Builder addTargetFields(String element) {
      this.targetFields.add(Objects.requireNonNull(element, "targetFields element"));
      return this;
    }

    /**
     * Adds elements to {@link ZdfForeignKey#targetFields() targetFields} list.
     * @param elements An array of targetFields elements
     * @return {@code this} builder for use in a chained invocation
     */
    public final Builder addTargetFields(String... elements) {
      for (String element : elements) {
        this.targetFields.add(Objects.requireNonNull(element, "targetFields element"));
      }
      return this;
    }


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

    /**
     * Adds elements to {@link ZdfForeignKey#targetFields() targetFields} list.
     * @param elements An iterable of targetFields elements
     * @return {@code this} builder for use in a chained invocation
     */
    public final Builder addAllTargetFields(Iterable<String> elements) {
      for (String element : elements) {
        this.targetFields.add(Objects.requireNonNull(element, "targetFields element"));
      }
      return this;
    }

    /**
     * Initializes the value for the {@link ZdfForeignKey#name() name} attribute.
     * @param name The value for name (can be {@code null})
     * @return {@code this} builder for use in a chained invocation
     */
    @JsonProperty("name")
    public final Builder name(@Nullable String name) {
      this.name = name;
      return this;
    }

    /**
     * Builds a new {@link ImmutableZdfForeignKey ImmutableZdfForeignKey}.
     * @return An immutable instance of ZdfForeignKey
     * @throws java.lang.IllegalStateException if any required attributes are missing
     */
    public ImmutableZdfForeignKey build() {
      if (initBits != 0) {
        throw new IllegalStateException(formatRequiredAttributesMessage());
      }
      return new ImmutableZdfForeignKey(dataset, List.copyOf(sourceFields), List.copyOf(targetFields), name);
    }

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