/**
 * Autogenerated by Thrift
 *
 * DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
 *  @generated
 */
package test.fixtures.adapter;

import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
import java.util.Set;
import java.util.HashSet;
import java.util.Collections;
import java.util.BitSet;
import java.util.Arrays;
import com.facebook.thrift.*;
import com.facebook.thrift.annotations.*;
import com.facebook.thrift.async.*;
import com.facebook.thrift.meta_data.*;
import com.facebook.thrift.server.*;
import com.facebook.thrift.transport.*;
import com.facebook.thrift.protocol.*;

@SuppressWarnings({ "unused", "serial", "unchecked" })
public class Baz extends TUnion<Baz> {
  private static final TStruct STRUCT_DESC = new TStruct("Baz");
  private static final TField INT_FIELD_FIELD_DESC = new TField("intField", TType.I32, (short)1);
  private static final TField SET_FIELD_FIELD_DESC = new TField("setField", TType.SET, (short)4);
  private static final TField MAP_FIELD_FIELD_DESC = new TField("mapField", TType.MAP, (short)6);
  private static final TField BINARY_FIELD_FIELD_DESC = new TField("binaryField", TType.STRING, (short)8);
  private static final TField LONG_FIELD_FIELD_DESC = new TField("longField", TType.I64, (short)9);

  public static final int INTFIELD = 1;
  public static final int SETFIELD = 4;
  public static final int MAPFIELD = 6;
  public static final int BINARYFIELD = 8;
  public static final int LONGFIELD = 9;

  public static final Map<Integer, FieldMetaData> metaDataMap = new HashMap<>();

  public Baz() {
    super();
  }

  public Baz(int setField, Object __value) {
    super(setField, __value);
  }

  public Baz(Baz other) {
    super(other);
  }

  public Baz deepCopy() {
    return new Baz(this);
  }

  public static Baz intField(Integer __value) {
    Baz x = new Baz();
    x.setIntField(__value);
    return x;
  }

  public static Baz setField(Set<String> __value) {
    Baz x = new Baz();
    x.setSetField(__value);
    return x;
  }

  public static Baz mapField(Map<String,List<String>> __value) {
    Baz x = new Baz();
    x.setMapField(__value);
    return x;
  }

  public static Baz binaryField(byte[] __value) {
    Baz x = new Baz();
    x.setBinaryField(__value);
    return x;
  }

  public static Baz longField(Long __value) {
    Baz x = new Baz();
    x.setLongField(__value);
    return x;
  }


  @Override
  protected Object readValue(TProtocol iprot, TField __field) throws TException {
    switch (__field.id) {
      case INTFIELD:
        if (__field.type == INT_FIELD_FIELD_DESC.type) {
          Integer intField;
          intField = iprot.readI32();
          return intField;
        }
        break;
      case SETFIELD:
        if (__field.type == SET_FIELD_FIELD_DESC.type) {
          Set<String> setField;
          {
            TSet _set29 = iprot.readSetBegin();
            setField = new HashSet<String>(Math.max(0, 2*_set29.size));
            for (int _i30 = 0; 
                 (_set29.size < 0) ? iprot.peekSet() : (_i30 < _set29.size); 
                 ++_i30)
            {
              String _elem31;
              _elem31 = iprot.readString();
              setField.add(_elem31);
            }
            iprot.readSetEnd();
          }
          return setField;
        }
        break;
      case MAPFIELD:
        if (__field.type == MAP_FIELD_FIELD_DESC.type) {
          Map<String,List<String>> mapField;
          {
            TMap _map32 = iprot.readMapBegin();
            mapField = new HashMap<String,List<String>>(Math.max(0, 2*_map32.size));
            for (int _i33 = 0; 
                 (_map32.size < 0) ? iprot.peekMap() : (_i33 < _map32.size); 
                 ++_i33)
            {
              String _key34;
              List<String> _val35;
              _key34 = iprot.readString();
              {
                TList _list36 = iprot.readListBegin();
                _val35 = new ArrayList<String>(Math.max(0, _list36.size));
                for (int _i37 = 0; 
                     (_list36.size < 0) ? iprot.peekList() : (_i37 < _list36.size); 
                     ++_i37)
                {
                  String _elem38;
                  _elem38 = iprot.readString();
                  _val35.add(_elem38);
                }
                iprot.readListEnd();
              }
              mapField.put(_key34, _val35);
            }
            iprot.readMapEnd();
          }
          return mapField;
        }
        break;
      case BINARYFIELD:
        if (__field.type == BINARY_FIELD_FIELD_DESC.type) {
          byte[] binaryField;
          binaryField = iprot.readBinary();
          return binaryField;
        }
        break;
      case LONGFIELD:
        if (__field.type == LONG_FIELD_FIELD_DESC.type) {
          Long longField;
          longField = iprot.readI64();
          return longField;
        }
        break;
    }
    TProtocolUtil.skip(iprot, __field.type);
    return null;
  }

  @Override
  protected void writeValue(TProtocol oprot, short setField, Object __value) throws TException {
    switch (setField) {
      case INTFIELD:
        Integer intField = (Integer)getFieldValue();
        oprot.writeI32(intField);
        return;
      case SETFIELD:
        Set<String> setField = (Set<String>)getFieldValue();
        {
          oprot.writeSetBegin(new TSet(TType.STRING, setField.size()));
          for (String _iter39 : setField)          {
            oprot.writeString(_iter39);
          }
          oprot.writeSetEnd();
        }
        return;
      case MAPFIELD:
        Map<String,List<String>> mapField = (Map<String,List<String>>)getFieldValue();
        {
          oprot.writeMapBegin(new TMap(TType.STRING, TType.LIST, mapField.size()));
          for (Map.Entry<String, List<String>> _iter40 : mapField.entrySet())          {
            oprot.writeString(_iter40.getKey());
            {
              oprot.writeListBegin(new TList(TType.STRING, _iter40.getValue().size()));
              for (String _iter41 : _iter40.getValue())              {
                oprot.writeString(_iter41);
              }
              oprot.writeListEnd();
            }
          }
          oprot.writeMapEnd();
        }
        return;
      case BINARYFIELD:
        byte[] binaryField = (byte[])getFieldValue();
        oprot.writeBinary(binaryField);
        return;
      case LONGFIELD:
        Long longField = (Long)getFieldValue();
        oprot.writeI64(longField);
        return;
      default:
        throw new IllegalStateException("Cannot write union with unknown field " + setField);
    }
  }

  @Override
  protected TField getFieldDesc(int setField) {
    switch (setField) {
      case INTFIELD:
        return INT_FIELD_FIELD_DESC;
      case SETFIELD:
        return SET_FIELD_FIELD_DESC;
      case MAPFIELD:
        return MAP_FIELD_FIELD_DESC;
      case BINARYFIELD:
        return BINARY_FIELD_FIELD_DESC;
      case LONGFIELD:
        return LONG_FIELD_FIELD_DESC;
      default:
        throw new IllegalArgumentException("Unknown field id " + setField);
    }
  }

  @Override
  protected TStruct getStructDesc() {
    return STRUCT_DESC;
  }

  @Override
  protected Map<Integer, FieldMetaData> getMetaDataMap() { return metaDataMap; }

  private Object __getValue(int expectedFieldId) {
    if (getSetField() == expectedFieldId) {
      return getFieldValue();
    } else {
      throw new RuntimeException("Cannot get field '" + getFieldDesc(expectedFieldId).name + "' because union is currently set to " + getFieldDesc(getSetField()).name);
    }
  }

  private void __setValue(int fieldId, Object __value) {
    if (__value == null) throw new NullPointerException();
    setField_ = fieldId;
    value_ = __value;
  }

  public Integer getIntField() {
    return (Integer) __getValue(INTFIELD);
  }

  public void setIntField(Integer __value) {
    __setValue(INTFIELD, __value);
  }

  public Set<String> getSetField() {
    return (Set<String>) __getValue(SETFIELD);
  }

  public void setSetField(Set<String> __value) {
    __setValue(SETFIELD, __value);
  }

  public Map<String,List<String>> getMapField() {
    return (Map<String,List<String>>) __getValue(MAPFIELD);
  }

  public void setMapField(Map<String,List<String>> __value) {
    __setValue(MAPFIELD, __value);
  }

  public byte[] getBinaryField() {
    return (byte[]) __getValue(BINARYFIELD);
  }

  public void setBinaryField(byte[] __value) {
    __setValue(BINARYFIELD, __value);
  }

  public Long getLongField() {
    return (Long) __getValue(LONGFIELD);
  }

  public void setLongField(Long __value) {
    __setValue(LONGFIELD, __value);
  }

  public boolean equals(Object other) {
    if (other instanceof Baz) {
      return equals((Baz)other);
    } else {
      return false;
    }
  }

  public boolean equals(Baz other) {
    return equalsSlowImpl(other);
  }


  @Override
  public int hashCode() {
    return Arrays.deepHashCode(new Object[] {getSetField(), getFieldValue()});
  }

}
