com.io7m.changelog.core.CRelease Maven / Gradle / Ivy
package com.io7m.changelog.core;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
/**
* A specific release in a changelog.
*/
@SuppressWarnings({"all"})
public final class CRelease implements CReleaseType {
private final ZonedDateTime date;
private final List changes;
private final String ticketSystemID;
private final CVersion version;
private final boolean open;
private CRelease(
ZonedDateTime date,
List changes,
String ticketSystemID,
CVersion version,
boolean open) {
this.date = date;
this.changes = changes;
this.ticketSystemID = ticketSystemID;
this.version = version;
this.open = open;
}
/**
* @return The release date
*/
@Override
public ZonedDateTime date() {
return date;
}
/**
* @return The list of release changes
*/
@Override
public List changes() {
return changes;
}
/**
* @return The ticket system ID
*/
@Override
public String ticketSystemID() {
return ticketSystemID;
}
/**
* @return The version number
*/
@Override
public CVersion version() {
return version;
}
/**
* @return {@code true} if the release is open for modifications
*/
@Override
public boolean isOpen() {
return open;
}
/**
* Copy the current immutable object by setting a value for the {@link CReleaseType#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 CRelease withDate(ZonedDateTime value) {
if (this.date == value) return this;
ZonedDateTime newValue = Objects.requireNonNull(value, "date");
return new CRelease(newValue, this.changes, this.ticketSystemID, this.version, this.open);
}
/**
* Copy the current immutable object with elements that replace the content of {@link CReleaseType#changes() changes}.
* @param elements The elements to set
* @return A modified copy of {@code this} object
*/
public final CRelease withChanges(CChange... elements) {
List newValue = createUnmodifiableList(false, createSafeList(Arrays.asList(elements), true, false));
return new CRelease(this.date, newValue, this.ticketSystemID, this.version, this.open);
}
/**
* Copy the current immutable object with elements that replace the content of {@link CReleaseType#changes() changes}.
* A shallow reference equality check is used to prevent copying of the same value by returning {@code this}.
* @param elements An iterable of changes elements to set
* @return A modified copy of {@code this} object
*/
public final CRelease withChanges(Iterable extends CChange> elements) {
if (this.changes == elements) return this;
List newValue = createUnmodifiableList(false, createSafeList(elements, true, false));
return new CRelease(this.date, newValue, this.ticketSystemID, this.version, this.open);
}
/**
* Copy the current immutable object by setting a value for the {@link CReleaseType#ticketSystemID() ticketSystemID} attribute.
* An equals check used to prevent copying of the same value by returning {@code this}.
* @param value A new value for ticketSystemID
* @return A modified copy of the {@code this} object
*/
public final CRelease withTicketSystemID(String value) {
String newValue = Objects.requireNonNull(value, "ticketSystemID");
if (this.ticketSystemID.equals(newValue)) return this;
return new CRelease(this.date, this.changes, newValue, this.version, this.open);
}
/**
* Copy the current immutable object by setting a value for the {@link CReleaseType#version() version} 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 version
* @return A modified copy of the {@code this} object
*/
public final CRelease withVersion(CVersion value) {
if (this.version == value) return this;
CVersion newValue = Objects.requireNonNull(value, "version");
return new CRelease(this.date, this.changes, this.ticketSystemID, newValue, this.open);
}
/**
* Copy the current immutable object by setting a value for the {@link CReleaseType#isOpen() open} attribute.
* A value equality check is used to prevent copying of the same value by returning {@code this}.
* @param value A new value for open
* @return A modified copy of the {@code this} object
*/
public final CRelease withOpen(boolean value) {
if (this.open == value) return this;
return new CRelease(this.date, this.changes, this.ticketSystemID, this.version, value);
}
/**
* This instance is equal to all instances of {@code CRelease} 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 CRelease
&& equalTo(0, (CRelease) another);
}
private boolean equalTo(int synthetic, CRelease another) {
return date.equals(another.date)
&& changes.equals(another.changes)
&& ticketSystemID.equals(another.ticketSystemID)
&& version.equals(another.version)
&& open == another.open;
}
/**
* Computes a hash code from attributes: {@code date}, {@code changes}, {@code ticketSystemID}, {@code version}, {@code open}.
* @return hashCode value
*/
@Override
public int hashCode() {
int h = 5381;
h += (h << 5) + date.hashCode();
h += (h << 5) + changes.hashCode();
h += (h << 5) + ticketSystemID.hashCode();
h += (h << 5) + version.hashCode();
h += (h << 5) + Boolean.hashCode(open);
return h;
}
/**
* Prints the immutable value {@code CRelease} with attribute values.
* @return A string representation of the value
*/
@Override
public String toString() {
return "CRelease{"
+ "date=" + date
+ ", changes=" + changes
+ ", ticketSystemID=" + ticketSystemID
+ ", version=" + version
+ ", open=" + open
+ "}";
}
/**
* Creates an immutable copy of a {@link CReleaseType} 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 CRelease instance
*/
public static CRelease copyOf(CReleaseType instance) {
if (instance instanceof CRelease) {
return (CRelease) instance;
}
return CRelease.builder()
.from(instance)
.build();
}
/**
* Creates a builder for {@link CRelease CRelease}.
*
* CRelease.builder()
* .setDate(java.time.ZonedDateTime) // required {@link CReleaseType#date() date}
* .addChanges|addAllChanges(CChange) // {@link CReleaseType#changes() changes} elements
* .setTicketSystemID(String) // required {@link CReleaseType#ticketSystemID() ticketSystemID}
* .setVersion(com.io7m.changelog.core.CVersion) // required {@link CReleaseType#version() version}
* .setOpen(boolean) // required {@link CReleaseType#isOpen() open}
* .build();
*
* @return A new CRelease builder
*/
public static CRelease.Builder builder() {
return new CRelease.Builder();
}
/**
* Builds instances of type {@link CRelease CRelease}.
* 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_TICKET_SYSTEM_I_D = 0x2L;
private static final long INIT_BIT_VERSION = 0x4L;
private static final long INIT_BIT_OPEN = 0x8L;
private long initBits = 0xfL;
private ZonedDateTime date;
private List changes = new ArrayList();
private String ticketSystemID;
private CVersion version;
private boolean open;
private Builder() {
}
/**
* Fill a builder with attribute values from the provided {@code CReleaseType} 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(CReleaseType instance) {
Objects.requireNonNull(instance, "instance");
this.setDate(instance.date());
addAllChanges(instance.changes());
this.setTicketSystemID(instance.ticketSystemID());
this.setVersion(instance.version());
this.setOpen(instance.isOpen());
return this;
}
/**
* Initializes the value for the {@link CReleaseType#date() date} attribute.
* @param date The value for date
* @return {@code this} builder for use in a chained invocation
*/
public final Builder setDate(ZonedDateTime date) {
this.date = Objects.requireNonNull(date, "date");
initBits &= ~INIT_BIT_DATE;
return this;
}
/**
* Adds one element to {@link CReleaseType#changes() changes} list.
* @param element A changes element
* @return {@code this} builder for use in a chained invocation
*/
public final Builder addChanges(CChange element) {
this.changes.add(Objects.requireNonNull(element, "changes element"));
return this;
}
/**
* Adds elements to {@link CReleaseType#changes() changes} list.
* @param elements An array of changes elements
* @return {@code this} builder for use in a chained invocation
*/
public final Builder addChanges(CChange... elements) {
for (CChange element : elements) {
this.changes.add(Objects.requireNonNull(element, "changes element"));
}
return this;
}
/**
* Sets or replaces all elements for {@link CReleaseType#changes() changes} list.
* @param elements An iterable of changes elements
* @return {@code this} builder for use in a chained invocation
*/
public final Builder setChanges(Iterable extends CChange> elements) {
this.changes.clear();
return addAllChanges(elements);
}
/**
* Adds elements to {@link CReleaseType#changes() changes} list.
* @param elements An iterable of changes elements
* @return {@code this} builder for use in a chained invocation
*/
public final Builder addAllChanges(Iterable extends CChange> elements) {
for (CChange element : elements) {
this.changes.add(Objects.requireNonNull(element, "changes element"));
}
return this;
}
/**
* Initializes the value for the {@link CReleaseType#ticketSystemID() ticketSystemID} attribute.
* @param ticketSystemID The value for ticketSystemID
* @return {@code this} builder for use in a chained invocation
*/
public final Builder setTicketSystemID(String ticketSystemID) {
this.ticketSystemID = Objects.requireNonNull(ticketSystemID, "ticketSystemID");
initBits &= ~INIT_BIT_TICKET_SYSTEM_I_D;
return this;
}
/**
* Initializes the value for the {@link CReleaseType#version() version} attribute.
* @param version The value for version
* @return {@code this} builder for use in a chained invocation
*/
public final Builder setVersion(CVersion version) {
this.version = Objects.requireNonNull(version, "version");
initBits &= ~INIT_BIT_VERSION;
return this;
}
/**
* Initializes the value for the {@link CReleaseType#isOpen() open} attribute.
* @param open The value for open
* @return {@code this} builder for use in a chained invocation
*/
public final Builder setOpen(boolean open) {
this.open = open;
initBits &= ~INIT_BIT_OPEN;
return this;
}
/**
* Builds a new {@link CRelease CRelease}.
* @return An immutable instance of CRelease
* @throws java.lang.IllegalStateException if any required attributes are missing
*/
public CRelease build() {
if (initBits != 0) {
throw new IllegalStateException(formatRequiredAttributesMessage());
}
return new CRelease(date, createUnmodifiableList(true, changes), ticketSystemID, version, open);
}
private String formatRequiredAttributesMessage() {
List attributes = new ArrayList<>();
if ((initBits & INIT_BIT_DATE) != 0) attributes.add("date");
if ((initBits & INIT_BIT_TICKET_SYSTEM_I_D) != 0) attributes.add("ticketSystemID");
if ((initBits & INIT_BIT_VERSION) != 0) attributes.add("version");
if ((initBits & INIT_BIT_OPEN) != 0) attributes.add("open");
return "Cannot build CRelease, some of required attributes are not set " + attributes;
}
}
private static List createSafeList(Iterable extends T> 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<>(size);
} 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);
}
}
}
}