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

org.immutables.fixture.GsonAdaptersSillyTuplie Maven / Gradle / Ivy

package org.immutables.fixture;

import com.google.common.base.Optional;
import com.google.common.collect.ImmutableSet;
import com.google.gson.*;
import com.google.gson.reflect.*;
import com.google.gson.stream.*;
import java.io.IOException;
import javax.annotation.Generated;
import javax.annotation.ParametersAreNonnullByDefault;

/**
 * A {@code TypeAdapterFactory} that handles all of the immutable types generated under {@code SillyTuplie}.
 * @see ImmutableSillyTuplie
 */
@SuppressWarnings("all")
@Generated({"Gsons.generator", "org.immutables.fixture.SillyTuplie"})
@ParametersAreNonnullByDefault
public final class GsonAdaptersSillyTuplie implements TypeAdapterFactory {
  @SuppressWarnings({"unchecked", "raw"}) // safe unchecked, types are verified in runtime
  @Override
  public  TypeAdapter create(Gson gson, TypeToken type) {
    if (SillyTuplieTypeAdapter.adapts(type)) {
      return (TypeAdapter) new SillyTuplieTypeAdapter(gson);
    }
    return null;
  }

  @Override
  public String toString() {
    return "GsonAdaptersSillyTuplie(SillyTuplie)";
  }

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

    SillyTuplieTypeAdapter(Gson gson) {
    } 

    static boolean adapts(TypeToken type) {
      return SillyTuplie.class == type.getRawType()
          || ImmutableSillyTuplie.class == type.getRawType();
    }

    @Override
    public void write(JsonWriter out, SillyTuplie value) throws IOException {
      if (value == null) {
        out.nullValue();
      } else {
        writeSillyTuplie(out, value);
      }
    }

    @Override
    public SillyTuplie read(JsonReader in) throws IOException {
      if (in.peek() == JsonToken.NULL) {
        in.nextNull();
        return null;
      }
      return readSillyTuplie(in);
    }

    private void writeSillyTuplie(JsonWriter out, SillyTuplie instance)
        throws IOException {
      out.beginArray();
      out.value(instance.float1());
      Optional opt2Optional = instance.opt2();
      if (opt2Optional.isPresent()) {
        byte opt2Value = opt2Optional.get();
        out.value(opt2Value);
      } else {
        out.nullValue();
      }
      out.beginArray();
      for (boolean e : instance.set3()) {
        out.value(e);
      }
      out.endArray();
      out.endArray();
    }

    private SillyTuplie readSillyTuplie(JsonReader in)
        throws IOException {
      in.beginArray();
      SillyTuplie instance = ImmutableSillyTuplie.of(
        readParameterFloat1(in),
        readParameterOpt2(in),
        readParameterSet3(in));
      in.endArray();
      return instance;
    }

    private float readParameterFloat1(JsonReader in)
        throws IOException {
      return (float) in.nextDouble();
    }

    private Optional readParameterOpt2(JsonReader in)
        throws IOException {
      JsonToken t = in.peek();
      if (t == JsonToken.NULL) {
        in.nextNull();
        return Optional.absent();
      }
      return Optional.of((byte) in.nextInt());
    }

    private Iterable readParameterSet3(JsonReader in)
        throws IOException {
      ImmutableSet.Builder elements = ImmutableSet.builder();
      if (in.peek() == JsonToken.BEGIN_ARRAY) {
        in.beginArray();
        while(in.hasNext()) {
          elements.add(in.nextBoolean());
        }
        in.endArray();
      } else if (in.peek() == JsonToken.NULL) {
        in.nextNull();
      } else {
        elements.add(in.nextBoolean());
      }
      return elements.build();
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy