All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.immutables.fixture.jdkonly.GsonAdaptersUsingAllOptionals Maven / Gradle / Ivy

package org.immutables.fixture.jdkonly;

import com.atlassian.fugue.Option;
import com.google.common.base.Optional;
import com.google.gson.*;
import com.google.gson.reflect.*;
import com.google.gson.stream.*;
import java.io.IOException;
import java.util.OptionalDouble;
import java.util.OptionalInt;
import java.util.OptionalLong;
import javax.annotation.Generated;
import javax.annotation.ParametersAreNonnullByDefault;

/**
 * A {@code TypeAdapterFactory} that handles all of the immutable types generated under {@code UsingAllOptionals}.
 * @see ImmutableUsingAllOptionals
 */
@SuppressWarnings("all")
@Generated({"Gsons.generator", "org.immutables.fixture.jdkonly.UsingAllOptionals"})
@ParametersAreNonnullByDefault
public final class GsonAdaptersUsingAllOptionals implements TypeAdapterFactory {
  @SuppressWarnings({"unchecked", "raw"}) // 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)";
  }

  @SuppressWarnings({"unchecked", "raw"}) // safe unchecked, types are verified in runtime
  private static class UsingAllOptionalsTypeAdapter extends TypeAdapter {

    UsingAllOptionalsTypeAdapter(Gson gson) {
    } 

    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 {
      if (in.peek() == JsonToken.NULL) {
        in.nextNull();
        return null;
      }
      return readUsingAllOptionals(in);
    }

    private void writeUsingAllOptionals(JsonWriter out, UsingAllOptionals instance)
        throws IOException {
      out.beginObject();
      Optional v1Optional = instance.v1();
      if (v1Optional.isPresent()) {
        out.name("v1");
        int v1Value = v1Optional.get();
        out.value(v1Value);
      } else if (out.getSerializeNulls()) {
        out.name("v1");
        out.nullValue();
      }
      java.util.Optional v2Optional = instance.v2();
      if (v2Optional.isPresent()) {
        out.name("v2");
        int v2Value = v2Optional.get();
        out.value(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 fo2Optional = instance.fo2();
      if (fo2Optional.isDefined()) {
        out.name("fo2");
        String fo2Value = fo2Optional.get();
        out.value(fo2Value);
      } else if (out.getSerializeNulls()) {
        out.name("fo2");
        out.nullValue();
      }
      io.atlassian.fugue.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 {
      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 ("fo2".equals(attributeName)) {
          readInFo2(in, builder);
          return;
        }
        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 {
        builder.v1(in.nextInt());
      }
    }

    private void readInV2(JsonReader in, ImmutableUsingAllOptionals.Builder builder)
        throws IOException {
      if (in.peek() == JsonToken.NULL) {
        in.nextNull();
      } else {
        builder.v2(in.nextInt());
      }
    }

    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 readInFo2(JsonReader in, ImmutableUsingAllOptionals.Builder builder)
        throws IOException {
      if (in.peek() == JsonToken.NULL) {
        in.nextNull();
      } else {
        builder.fo2(in.nextString());
      }
    }

    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());
      }
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy