com.io7m.coffeepick.runtime.parser.spi.FormatDescription Maven / Gradle / Ivy
Show all versions of com.io7m.coffeepick.runtime.parser.spi Show documentation
package com.io7m.coffeepick.runtime.parser.spi;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
/**
* A runtimes of a supported format.
*/
@SuppressWarnings({"all"})
public final class FormatDescription implements FormatDescriptionType {
private final String description;
private final String mimeType;
private final URI name;
private FormatDescription(String description, String mimeType, URI name) {
this.description = description;
this.mimeType = mimeType;
this.name = name;
}
/**
* @return A human-readable runtimes of the format
*/
@Override
public String description() {
return description;
}
/**
* @return The mime type for the format
*/
@Override
public String mimeType() {
return mimeType;
}
/**
* @return The unique name for the format
*/
@Override
public URI name() {
return name;
}
/**
* Copy the current immutable object by setting a value for the {@link FormatDescriptionType#description() description} attribute.
* An equals check used to prevent copying of the same value by returning {@code this}.
* @param value A new value for description
* @return A modified copy of the {@code this} object
*/
public final FormatDescription withDescription(String value) {
String newValue = Objects.requireNonNull(value, "description");
if (this.description.equals(newValue)) return this;
return new FormatDescription(newValue, this.mimeType, this.name);
}
/**
* Copy the current immutable object by setting a value for the {@link FormatDescriptionType#mimeType() mimeType} attribute.
* An equals check used to prevent copying of the same value by returning {@code this}.
* @param value A new value for mimeType
* @return A modified copy of the {@code this} object
*/
public final FormatDescription withMimeType(String value) {
String newValue = Objects.requireNonNull(value, "mimeType");
if (this.mimeType.equals(newValue)) return this;
return new FormatDescription(this.description, newValue, this.name);
}
/**
* Copy the current immutable object by setting a value for the {@link FormatDescriptionType#name() name} 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 name
* @return A modified copy of the {@code this} object
*/
public final FormatDescription withName(URI value) {
if (this.name == value) return this;
URI newValue = Objects.requireNonNull(value, "name");
return new FormatDescription(this.description, this.mimeType, newValue);
}
/**
* This instance is equal to all instances of {@code FormatDescription} 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 FormatDescription
&& equalTo((FormatDescription) another);
}
private boolean equalTo(FormatDescription another) {
return description.equals(another.description)
&& mimeType.equals(another.mimeType)
&& name.equals(another.name);
}
/**
* Computes a hash code from attributes: {@code description}, {@code mimeType}, {@code name}.
* @return hashCode value
*/
@Override
public int hashCode() {
int h = 5381;
h += (h << 5) + description.hashCode();
h += (h << 5) + mimeType.hashCode();
h += (h << 5) + name.hashCode();
return h;
}
/**
* Prints the immutable value {@code FormatDescription} with attribute values.
* @return A string representation of the value
*/
@Override
public String toString() {
return "FormatDescription{"
+ "description=" + description
+ ", mimeType=" + mimeType
+ ", name=" + name
+ "}";
}
/**
* Creates an immutable copy of a {@link FormatDescriptionType} 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 FormatDescription instance
*/
public static FormatDescription copyOf(FormatDescriptionType instance) {
if (instance instanceof FormatDescription) {
return (FormatDescription) instance;
}
return FormatDescription.builder()
.from(instance)
.build();
}
/**
* Creates a builder for {@link FormatDescription FormatDescription}.
*
* FormatDescription.builder()
* .setDescription(String) // required {@link FormatDescriptionType#description() description}
* .setMimeType(String) // required {@link FormatDescriptionType#mimeType() mimeType}
* .setName(java.net.URI) // required {@link FormatDescriptionType#name() name}
* .build();
*
* @return A new FormatDescription builder
*/
public static FormatDescription.Builder builder() {
return new FormatDescription.Builder();
}
/**
* Builds instances of type {@link FormatDescription FormatDescription}.
* 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_DESCRIPTION = 0x1L;
private static final long INIT_BIT_MIME_TYPE = 0x2L;
private static final long INIT_BIT_NAME = 0x4L;
private long initBits = 0x7L;
private String description;
private String mimeType;
private URI name;
private Builder() {
}
/**
* Fill a builder with attribute values from the provided {@code FormatDescriptionType} 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(FormatDescriptionType instance) {
Objects.requireNonNull(instance, "instance");
setDescription(instance.description());
setMimeType(instance.mimeType());
setName(instance.name());
return this;
}
/**
* Initializes the value for the {@link FormatDescriptionType#description() description} attribute.
* @param description The value for description
* @return {@code this} builder for use in a chained invocation
*/
public final Builder setDescription(String description) {
this.description = Objects.requireNonNull(description, "description");
initBits &= ~INIT_BIT_DESCRIPTION;
return this;
}
/**
* Initializes the value for the {@link FormatDescriptionType#mimeType() mimeType} attribute.
* @param mimeType The value for mimeType
* @return {@code this} builder for use in a chained invocation
*/
public final Builder setMimeType(String mimeType) {
this.mimeType = Objects.requireNonNull(mimeType, "mimeType");
initBits &= ~INIT_BIT_MIME_TYPE;
return this;
}
/**
* Initializes the value for the {@link FormatDescriptionType#name() name} attribute.
* @param name The value for name
* @return {@code this} builder for use in a chained invocation
*/
public final Builder setName(URI name) {
this.name = Objects.requireNonNull(name, "name");
initBits &= ~INIT_BIT_NAME;
return this;
}
/**
* Builds a new {@link FormatDescription FormatDescription}.
* @return An immutable instance of FormatDescription
* @throws java.lang.IllegalStateException if any required attributes are missing
*/
public FormatDescription build() {
if (initBits != 0) {
throw new IllegalStateException(formatRequiredAttributesMessage());
}
return new FormatDescription(description, mimeType, name);
}
private String formatRequiredAttributesMessage() {
List attributes = new ArrayList<>();
if ((initBits & INIT_BIT_DESCRIPTION) != 0) attributes.add("description");
if ((initBits & INIT_BIT_MIME_TYPE) != 0) attributes.add("mimeType");
if ((initBits & INIT_BIT_NAME) != 0) attributes.add("name");
return "Cannot build FormatDescription, some of required attributes are not set " + attributes;
}
}
}