io.protostuff.generator.html.json.enumeration.ImmutableEnumConstant Maven / Gradle / Ivy
package io.protostuff.generator.html.json.enumeration;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableMap;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.errorprone.annotations.Var;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import javax.annotation.CheckReturnValue;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import javax.annotation.concurrent.Immutable;
import javax.annotation.concurrent.NotThreadSafe;
import org.immutables.value.Generated;
/**
* Immutable implementation of {@link EnumConstant}.
*
* Use the builder to create immutable instances:
* {@code ImmutableEnumConstant.builder()}.
*/
@Generated(from = "EnumConstant", generator = "Immutables")
@SuppressWarnings({"all"})
@ParametersAreNonnullByDefault
@javax.annotation.processing.Generated("org.immutables.processor.ProxyProcessor")
@Immutable
@CheckReturnValue
public final class ImmutableEnumConstant implements EnumConstant {
private final String name;
private final @Nullable String description;
private final int value;
private final ImmutableMap options;
private ImmutableEnumConstant(
String name,
@Nullable String description,
int value,
ImmutableMap options) {
this.name = name;
this.description = description;
this.value = value;
this.options = options;
}
/**
* @return The value of the {@code name} attribute
*/
@JsonProperty("name")
@Override
public String name() {
return name;
}
/**
* @return The value of the {@code description} attribute
*/
@JsonProperty("description")
@Override
public @Nullable String description() {
return description;
}
/**
* @return The value of the {@code value} attribute
*/
@JsonProperty("value")
@Override
public int value() {
return value;
}
/**
* @return The value of the {@code options} attribute
*/
@JsonProperty("options")
@Override
public ImmutableMap options() {
return options;
}
/**
* Copy the current immutable object by setting a value for the {@link EnumConstant#name() name} attribute.
* An equals check 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 ImmutableEnumConstant withName(String value) {
String newValue = Objects.requireNonNull(value, "name");
if (this.name.equals(newValue)) return this;
return new ImmutableEnumConstant(newValue, this.description, this.value, this.options);
}
/**
* Copy the current immutable object by setting a value for the {@link EnumConstant#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 (can be {@code null})
* @return A modified copy of the {@code this} object
*/
public final ImmutableEnumConstant withDescription(@Nullable String value) {
if (Objects.equals(this.description, value)) return this;
return new ImmutableEnumConstant(this.name, value, this.value, this.options);
}
/**
* Copy the current immutable object by setting a value for the {@link EnumConstant#value() value} attribute.
* A value equality check is used to prevent copying of the same value by returning {@code this}.
* @param value A new value for value
* @return A modified copy of the {@code this} object
*/
public final ImmutableEnumConstant withValue(int value) {
if (this.value == value) return this;
return new ImmutableEnumConstant(this.name, this.description, value, this.options);
}
/**
* Copy the current immutable object by replacing the {@link EnumConstant#options() options} map with the specified map.
* Nulls are not permitted as keys or values.
* A shallow reference equality check is used to prevent copying of the same value by returning {@code this}.
* @param entries The entries to be added to the options map
* @return A modified copy of {@code this} object
*/
public final ImmutableEnumConstant withOptions(Map entries) {
if (this.options == entries) return this;
ImmutableMap newValue = ImmutableMap.copyOf(entries);
return new ImmutableEnumConstant(this.name, this.description, this.value, newValue);
}
/**
* This instance is equal to all instances of {@code ImmutableEnumConstant} that have equal attribute values.
* @return {@code true} if {@code this} is equal to {@code another} instance
*/
@Override
public boolean equals(@Nullable Object another) {
if (this == another) return true;
return another instanceof ImmutableEnumConstant
&& equalTo((ImmutableEnumConstant) another);
}
private boolean equalTo(ImmutableEnumConstant another) {
return name.equals(another.name)
&& Objects.equals(description, another.description)
&& value == another.value
&& options.equals(another.options);
}
/**
* Computes a hash code from attributes: {@code name}, {@code description}, {@code value}, {@code options}.
* @return hashCode value
*/
@Override
public int hashCode() {
@Var int h = 5381;
h += (h << 5) + name.hashCode();
h += (h << 5) + Objects.hashCode(description);
h += (h << 5) + value;
h += (h << 5) + options.hashCode();
return h;
}
/**
* Prints the immutable value {@code EnumConstant} with attribute values.
* @return A string representation of the value
*/
@Override
public String toString() {
return MoreObjects.toStringHelper("EnumConstant")
.omitNullValues()
.add("name", name)
.add("description", description)
.add("value", value)
.add("options", options)
.toString();
}
/**
* Utility type used to correctly read immutable object from JSON representation.
* @deprecated Do not use this type directly, it exists only for the Jackson-binding infrastructure
*/
@Generated(from = "EnumConstant", generator = "Immutables")
@Deprecated
@SuppressWarnings("Immutable")
@JsonDeserialize
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.NONE)
static final class Json implements EnumConstant {
@Nullable String name;
@Nullable String description;
int value;
boolean valueIsSet;
@Nullable Map options = ImmutableMap.of();
@JsonProperty("name")
public void setName(String name) {
this.name = name;
}
@JsonProperty("description")
public void setDescription(@Nullable String description) {
this.description = description;
}
@JsonProperty("value")
public void setValue(int value) {
this.value = value;
this.valueIsSet = true;
}
@JsonProperty("options")
public void setOptions(Map options) {
this.options = options;
}
@Override
public String name() { throw new UnsupportedOperationException(); }
@Override
public String description() { throw new UnsupportedOperationException(); }
@Override
public int value() { throw new UnsupportedOperationException(); }
@Override
public Map options() { throw new UnsupportedOperationException(); }
}
/**
* @param json A JSON-bindable data structure
* @return An immutable value type
* @deprecated Do not use this method directly, it exists only for the Jackson-binding infrastructure
*/
@Deprecated
@JsonCreator(mode = JsonCreator.Mode.DELEGATING)
static ImmutableEnumConstant fromJson(Json json) {
ImmutableEnumConstant.Builder builder = ImmutableEnumConstant.builder();
if (json.name != null) {
builder.name(json.name);
}
if (json.description != null) {
builder.description(json.description);
}
if (json.valueIsSet) {
builder.value(json.value);
}
if (json.options != null) {
builder.putAllOptions(json.options);
}
return builder.build();
}
/**
* Creates an immutable copy of a {@link EnumConstant} 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 EnumConstant instance
*/
public static ImmutableEnumConstant copyOf(EnumConstant instance) {
if (instance instanceof ImmutableEnumConstant) {
return (ImmutableEnumConstant) instance;
}
return ImmutableEnumConstant.builder()
.from(instance)
.build();
}
/**
* Creates a builder for {@link ImmutableEnumConstant ImmutableEnumConstant}.
*
* ImmutableEnumConstant.builder()
* .name(String) // required {@link EnumConstant#name() name}
* .description(String | null) // nullable {@link EnumConstant#description() description}
* .value(int) // required {@link EnumConstant#value() value}
* .putOptions|putAllOptions(String => Object) // {@link EnumConstant#options() options} mappings
* .build();
*
* @return A new ImmutableEnumConstant builder
*/
public static ImmutableEnumConstant.Builder builder() {
return new ImmutableEnumConstant.Builder();
}
/**
* Builds instances of type {@link ImmutableEnumConstant ImmutableEnumConstant}.
* 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.
*/
@Generated(from = "EnumConstant", generator = "Immutables")
@NotThreadSafe
public static final class Builder {
private static final long INIT_BIT_NAME = 0x1L;
private static final long INIT_BIT_VALUE = 0x2L;
private long initBits = 0x3L;
private @Nullable String name;
private @Nullable String description;
private int value;
private ImmutableMap.Builder options = ImmutableMap.builder();
private Builder() {
}
/**
* Fill a builder with attribute values from the provided {@code EnumConstant} 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
*/
@CanIgnoreReturnValue
public final Builder from(EnumConstant instance) {
Objects.requireNonNull(instance, "instance");
name(instance.name());
@Nullable String descriptionValue = instance.description();
if (descriptionValue != null) {
description(descriptionValue);
}
value(instance.value());
putAllOptions(instance.options());
return this;
}
/**
* Initializes the value for the {@link EnumConstant#name() name} attribute.
* @param name The value for name
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
@JsonProperty("name")
public final Builder name(String name) {
this.name = Objects.requireNonNull(name, "name");
initBits &= ~INIT_BIT_NAME;
return this;
}
/**
* Initializes the value for the {@link EnumConstant#description() description} attribute.
* @param description The value for description (can be {@code null})
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
@JsonProperty("description")
public final Builder description(@Nullable String description) {
this.description = description;
return this;
}
/**
* Initializes the value for the {@link EnumConstant#value() value} attribute.
* @param value The value for value
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
@JsonProperty("value")
public final Builder value(int value) {
this.value = value;
initBits &= ~INIT_BIT_VALUE;
return this;
}
/**
* Put one entry to the {@link EnumConstant#options() options} map.
* @param key The key in the options map
* @param value The associated value in the options map
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder putOptions(String key, Object value) {
this.options.put(key, value);
return this;
}
/**
* Put one entry to the {@link EnumConstant#options() options} map. Nulls are not permitted
* @param entry The key and value entry
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder putOptions(Map.Entry entry) {
this.options.put(entry);
return this;
}
/**
* Sets or replaces all mappings from the specified map as entries for the {@link EnumConstant#options() options} map. Nulls are not permitted
* @param entries The entries that will be added to the options map
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
@JsonProperty("options")
public final Builder options(Map entries) {
this.options = ImmutableMap.builder();
return putAllOptions(entries);
}
/**
* Put all mappings from the specified map as entries to {@link EnumConstant#options() options} map. Nulls are not permitted
* @param entries The entries that will be added to the options map
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder putAllOptions(Map entries) {
this.options.putAll(entries);
return this;
}
/**
* Builds a new {@link ImmutableEnumConstant ImmutableEnumConstant}.
* @return An immutable instance of EnumConstant
* @throws java.lang.IllegalStateException if any required attributes are missing
*/
public ImmutableEnumConstant build() {
if (initBits != 0) {
throw new IllegalStateException(formatRequiredAttributesMessage());
}
return new ImmutableEnumConstant(name, description, value, options.build());
}
private String formatRequiredAttributesMessage() {
List attributes = new ArrayList<>();
if ((initBits & INIT_BIT_NAME) != 0) attributes.add("name");
if ((initBits & INIT_BIT_VALUE) != 0) attributes.add("value");
return "Cannot build EnumConstant, some of required attributes are not set " + attributes;
}
}
}