org.immutables.fixture.jdkonly.GsonAdaptersUsingAllOptionals Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of value-fixture Show documentation
Show all versions of value-fixture Show documentation
Module that contains all tests for the code generation capability
package org.immutables.fixture.jdkonly;
import com.google.common.base.Optional;
import com.google.gson.*;
import com.google.gson.reflect.*;
import com.google.gson.stream.*;
import io.atlassian.fugue.Option;
import java.io.IOException;
import java.util.OptionalDouble;
import java.util.OptionalInt;
import java.util.OptionalLong;
import javax.annotation.ParametersAreNonnullByDefault;
import javax.annotation.processing.Generated;
/**
* A {@code TypeAdapterFactory} that handles all of the immutable types generated under {@code UsingAllOptionals}.
* @see ImmutableUsingAllOptionals
*/
@SuppressWarnings({"all", "MethodCanBeStatic"})
@ParametersAreNonnullByDefault
@Generated("org.immutables.processor.ProxyProcessor")
@org.immutables.value.Generated(from = "org.immutables.fixture.jdkonly", generator = "Gsons")
public final class GsonAdaptersUsingAllOptionals implements TypeAdapterFactory {
@SuppressWarnings({"unchecked", "rawtypes"}) // safe unchecked, types are verified in runtime
@Override
public TypeAdapter create(Gson gson, TypeToken type) {
if (UsingAllOptionalsTypeAdapter.adapts(type)) {
return (TypeAdapter) new UsingAllOptionalsTypeAdapter(gson);
}
return null;
}
@Override
public String toString() {
return "GsonAdaptersUsingAllOptionals(UsingAllOptionals)";
}
@org.immutables.value.Generated(from = "UsingAllOptionals", generator = "Gsons")
@SuppressWarnings({"unchecked", "rawtypes"}) // safe unchecked, types are verified in runtime
private static class UsingAllOptionalsTypeAdapter extends TypeAdapter {
public final Integer v1TypeSample = null;
public final Integer v2TypeSample = null;
private final TypeAdapter v1TypeAdapter;
private final TypeAdapter v2TypeAdapter;
UsingAllOptionalsTypeAdapter(Gson gson) {
this.v1TypeAdapter = gson.getAdapter( Integer.class);
this.v2TypeAdapter = gson.getAdapter( Integer.class);
}
static boolean adapts(TypeToken> type) {
return UsingAllOptionals.class == type.getRawType()
|| ImmutableUsingAllOptionals.class == type.getRawType();
}
@Override
public void write(JsonWriter out, UsingAllOptionals value) throws IOException {
if (value == null) {
out.nullValue();
} else {
writeUsingAllOptionals(out, value);
}
}
@Override
public UsingAllOptionals read(JsonReader in) throws IOException {
return readUsingAllOptionals(in);
}
private void writeUsingAllOptionals(JsonWriter out, UsingAllOptionals instance)
throws IOException {
out.beginObject();
Optional v1Optional = instance.v1();
if (v1Optional.isPresent()) {
out.name("v1");
Integer v1Value = v1Optional.get();
v1TypeAdapter.write(out, v1Value);
} else if (out.getSerializeNulls()) {
out.name("v1");
out.nullValue();
}
java.util.Optional v2Optional = instance.v2();
if (v2Optional.isPresent()) {
out.name("v2");
Integer v2Value = v2Optional.get();
v2TypeAdapter.write(out, v2Value);
} else if (out.getSerializeNulls()) {
out.name("v2");
out.nullValue();
}
OptionalInt i1Optional = instance.i1();
if (i1Optional.isPresent()) {
out.name("i1");
int i1Value = i1Optional.getAsInt();
out.value(i1Value);
} else if (out.getSerializeNulls()) {
out.name("i1");
out.nullValue();
}
OptionalLong l1Optional = instance.l1();
if (l1Optional.isPresent()) {
out.name("l1");
long l1Value = l1Optional.getAsLong();
out.value(l1Value);
} else if (out.getSerializeNulls()) {
out.name("l1");
out.nullValue();
}
OptionalDouble d1Optional = instance.d1();
if (d1Optional.isPresent()) {
out.name("d1");
double d1Value = d1Optional.getAsDouble();
out.value(d1Value);
} else if (out.getSerializeNulls()) {
out.name("d1");
out.nullValue();
}
Option fo3Optional = instance.fo3();
if (fo3Optional.isDefined()) {
out.name("fo3");
String fo3Value = fo3Optional.get();
out.value(fo3Value);
} else if (out.getSerializeNulls()) {
out.name("fo3");
out.nullValue();
}
javaslang.control.Option jsoOptional = instance.jso();
if (jsoOptional.isDefined()) {
out.name("jso");
String jsoValue = jsoOptional.get();
out.value(jsoValue);
} else if (out.getSerializeNulls()) {
out.name("jso");
out.nullValue();
}
out.endObject();
}
private UsingAllOptionals readUsingAllOptionals(JsonReader in)
throws IOException {
if (in.peek() == JsonToken.NULL) {
in.nextNull();
return null;
}
ImmutableUsingAllOptionals.Builder builder = ImmutableUsingAllOptionals.builder();
in.beginObject();
while (in.hasNext()) {
eachAttribute(in, builder);
}
in.endObject();
return builder.build();
}
private void eachAttribute(JsonReader in, ImmutableUsingAllOptionals.Builder builder)
throws IOException {
String attributeName = in.nextName();
switch (attributeName.charAt(0)) {
case 'v':
if ("v1".equals(attributeName)) {
readInV1(in, builder);
return;
}
if ("v2".equals(attributeName)) {
readInV2(in, builder);
return;
}
break;
case 'i':
if ("i1".equals(attributeName)) {
readInI1(in, builder);
return;
}
break;
case 'l':
if ("l1".equals(attributeName)) {
readInL1(in, builder);
return;
}
break;
case 'd':
if ("d1".equals(attributeName)) {
readInD1(in, builder);
return;
}
break;
case 'f':
if ("fo3".equals(attributeName)) {
readInFo3(in, builder);
return;
}
break;
case 'j':
if ("jso".equals(attributeName)) {
readInJso(in, builder);
return;
}
break;
default:
}
in.skipValue();
}
private void readInV1(JsonReader in, ImmutableUsingAllOptionals.Builder builder)
throws IOException {
if (in.peek() == JsonToken.NULL) {
in.nextNull();
} else {
Integer value = v1TypeAdapter.read(in);
builder.v1(value);
}
}
private void readInV2(JsonReader in, ImmutableUsingAllOptionals.Builder builder)
throws IOException {
if (in.peek() == JsonToken.NULL) {
in.nextNull();
} else {
Integer value = v2TypeAdapter.read(in);
builder.v2(value);
}
}
private void readInI1(JsonReader in, ImmutableUsingAllOptionals.Builder builder)
throws IOException {
if (in.peek() == JsonToken.NULL) {
in.nextNull();
} else {
builder.i1(in.nextInt());
}
}
private void readInL1(JsonReader in, ImmutableUsingAllOptionals.Builder builder)
throws IOException {
if (in.peek() == JsonToken.NULL) {
in.nextNull();
} else {
builder.l1(in.nextLong());
}
}
private void readInD1(JsonReader in, ImmutableUsingAllOptionals.Builder builder)
throws IOException {
if (in.peek() == JsonToken.NULL) {
in.nextNull();
} else {
builder.d1(in.nextDouble());
}
}
private void readInFo3(JsonReader in, ImmutableUsingAllOptionals.Builder builder)
throws IOException {
if (in.peek() == JsonToken.NULL) {
in.nextNull();
} else {
builder.fo3(in.nextString());
}
}
private void readInJso(JsonReader in, ImmutableUsingAllOptionals.Builder builder)
throws IOException {
if (in.peek() == JsonToken.NULL) {
in.nextNull();
} else {
builder.jso(in.nextString());
}
}
}
}