Skip to content

Instantly share code, notes, and snippets.

@elucash
Created May 21, 2016 01:41
Show Gist options
  • Save elucash/160ac106ec02a67ffa6d8b8b9023def4 to your computer and use it in GitHub Desktop.
Save elucash/160ac106ec02a67ffa6d8b8b9023def4 to your computer and use it in GitHub Desktop.
Jackson works with 2.2
package gasdf;
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.base.Objects;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import java.util.List;
import javax.annotation.Generated;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import javax.annotation.concurrent.Immutable;
import javax.annotation.concurrent.NotThreadSafe;
/**
* Immutable implementation of {@link Zone}.
* <p>
* Use the builder to create immutable instances:
* {@code ImmutableZone.builder()}.
*/
@SuppressWarnings("all")
@ParametersAreNonnullByDefault
@Generated({"Immutables.generator", "Zone"})
@Immutable
public final class ImmutableZone extends Zone {
private final @Nullable Long id;
private final String state;
private ImmutableZone(@Nullable Long id, String state) {
this.id = id;
this.state = state;
}
/**
* @return The value of the {@code id} attribute
*/
@JsonProperty
@Override
public @Nullable Long getId() {
return id;
}
/**
* @return The value of the {@code state} attribute
*/
@JsonProperty
@Override
public String getState() {
return state;
}
/**
* Copy the current immutable object by setting a value for the {@link Zone#getId() id} attribute.
* An equals check used to prevent copying of the same value by returning {@code this}.
* @param id A new value for id (can be {@code null})
* @return A modified copy of the {@code this} object
*/
public final ImmutableZone withId(@Nullable Long id) {
if (Objects.equal(this.id, id)) return this;
return new ImmutableZone(id, this.state);
}
/**
* Copy the current immutable object by setting a value for the {@link Zone#getState() state} attribute.
* An equals check used to prevent copying of the same value by returning {@code this}.
* @param state A new value for state
* @return A modified copy of the {@code this} object
*/
public final ImmutableZone withState(String state) {
if (this.state.equals(state)) return this;
String newValue = Preconditions.checkNotNull(state, "state");
return new ImmutableZone(this.id, newValue);
}
/**
* This instance is equal to all instances of {@code ImmutableZone} 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 ImmutableZone
&& equalTo((ImmutableZone) another);
}
private boolean equalTo(ImmutableZone another) {
return Objects.equal(id, another.id)
&& state.equals(another.state);
}
/**
* Computes a hash code from attributes: {@code id}, {@code state}.
* @return hashCode value
*/
@Override
public int hashCode() {
int h = 31;
h = h * 17 + Objects.hashCode(id);
h = h * 17 + state.hashCode();
return h;
}
/**
* Prints the immutable value {@code Zone} with attribute values.
* @return A string representation of the value
*/
@Override
public String toString() {
return MoreObjects.toStringHelper("Zone")
.omitNullValues()
.add("id", id)
.add("state", state)
.toString();
}
/**
* Utility type used to correctly read immutable object from JSON representation.
* @deprecated Do not use this type directly, it exists only for the <em>Jackson</em>-binding infrastructure
*/
@Deprecated
@JsonDeserialize
static final class Json extends Zone {
@Nullable Long id;
@Nullable String state;
@JsonProperty
public void setId(@Nullable Long id) {
this.id = id;
}
@JsonProperty
public void setState(String state) {
this.state = state;
}
@Override
public Long getId() { throw new UnsupportedOperationException(); }
@Override
public String getState() { 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 <em>Jackson</em>-binding infrastructure
*/
@Deprecated
@JsonCreator
static ImmutableZone fromJson(Json json) {
ImmutableZone.Builder builder = ImmutableZone.builder();
if (json.id != null) {
builder.id(json.id);
}
if (json.state != null) {
builder.state(json.state);
}
return builder.build();
}
/**
* Creates an immutable copy of a {@link Zone} 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 Zone instance
*/
public static ImmutableZone copyOf(Zone instance) {
if (instance instanceof ImmutableZone) {
return (ImmutableZone) instance;
}
return ImmutableZone.builder()
.from(instance)
.build();
}
/**
* Creates a builder for {@link ImmutableZone ImmutableZone}.
* @return A new ImmutableZone builder
*/
public static ImmutableZone.Builder builder() {
return new ImmutableZone.Builder();
}
/**
* Builds instances of type {@link ImmutableZone ImmutableZone}.
* Initialize attributes and then invoke the {@link #build()} method to create an
* immutable instance.
* <p><em>{@code Builder} is not thread-safe and generally should not be stored in a field or collection,
* but instead used immediately to create instances.</em>
*/
@NotThreadSafe
public static final class Builder {
private static final long INIT_BIT_STATE = 0x1L;
private long initBits = 0x1L;
private @Nullable Long id;
private @Nullable String state;
private Builder() {
}
/**
* Fill a builder with attribute values from the provided {@code Zone} 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(Zone instance) {
Preconditions.checkNotNull(instance, "instance");
@Nullable Long idValue = instance.getId();
if (idValue != null) {
id(idValue);
}
state(instance.getState());
return this;
}
/**
* Initializes the value for the {@link Zone#getId() id} attribute.
* @param id The value for id (can be {@code null})
* @return {@code this} builder for use in a chained invocation
*/
public final Builder id(@Nullable Long id) {
this.id = id;
return this;
}
/**
* Initializes the value for the {@link Zone#getState() state} attribute.
* @param state The value for state
* @return {@code this} builder for use in a chained invocation
*/
public final Builder state(String state) {
this.state = Preconditions.checkNotNull(state, "state");
initBits &= ~INIT_BIT_STATE;
return this;
}
/**
* Builds a new {@link ImmutableZone ImmutableZone}.
* @return An immutable instance of Zone
* @throws java.lang.IllegalStateException if any required attributes are missing
*/
public ImmutableZone build() {
if (initBits != 0) {
throw new IllegalStateException(formatRequiredAttributesMessage());
}
return new ImmutableZone(id, state);
}
private String formatRequiredAttributesMessage() {
List<String> attributes = Lists.newArrayList();
if ((initBits & INIT_BIT_STATE) != 0) attributes.add("state");
return "Cannot build Zone, some of required attributes are not set " + attributes;
}
}
}
package gasdf;
import javax.annotation.Nullable;
import org.immutables.gson.Gson;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import org.immutables.value.Value.*;
@Immutable
@Gson.TypeAdapters
@JsonSerialize(as = ImmutableZone.class)
@JsonDeserialize(as = ImmutableZone.class)
@Style(visibility = Style.ImplementationVisibility.PUBLIC)
public abstract class Zone {
@Nullable
public abstract Long getId();
public abstract String getState();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment