com.io7m.changelog.core.CChange 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;
import java.util.Optional;
/**
* A change within a specific release.
*/
@SuppressWarnings({"all"})
public final class CChange implements CChangeType {
private final ZonedDateTime date;
private final String summary;
private final List tickets;
private final CModuleName module;
private final boolean backwardsCompatible;
private CChange(CChange.Builder builder) {
this.date = builder.date;
this.summary = builder.summary;
this.tickets = createUnmodifiableList(true, builder.tickets);
this.module = builder.module;
this.backwardsCompatible = builder.backwardsCompatibleIsSet()
? builder.backwardsCompatible
: CChangeType.super.backwardsCompatible();
}
private CChange(
ZonedDateTime date,
String summary,
List tickets,
CModuleName module,
boolean backwardsCompatible) {
this.date = date;
this.summary = summary;
this.tickets = tickets;
this.module = module;
this.backwardsCompatible = backwardsCompatible;
}
/**
* @return The change date
*/
@Override
public ZonedDateTime 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 module that this change affects
*/
@Override
public Optional module() {
return Optional.ofNullable(module);
}
/**
* @return {@code true} iff the change is backwards compatible
*/
@Override
public boolean backwardsCompatible() {
return backwardsCompatible;
}
/**
* Copy the current immutable object by setting a value for the {@link CChangeType#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 CChange withDate(ZonedDateTime value) {
if (this.date == value) return this;
ZonedDateTime newValue = Objects.requireNonNull(value, "date");
return new CChange(newValue, this.summary, this.tickets, this.module, this.backwardsCompatible);
}
/**
* Copy the current immutable object by setting a value for the {@link CChangeType#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 CChange withSummary(String value) {
String newValue = Objects.requireNonNull(value, "summary");
if (this.summary.equals(newValue)) return this;
return new CChange(this.date, newValue, this.tickets, this.module, this.backwardsCompatible);
}
/**
* Copy the current immutable object with elements that replace the content of {@link CChangeType#tickets() tickets}.
* @param elements The elements to set
* @return A modified copy of {@code this} object
*/
public final CChange withTickets(CTicketID... elements) {
List newValue = createUnmodifiableList(false, createSafeList(Arrays.asList(elements), true, false));
return new CChange(this.date, this.summary, newValue, this.module, this.backwardsCompatible);
}
/**
* Copy the current immutable object with elements that replace the content of {@link CChangeType#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 CChange withTickets(Iterable extends CTicketID> elements) {
if (this.tickets == elements) return this;
List newValue = createUnmodifiableList(false, createSafeList(elements, true, false));
return new CChange(this.date, this.summary, newValue, this.module, this.backwardsCompatible);
}
/**
* Copy the current immutable object by setting a present value for the optional {@link CChangeType#module() module} attribute.
* @param value The value for module
* @return A modified copy of {@code this} object
*/
public final CChange withModule(CModuleName value) {
CModuleName newValue = Objects.requireNonNull(value, "module");
if (this.module == newValue) return this;
return new CChange(this.date, this.summary, this.tickets, newValue, this.backwardsCompatible);
}
/**
* Copy the current immutable object by setting an optional value for the {@link CChangeType#module() module} attribute.
* A shallow reference equality check is used on unboxed optional value to prevent copying of the same value by returning {@code this}.
* @param optional A value for module
* @return A modified copy of {@code this} object
*/
@SuppressWarnings("unchecked") // safe covariant cast
public final CChange withModule(Optional extends CModuleName> optional) {
CModuleName value = optional.orElse(null);
if (this.module == value) return this;
return new CChange(this.date, this.summary, this.tickets, value, this.backwardsCompatible);
}
/**
* Copy the current immutable object by setting a value for the {@link CChangeType#backwardsCompatible() backwardsCompatible} attribute.
* A value equality check is used to prevent copying of the same value by returning {@code this}.
* @param value A new value for backwardsCompatible
* @return A modified copy of the {@code this} object
*/
public final CChange withBackwardsCompatible(boolean value) {
if (this.backwardsCompatible == value) return this;
return new CChange(this.date, this.summary, this.tickets, this.module, value);
}
/**
* This instance is equal to all instances of {@code CChange} 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 CChange
&& equalTo(0, (CChange) another);
}
private boolean equalTo(int synthetic, CChange another) {
return date.equals(another.date)
&& summary.equals(another.summary)
&& tickets.equals(another.tickets)
&& Objects.equals(module, another.module)
&& backwardsCompatible == another.backwardsCompatible;
}
/**
* Computes a hash code from attributes: {@code date}, {@code summary}, {@code tickets}, {@code module}, {@code backwardsCompatible}.
* @return hashCode value
*/
@Override
public int hashCode() {
int h = 5381;
h += (h << 5) + date.hashCode();
h += (h << 5) + summary.hashCode();
h += (h << 5) + tickets.hashCode();
h += (h << 5) + Objects.hashCode(module);
h += (h << 5) + Boolean.hashCode(backwardsCompatible);
return h;
}
/**
* Prints the immutable value {@code CChange} with attribute values.
* @return A string representation of the value
*/
@Override
public String toString() {
StringBuilder builder = new StringBuilder("CChange{");
builder.append("date=").append(date);
builder.append(", ");
builder.append("summary=").append(summary);
builder.append(", ");
builder.append("tickets=").append(tickets);
if (module != null) {
builder.append(", ");
builder.append("module=").append(module);
}
builder.append(", ");
builder.append("backwardsCompatible=").append(backwardsCompatible);
return builder.append("}").toString();
}
/**
* Creates an immutable copy of a {@link CChangeType} 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 CChange instance
*/
public static CChange copyOf(CChangeType instance) {
if (instance instanceof CChange) {
return (CChange) instance;
}
return CChange.builder()
.from(instance)
.build();
}
/**
* Creates a builder for {@link CChange CChange}.
*
* CChange.builder()
* .setDate(java.time.ZonedDateTime) // required {@link CChangeType#date() date}
* .setSummary(String) // required {@link CChangeType#summary() summary}
* .addTickets|addAllTickets(CTicketID) // {@link CChangeType#tickets() tickets} elements
* .setModule(CModuleName) // optional {@link CChangeType#module() module}
* .setBackwardsCompatible(boolean) // optional {@link CChangeType#backwardsCompatible() backwardsCompatible}
* .build();
*
* @return A new CChange builder
*/
public static CChange.Builder builder() {
return new CChange.Builder();
}
/**
* Builds instances of type {@link CChange CChange}.
* 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 OPT_BIT_BACKWARDS_COMPATIBLE = 0x1L;
private long initBits = 0x3L;
private long optBits;
private ZonedDateTime date;
private String summary;
private List tickets = new ArrayList();
private CModuleName module;
private boolean backwardsCompatible;
private Builder() {
}
/**
* Fill a builder with attribute values from the provided {@code CChangeType} 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(CChangeType instance) {
Objects.requireNonNull(instance, "instance");
this.setDate(instance.date());
this.setSummary(instance.summary());
addAllTickets(instance.tickets());
Optional moduleOptional = instance.module();
if (moduleOptional.isPresent()) {
setModule(moduleOptional);
}
this.setBackwardsCompatible(instance.backwardsCompatible());
return this;
}
/**
* Initializes the value for the {@link CChangeType#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;
}
/**
* Initializes the value for the {@link CChangeType#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 CChangeType#tickets() tickets} list.
* @param element A tickets element
* @return {@code this} builder for use in a chained invocation
*/
public final Builder addTickets(CTicketID element) {
this.tickets.add(Objects.requireNonNull(element, "tickets element"));
return this;
}
/**
* Adds elements to {@link CChangeType#tickets() tickets} list.
* @param elements An array of tickets elements
* @return {@code this} builder for use in a chained invocation
*/
public final Builder addTickets(CTicketID... elements) {
for (CTicketID element : elements) {
this.tickets.add(Objects.requireNonNull(element, "tickets element"));
}
return this;
}
/**
* Sets or replaces all elements for {@link CChangeType#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 extends CTicketID> elements) {
this.tickets.clear();
return addAllTickets(elements);
}
/**
* Adds elements to {@link CChangeType#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 extends CTicketID> elements) {
for (CTicketID element : elements) {
this.tickets.add(Objects.requireNonNull(element, "tickets element"));
}
return this;
}
/**
* Initializes the optional value {@link CChangeType#module() module} to module.
* @param module The value for module
* @return {@code this} builder for chained invocation
*/
public final Builder setModule(CModuleName module) {
this.module = Objects.requireNonNull(module, "module");
return this;
}
/**
* Initializes the optional value {@link CChangeType#module() module} to module.
* @param module The value for module
* @return {@code this} builder for use in a chained invocation
*/
public final Builder setModule(Optional extends CModuleName> module) {
this.module = module.orElse(null);
return this;
}
/**
* Initializes the value for the {@link CChangeType#backwardsCompatible() backwardsCompatible} attribute.
* If not set, this attribute will have a default value as returned by the initializer of {@link CChangeType#backwardsCompatible() backwardsCompatible}.
* @param backwardsCompatible The value for backwardsCompatible
* @return {@code this} builder for use in a chained invocation
*/
public final Builder setBackwardsCompatible(boolean backwardsCompatible) {
this.backwardsCompatible = backwardsCompatible;
optBits |= OPT_BIT_BACKWARDS_COMPATIBLE;
return this;
}
/**
* Builds a new {@link CChange CChange}.
* @return An immutable instance of CChange
* @throws java.lang.IllegalStateException if any required attributes are missing
*/
public CChange build() {
if (initBits != 0) {
throw new IllegalStateException(formatRequiredAttributesMessage());
}
return new CChange(this);
}
private boolean backwardsCompatibleIsSet() {
return (optBits & OPT_BIT_BACKWARDS_COMPATIBLE) != 0;
}
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");
return "Cannot build CChange, 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);
}
}
}
}