All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.io7m.changelog.core.CItem Maven / Gradle / Ivy

There is a newer version: 3.0.3
Show newest version
package com.io7m.changelog.core;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import javax.annotation.Generated;

/**
 * A changelog item for a specific release.
 */
@SuppressWarnings({"all"})
@Generated({"Immutables.generator", "CItemType"})
public final class CItem implements CItemType {
  private final LocalDate date;
  private final String summary;
  private final List tickets;
  private final CChangeType type;

  private CItem(
      LocalDate date,
      String summary,
      Iterable tickets,
      CChangeType type) {
    this.date = Objects.requireNonNull(date, "date");
    this.summary = Objects.requireNonNull(summary, "summary");
    this.tickets = createUnmodifiableList(false, createSafeList(tickets, true, false));
    this.type = Objects.requireNonNull(type, "type");
  }

  private CItem(
      CItem original,
      LocalDate date,
      String summary,
      List tickets,
      CChangeType type) {
    this.date = date;
    this.summary = summary;
    this.tickets = tickets;
    this.type = type;
  }

  /**
   * @return The change date
   */
  @Override
  public LocalDate date() {
    return date;
  }

  /**
   * @return The change summary
   */
  @Override
  public String summary() {
    return summary;
  }

  /**
   * @return The change tickets
   */
  @Override
  public List tickets() {
    return tickets;
  }

  /**
   * @return The change type
   */
  @Override
  public CChangeType type() {
    return type;
  }

  /**
   * Copy the current immutable object by setting a value for the {@link CItemType#date() date} 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 date
   * @return A modified copy of the {@code this} object
   */
  public final CItem withDate(LocalDate value) {
    if (this.date == value) return this;
    LocalDate newValue = Objects.requireNonNull(value, "date");
    return new CItem(this, newValue, this.summary, this.tickets, this.type);
  }

  /**
   * Copy the current immutable object by setting a value for the {@link CItemType#summary() summary} attribute.
   * An equals check used to prevent copying of the same value by returning {@code this}.
   * @param value A new value for summary
   * @return A modified copy of the {@code this} object
   */
  public final CItem withSummary(String value) {
    if (this.summary.equals(value)) return this;
    String newValue = Objects.requireNonNull(value, "summary");
    return new CItem(this, this.date, newValue, this.tickets, this.type);
  }

  /**
   * Copy the current immutable object with elements that replace the content of {@link CItemType#tickets() tickets}.
   * @param elements The elements to set
   * @return A modified copy of {@code this} object
   */
  public final CItem withTickets(String... elements) {
    List newValue = createUnmodifiableList(false, createSafeList(Arrays.asList(elements), true, false));
    return new CItem(this, this.date, this.summary, newValue, this.type);
  }

  /**
   * Copy the current immutable object with elements that replace the content of {@link CItemType#tickets() tickets}.
   * A shallow reference equality check is used to prevent copying of the same value by returning {@code this}.
   * @param elements An iterable of tickets elements to set
   * @return A modified copy of {@code this} object
   */
  public final CItem withTickets(Iterable elements) {
    if (this.tickets == elements) return this;
    List newValue = createUnmodifiableList(false, createSafeList(elements, true, false));
    return new CItem(this, this.date, this.summary, newValue, this.type);
  }

  /**
   * Copy the current immutable object by setting a value for the {@link CItemType#type() type} 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 type
   * @return A modified copy of the {@code this} object
   */
  public final CItem withType(CChangeType value) {
    if (this.type == value) return this;
    CChangeType newValue = Objects.requireNonNull(value, "type");
    return new CItem(this, this.date, this.summary, this.tickets, newValue);
  }

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

  private boolean equalTo(CItem another) {
    return date.equals(another.date)
        && summary.equals(another.summary)
        && tickets.equals(another.tickets)
        && type.equals(another.type);
  }

  /**
   * Computes a hash code from attributes: {@code date}, {@code summary}, {@code tickets}, {@code type}.
   * @return hashCode value
   */
  @Override
  public int hashCode() {
    int h = 31;
    h = h * 17 + date.hashCode();
    h = h * 17 + summary.hashCode();
    h = h * 17 + tickets.hashCode();
    h = h * 17 + type.hashCode();
    return h;
  }

  /**
   * Prints the immutable value {@code CItem} with attribute values.
   * @return A string representation of the value
   */
  @Override
  public String toString() {
    return "CItem{"
        + "date=" + date
        + ", summary=" + summary
        + ", tickets=" + tickets
        + ", type=" + type
        + "}";
  }

  /**
   * Construct a new immutable {@code CItem} instance.
   * @param date The value for the {@code date} attribute
   * @param summary The value for the {@code summary} attribute
   * @param tickets The value for the {@code tickets} attribute
   * @param type The value for the {@code type} attribute
   * @return An immutable CItem instance
   */
  public static CItem of(LocalDate date, String summary, List tickets, CChangeType type) {
    return of(date, summary, (Iterable) tickets, type);
  }

