/**
 * Autogenerated by Thrift
 *
 * DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
 *  @generated
 */
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 ValUnion extends TUnion<ValUnion> {
  private static final TStruct STRUCT_DESC = new TStruct("ValUnion");
  private static final TField V1_FIELD_DESC = new TField("v1", TType.STRUCT, (short)1);
  private static final TField V2_FIELD_DESC = new TField("v2", TType.STRUCT, (short)2);

  public static final int V1 = 1;
  public static final int V2 = 2;

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

  public ValUnion() {
    super();
  }

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

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

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

  public static ValUnion v1(Val __value) {
    ValUnion x = new ValUnion();
    x.setV1(__value);
    return x;
  }

  public static ValUnion v2(Val __value) {
    ValUnion x = new ValUnion();
    x.setV2(__value);
    return x;
  }


  @Override
  protected Object readValue(TProtocol iprot, TField __field) throws TException {
    switch (__field.id) {
      case V1:
        if (__field.type == V1_FIELD_DESC.type) {
          Val v1;
          v1 = Val.deserialize(iprot);
          return v1;
        }
        break;
      case V2:
        if (__field.type == V2_FIELD_DESC.type) {
          Val v2;
          v2 = Val.deserialize(iprot);
          return v2;
        }
        break;
    }
    TProtocolUtil.skip(iprot, __field.type);
    return null;
  }

  @Override
  protected void writeValue(TProtocol oprot, short setField, Object __value) throws TException {
    switch (setField) {
      case V1:
        Val v1 = (Val)getFieldValue();
        v1.write(oprot);
        return;
      case V2:
        Val v2 = (Val)getFieldValue();
        v2.write(oprot);
        return;
      default:
        throw new IllegalStateException("Cannot write union with unknown field " + setField);
    }
  }

  @Override
  protected TField getFieldDesc(int setField) {
    switch (setField) {
      case V1:
        return V1_FIELD_DESC;
      case V2:
        return V2_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 Val getV1() {
    return (Val) __getValue(V1);
  }

  public void setV1(Val __value) {
    __setValue(V1, __value);
  }

  public Val getV2() {
    return (Val) __getValue(V2);
  }

  public void setV2(Val __value) {
    __setValue(V2, __value);
  }

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

  public boolean equals(ValUnion other) {
    return equalsNobinaryImpl(other);
  }


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

}
