com.io7m.changelog.core.CTicketSystem Maven / Gradle / Ivy
package com.io7m.changelog.core;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
/**
* The definition of a ticket system.
*/
@SuppressWarnings({"all"})
public final class CTicketSystem implements CTicketSystemType {
private final String id;
private final URI uri;
private final boolean isDefault;
private CTicketSystem(CTicketSystem.Builder builder) {
this.id = builder.id;
this.uri = builder.uri;
this.isDefault = builder.defaultIsSet()
? builder.isDefault
: CTicketSystemType.super.isDefault();
}
private CTicketSystem(String id, URI uri, boolean isDefault) {
this.id = id;
this.uri = uri;
this.isDefault = isDefault;
}
/**
* @return The ID of the ticket system
*/
@Override
public String id() {
return id;
}
/**
* @return The URI for the ticket system
*/
@Override
public URI uri() {
return uri;
}
/**
* @return {@code true} if the ticket system is the current default
*/
@Override
public boolean isDefault() {
return isDefault;
}
/**
* Copy the current immutable object by setting a value for the {@link CTicketSystemType#id() id} attribute.
* An equals check used to prevent copying of the same value by returning {@code this}.
* @param value A new value for id
* @return A modified copy of the {@code this} object
*/
public final CTicketSystem withId(String value) {
String newValue = Objects.requireNonNull(value, "id");
if (this.id.equals(newValue)) return this;
return new CTicketSystem(newValue, this.uri, this.isDefault);
}
/**
* Copy the current immutable object by setting a value for the {@link CTicketSystemType#uri() uri} 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 uri
* @return A modified copy of the {@code this} object
*/
public final CTicketSystem withUri(URI value) {
if (this.uri == value) return this;
URI newValue = Objects.requireNonNull(value, "uri");
return new CTicketSystem(this.id, newValue, this.isDefault);
}
/**
* Copy the current immutable object by setting a value for the {@link CTicketSystemType#isDefault() default} attribute.
* A value equality check is used to prevent copying of the same value by returning {@code this}.
* @param value A new value for isDefault
* @return A modified copy of the {@code this} object
*/
public final CTicketSystem withDefault(boolean value) {
if (this.isDefault == value) return this;
return new CTicketSystem(this.id, this.uri, value);
}
/**
* This instance is equal to all instances of {@code CTicketSystem} 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 CTicketSystem
&& equalTo(0, (CTicketSystem) another);
}
private boolean equalTo(int synthetic, CTicketSystem another) {
return id.equals(another.id)
&& uri.equals(another.uri)
&& isDefault == another.isDefault;
}
/**
* Computes a hash code from attributes: {@code id}, {@code uri}, {@code isDefault}.
* @return hashCode value
*/
@Override
public int hashCode() {
int h = 5381;
h += (h << 5) + id.hashCode();
h += (h << 5) + uri.hashCode();
h += (h << 5) + Boolean.hashCode(isDefault);
return h;
}
/**
* Prints the immutable value {@code CTicketSystem} with attribute values.
* @return A string representation of the value
*/
@Override
public String toString() {
return "CTicketSystem{"
+ "id=" + id
+ ", uri=" + uri
+ ", default=" + isDefault
+ "}";
}
/**
* Creates an immutable copy of a {@link CTicketSystemType} 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 CTicketSystem instance
*/
public static CTicketSystem copyOf(CTicketSystemType instance) {
if (instance instanceof CTicketSystem) {
return (CTicketSystem) instance;
}
return CTicketSystem.builder()
.from(instance)
.build();
}
/**
* Creates a builder for {@link CTicketSystem CTicketSystem}.
*
* CTicketSystem.builder()
* .setId(String) // required {@link CTicketSystemType#id() id}
* .setUri(java.net.URI) // required {@link CTicketSystemType#uri() uri}
* .setDefault(boolean) // optional {@link CTicketSystemType#isDefault() default}
* .build();
*
* @return A new CTicketSystem builder
*/
public static CTicketSystem.Builder builder() {
return new CTicketSystem.Builder();
}
/**
* Builds instances of type {@link CTicketSystem CTicketSystem}.
* 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_ID = 0x1L;
private static final long INIT_BIT_URI = 0x2L;
private static final long OPT_BIT_IS_DEFAULT = 0x1L;
private long initBits = 0x3L;
private long optBits;
private String id;
private URI uri;
private boolean isDefault;
private Builder() {
}
/**
* Fill a builder with attribute values from the provided {@code CTicketSystemType} instance.
* Regular attribute values will be replaced with those from the given instance.
* Absent optional values will not replace present values.
* @param instance The instance from which to copy values
* @return {@code this} builder for use in a chained invocation
*/
public final Builder from(CTicketSystemType instance) {
Objects.requireNonNull(instance, "instance");
this.setId(instance.id());
this.setUri(instance.uri());
this.setDefault(instance.isDefault());
return this;
}
/**
* Initializes the value for the {@link CTicketSystemType#id() id} attribute.
* @param id The value for id
* @return {@code this} builder for use in a chained invocation
*/
public final Builder setId(String id) {
this.id = Objects.requireNonNull(id, "id");
initBits &= ~INIT_BIT_ID;
return this;
}
/**
* Initializes the value for the {@link CTicketSystemType#uri() uri} attribute.
* @param uri The value for uri
* @return {@code this} builder for use in a chained invocation
*/
public final Builder setUri(URI uri) {
this.uri = Objects.requireNonNull(uri, "uri");
initBits &= ~INIT_BIT_URI;
return this;
}
/**
* Initializes the value for the {@link CTicketSystemType#isDefault() default} attribute.
*
If not set, this attribute will have a default value as returned by the initializer of {@link CTicketSystemType#isDefault() default}.
* @param isDefault The value for isDefault
* @return {@code this} builder for use in a chained invocation
*/
public final Builder setDefault(boolean isDefault) {
this.isDefault = isDefault;
optBits |= OPT_BIT_IS_DEFAULT;
return this;
}
/**
* Builds a new {@link CTicketSystem CTicketSystem}.
* @return An immutable instance of CTicketSystem
* @throws java.lang.IllegalStateException if any required attributes are missing
*/
public CTicketSystem build() {
if (initBits != 0) {
throw new IllegalStateException(formatRequiredAttributesMessage());
}
return new CTicketSystem(this);
}
private boolean defaultIsSet() {
return (optBits & OPT_BIT_IS_DEFAULT) != 0;
}
private String formatRequiredAttributesMessage() {
List attributes = new ArrayList<>();
if ((initBits & INIT_BIT_ID) != 0) attributes.add("id");
if ((initBits & INIT_BIT_URI) != 0) attributes.add("uri");
return "Cannot build CTicketSystem, some of required attributes are not set " + attributes;
}
}
}