org.immutables.gson.stream.GsonProviderOptionsBuilder Maven / Gradle / Ivy
package org.immutables.gson.stream;
import com.google.gson.Gson;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import javax.annotation.Generated;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import javax.annotation.concurrent.Immutable;
import javax.annotation.concurrent.NotThreadSafe;
import javax.ws.rs.core.MediaType;
/**
* Builds instances of type {@link GsonMessageBodyProvider.GsonProviderOptions GsonProviderOptions}.
* Initialize attributes and then invoke the {@link #build()} method to create an
* immutable instance.
* {@code GsonProviderOptionsBuilder} is not thread-safe and generally should not be stored in a field or collection,
* but instead used immediately to create instances.
*/
@SuppressWarnings("all")
@ParametersAreNonnullByDefault
@Generated({"Immutables.generator", "GsonMessageBodyProvider.GsonProviderOptions"})
@NotThreadSafe
public final class GsonProviderOptionsBuilder {
private static final long OPT_BIT_ALLOW_JACKSON = 0x1L;
private static final long OPT_BIT_LENIENT = 0x2L;
private long optBits;
private @Nullable Gson gson;
private boolean allowJackson;
private boolean lenient;
private List mediaTypes = new ArrayList();
/**
* Creates a builder for {@link GsonMessageBodyProvider.GsonProviderOptions GsonProviderOptions} instances.
*/
public GsonProviderOptionsBuilder() {
}
/**
* Fill a builder with attribute values from the provided {@code GsonProviderOptions} 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 GsonProviderOptionsBuilder from(GsonMessageBodyProvider.GsonProviderOptions instance) {
Objects.requireNonNull(instance, "instance");
gson(instance.gson());
allowJackson(instance.allowJackson());
lenient(instance.lenient());
addAllMediaTypes(instance.mediaTypes());
return this;
}
/**
* Initializes the value for the {@link GsonMessageBodyProvider.GsonProviderOptions#gson() gson} attribute.
* If not set, this attribute will have a default value as returned by the initializer of {@link GsonMessageBodyProvider.GsonProviderOptions#gson() gson}.
* @param gson The value for gson
* @return {@code this} builder for use in a chained invocation
*/
public final GsonProviderOptionsBuilder gson(Gson gson) {
this.gson = Objects.requireNonNull(gson, "gson");
return this;
}
/**
* Initializes the value for the {@link GsonMessageBodyProvider.GsonProviderOptions#allowJackson() allowJackson} attribute.
*
If not set, this attribute will have a default value as returned by the initializer of {@link GsonMessageBodyProvider.GsonProviderOptions#allowJackson() allowJackson}.
* @param allowJackson The value for allowJackson
* @return {@code this} builder for use in a chained invocation
*/
public final GsonProviderOptionsBuilder allowJackson(boolean allowJackson) {
this.allowJackson = allowJackson;
optBits |= OPT_BIT_ALLOW_JACKSON;
return this;
}
/**
* Initializes the value for the {@link GsonMessageBodyProvider.GsonProviderOptions#lenient() lenient} attribute.
*
If not set, this attribute will have a default value as returned by the initializer of {@link GsonMessageBodyProvider.GsonProviderOptions#lenient() lenient}.
* @param lenient The value for lenient
* @return {@code this} builder for use in a chained invocation
*/
public final GsonProviderOptionsBuilder lenient(boolean lenient) {
this.lenient = lenient;
optBits |= OPT_BIT_LENIENT;
return this;
}
/**
* Adds one element to {@link GsonMessageBodyProvider.GsonProviderOptions#mediaTypes() mediaTypes} list.
* @param element A mediaTypes element
* @return {@code this} builder for use in a chained invocation
*/
public final GsonProviderOptionsBuilder addMediaTypes(MediaType element) {
this.mediaTypes.add(Objects.requireNonNull(element, "mediaTypes element"));
return this;
}
/**
* Adds elements to {@link GsonMessageBodyProvider.GsonProviderOptions#mediaTypes() mediaTypes} list.
* @param elements An array of mediaTypes elements
* @return {@code this} builder for use in a chained invocation
*/
public final GsonProviderOptionsBuilder addMediaTypes(MediaType... elements) {
for (MediaType element : elements) {
this.mediaTypes.add(Objects.requireNonNull(element, "mediaTypes element"));
}
return this;
}
/**
* Sets or replaces all elements for {@link GsonMessageBodyProvider.GsonProviderOptions#mediaTypes() mediaTypes} list.
* @param elements An iterable of mediaTypes elements
* @return {@code this} builder for use in a chained invocation
*/
public final GsonProviderOptionsBuilder mediaTypes(Iterable extends MediaType> elements) {
this.mediaTypes.clear();
return addAllMediaTypes(elements);
}
/**
* Adds elements to {@link GsonMessageBodyProvider.GsonProviderOptions#mediaTypes() mediaTypes} list.
* @param elements An iterable of mediaTypes elements
* @return {@code this} builder for use in a chained invocation
*/
public final GsonProviderOptionsBuilder addAllMediaTypes(Iterable extends MediaType> elements) {
for (MediaType element : elements) {
this.mediaTypes.add(Objects.requireNonNull(element, "mediaTypes element"));
}
return this;
}
/**
* Builds a new {@link GsonMessageBodyProvider.GsonProviderOptions GsonProviderOptions}.
* @return An immutable instance of GsonProviderOptions
* @throws java.lang.IllegalStateException if any required attributes are missing
*/
public GsonMessageBodyProvider.GsonProviderOptions build() {
return GsonProviderOptionsBuilder.ImmutableGsonProviderOptions.validate(new GsonProviderOptionsBuilder.ImmutableGsonProviderOptions(this));
}
private boolean allowJacksonIsSet() {
return (optBits & OPT_BIT_ALLOW_JACKSON) != 0;
}
private boolean lenientIsSet() {
return (optBits & OPT_BIT_LENIENT) != 0;
}
/**
* Immutable implementation of {@link GsonMessageBodyProvider.GsonProviderOptions}.
*
* Use the builder to create immutable instances:
* {@code new GsonProviderOptionsBuilder()}.
* Use the static factory method to get the default singleton instance:
* {@code GsonProviderOptionsBuilder.ImmutableGsonProviderOptions.of()}.
*/
@Immutable
private static final class ImmutableGsonProviderOptions
extends GsonMessageBodyProvider.GsonProviderOptions {
private final Gson gson;
private final boolean allowJackson;
private final boolean lenient;
private final List mediaTypes;
private ImmutableGsonProviderOptions() {
this.mediaTypes = Collections.emptyList();
this.gson = initShim.gson();
this.allowJackson = initShim.allowJackson();
this.lenient = initShim.lenient();
this.initShim = null;
}
private ImmutableGsonProviderOptions(GsonProviderOptionsBuilder builder) {
this.mediaTypes = createUnmodifiableList(true, builder.mediaTypes);
if (builder.gson != null) {
initShim.gson(builder.gson);
}
if (builder.allowJacksonIsSet()) {
initShim.allowJackson(builder.allowJackson);
}
if (builder.lenientIsSet()) {
initShim.lenient(builder.lenient);
}
this.gson = initShim.gson();
this.allowJackson = initShim.allowJackson();
this.lenient = initShim.lenient();
this.initShim = null;
}
private static final int STAGE_INITIALIZING = -1;
private static final int STAGE_UNINITIALIZED = 0;
private static final int STAGE_INITIALIZED = 1;
private transient volatile InitShim initShim = new InitShim();
private final class InitShim {
private Gson gson;
private int gsonStage;
Gson gson() {
if (gsonStage == STAGE_INITIALIZING) throw new IllegalStateException(formatInitCycleMessage());
if (gsonStage == STAGE_UNINITIALIZED) {
gsonStage = STAGE_INITIALIZING;
this.gson = Objects.requireNonNull(ImmutableGsonProviderOptions.super.gson(), "gson");
gsonStage = STAGE_INITIALIZED;
}
return this.gson;
}
void gson(Gson gson) {
this.gson = gson;
gsonStage = STAGE_INITIALIZED;
}
private boolean allowJackson;
private int allowJacksonStage;
boolean allowJackson() {
if (allowJacksonStage == STAGE_INITIALIZING) throw new IllegalStateException(formatInitCycleMessage());
if (allowJacksonStage == STAGE_UNINITIALIZED) {
allowJacksonStage = STAGE_INITIALIZING;
this.allowJackson = ImmutableGsonProviderOptions.super.allowJackson();
allowJacksonStage = STAGE_INITIALIZED;
}
return this.allowJackson;
}
void allowJackson(boolean allowJackson) {
this.allowJackson = allowJackson;
allowJacksonStage = STAGE_INITIALIZED;
}
private boolean lenient;
private int lenientStage;
boolean lenient() {
if (lenientStage == STAGE_INITIALIZING) throw new IllegalStateException(formatInitCycleMessage());
if (lenientStage == STAGE_UNINITIALIZED) {
lenientStage = STAGE_INITIALIZING;
this.lenient = ImmutableGsonProviderOptions.super.lenient();
lenientStage = STAGE_INITIALIZED;
}
return this.lenient;
}
void lenient(boolean lenient) {
this.lenient = lenient;
lenientStage = STAGE_INITIALIZED;
}
private String formatInitCycleMessage() {
ArrayList attributes = new ArrayList();
if (gsonStage == STAGE_INITIALIZING) attributes.add("gson");
if (allowJacksonStage == STAGE_INITIALIZING) attributes.add("allowJackson");
if (lenientStage == STAGE_INITIALIZING) attributes.add("lenient");
return "Cannot build GsonProviderOptions, attribute initializers form cycle" + attributes;
}
}
/**
* @return The value of the {@code gson} attribute
*/
@Override
public Gson gson() {
InitShim shim = this.initShim;
return shim != null
? shim.gson()
: this.gson;
}
/**
* @return The value of the {@code allowJackson} attribute
*/
@Override
public boolean allowJackson() {
InitShim shim = this.initShim;
return shim != null
? shim.allowJackson()
: this.allowJackson;
}
/**
* @return The value of the {@code lenient} attribute
*/
@Override
public boolean lenient() {
InitShim shim = this.initShim;
return shim != null
? shim.lenient()
: this.lenient;
}
/**
* @return The value of the {@code mediaTypes} attribute
*/
@Override
public List mediaTypes() {
return mediaTypes;
}
/**
* This instance is equal to all instances of {@code ImmutableGsonProviderOptions} 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 ImmutableGsonProviderOptions
&& equalTo((ImmutableGsonProviderOptions) another);
}
private boolean equalTo(ImmutableGsonProviderOptions another) {
return gson.equals(another.gson)
&& allowJackson == another.allowJackson
&& lenient == another.lenient
&& mediaTypes.equals(another.mediaTypes);
}
/**
* Computes a hash code from attributes: {@code gson}, {@code allowJackson}, {@code lenient}, {@code mediaTypes}.
* @return hashCode value
*/
@Override
public int hashCode() {
int h = 31;
h = h * 17 + gson.hashCode();
h = h * 17 + (allowJackson ? 1231 : 1237);
h = h * 17 + (lenient ? 1231 : 1237);
h = h * 17 + mediaTypes.hashCode();
return h;
}
/**
* Prints the immutable value {@code GsonProviderOptions} with attribute values.
* @return A string representation of the value
*/
@Override
public String toString() {
return "GsonProviderOptions{"
+ "gson=" + gson
+ ", allowJackson=" + allowJackson
+ ", lenient=" + lenient
+ ", mediaTypes=" + mediaTypes
+ "}";
}
private static final ImmutableGsonProviderOptions INSTANCE = validate(new ImmutableGsonProviderOptions());
/**
* Returns the default immutable singleton value of {@code GsonProviderOptions}
* @return An immutable instance of GsonProviderOptions
*/
public static GsonMessageBodyProvider.GsonProviderOptions of() {
return INSTANCE;
}
private static ImmutableGsonProviderOptions validate(ImmutableGsonProviderOptions instance) {
return INSTANCE != null && INSTANCE.equalTo(instance) ? INSTANCE : instance;
}
}
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();
} 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);
}
}
}
}