  /**
   * Construct a new immutable {@code CItem} instance.
   * @param date The value for the {@code date} attribute
   * @param summary The value for the {@code summary} attribute
   * @param tickets The value for the {@code tickets} attribute
   * @param type The value for the {@code type} attribute
   * @return An immutable CItem instance
   */
  public static CItem of(LocalDate date, String summary, Iterable tickets, CChangeType type) {
    return new CItem(date, summary, tickets, type);
  }

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

  /**
   * Creates a builder for {@link CItem CItem}.
   * @return A new CItem builder
   */
  public static CItem.Builder builder() {
    return new CItem.Builder();
  }

  /**
   * Builds instances of type {@link CItem CItem}.
   * Initialize attributes and then invoke the {@link #build()} method to create an
   * immutable instance.
   * 

{@code Builder} is not thread-safe and generally should not be stored in a field or collection, * but instead used immediately to create instances. */ public static final class Builder { private static final long INIT_BIT_DATE = 0x1L; private static final long INIT_BIT_SUMMARY = 0x2L; private static final long INIT_BIT_TYPE = 0x4L; private long initBits = 0x7L; private LocalDate date; private String summary; private List tickets = new ArrayList(); private CChangeType type; private Builder() { } /** * Fill a builder with attribute values from the provided {@code CItemType} 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(CItemType instance) { Objects.requireNonNull(instance, "instance"); setDate(instance.date()); setSummary(instance.summary()); addAllTickets(instance.tickets()); setType(instance.type()); return this; } /** * Initializes the value for the {@link CItemType#date() date} attribute. * @param date The value for date * @return {@code this} builder for use in a chained invocation */ public final Builder setDate(LocalDate date) { this.date = Objects.requireNonNull(date, "date"); initBits &= ~INIT_BIT_DATE; return this; } /** * Initializes the value for the {@link CItemType#summary() summary} attribute. * @param summary The value for summary * @return {@code this} builder for use in a chained invocation */ public final Builder setSummary(String summary) { this.summary = Objects.requireNonNull(summary, "summary"); initBits &= ~INIT_BIT_SUMMARY; return this; } /** * Adds one element to {@link CItemType#tickets() tickets} list. * @param element A tickets element * @return {@code this} builder for use in a chained invocation */ public final Builder addTickets(String element) { this.tickets.add(Objects.requireNonNull(element, "tickets element")); return this; } /** * Adds elements to {@link CItemType#tickets() tickets} list. * @param elements An array of tickets elements * @return {@code this} builder for use in a chained invocation */ public final Builder addTickets(String... elements) { for (String element : elements) { this.tickets.add(Objects.requireNonNull(element, "tickets element")); } return this; } /** * Sets or replaces all elements for {@link CItemType#tickets() tickets} list. * @param elements An iterable of tickets elements * @return {@code this} builder for use in a chained invocation */ public final Builder setTickets(Iterable elements) { this.tickets.clear(); return addAllTickets(elements); } /** * Adds elements to {@link CItemType#tickets() tickets} list. * @param elements An iterable of tickets elements * @return {@code this} builder for use in a chained invocation */ public final Builder addAllTickets(Iterable elements) { for (String element : elements) { this.tickets.add(Objects.requireNonNull(element, "tickets element")); } return this; } /** * Initializes the value for the {@link CItemType#type() type} attribute. * @param type The value for type * @return {@code this} builder for use in a chained invocation */ public final Builder setType(CChangeType type) { this.type = Objects.requireNonNull(type, "type"); initBits &= ~INIT_BIT_TYPE; return this; } /** * Builds a new {@link CItem CItem}. * @return An immutable instance of CItem * @throws java.lang.IllegalStateException if any required attributes are missing */ public CItem build() { if (initBits != 0) { throw new IllegalStateException(formatRequiredAttributesMessage()); } return new CItem(null, date, summary, createUnmodifiableList(true, tickets), type); } private String formatRequiredAttributesMessage() { List attributes = new ArrayList(); if ((initBits & INIT_BIT_DATE) != 0) attributes.add("date"); if ((initBits & INIT_BIT_SUMMARY) != 0) attributes.add("summary"); if ((initBits & INIT_BIT_TYPE) != 0) attributes.add("type"); return "Cannot build CItem, some of required attributes are not set " + attributes; } } private static List createSafeList(Iterable iterable, boolean checkNulls, boolean skipNulls) { ArrayList list; if (iterable instanceof Collection) { int size = ((Collection) iterable).size(); if (size == 0) return Collections.emptyList(); list = new ArrayList(); } else { list = new ArrayList(); } for (T element : iterable) { if (skipNulls && element == null) continue; if (checkNulls) Objects.requireNonNull(element, "element"); list.add(element); } return list; } private static List createUnmodifiableList(boolean clone, List list) { switch(list.size()) { case 0: return Collections.emptyList(); case 1: return Collections.singletonList(list.get(0)); default: if (clone) { return Collections.unmodifiableList(new ArrayList(list)); } else { if (list instanceof ArrayList) { ((ArrayList) list).trimToSize(); } return Collections.unmodifiableList(list); } } } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy