/**
 * 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> implements Comparable<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;

  static {
    Map<Integer, FieldMetaData> tmpMetaDataMap = new HashMap<Integer, FieldMetaData>();
    tmpMetaDataMap.put(V1, new FieldMetaData("v1", TFieldRequirementType.DEFAULT, 
        new StructMetaData(TType.STRUCT, Val.class)));
    tmpMetaDataMap.put(V2, new FieldMetaData("v2", TFieldRequirementType.DEFAULT, 
        new StructMetaData(TType.STRUCT, Val.class)));
    metaDataMap = Collections.unmodifiableMap(tmpMetaDataMap);
  }

  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 void checkType(short setField, Object __value) throws ClassCastException {
    switch (setField) {
      case V1:
        if (__value instanceof Val) {
          break;
        }
        throw new ClassCastException("Was expecting value of type Val for field 'v1', but got " + __value.getClass().getSimpleName());
      case V2:
        if (__value instanceof Val) {
          break;
        }
        throw new ClassCastException("Was expecting value of type Val for field 'v2', but got " + __value.getClass().getSimpleName());
      default:
        throw new IllegalArgumentException("Unknown field id " + setField);
    }
  }

  @Override
  public void read(TProtocol iprot) throws TException {
    setField_ = 0;
    value_ = null;
    iprot.readStructBegin(metaDataMap);
    TField __field = iprot.readFieldBegin();
    if (__field.type != TType.STOP)
    {
      value_ = readValue(iprot, __field);
      if (value_ != null)
      {
        switch (__field.id) {
          case V1:
            if (__field.type == V1_FIELD_DESC.type) {
              setField_ = __field.id;
            }
            break;
          case V2:
            if (__field.type == V2_FIELD_DESC.type) {
              setField_ = __field.id;
            }
            break;
        }
      }
      iprot.readFieldEnd();
      TField __stopField = iprot.readFieldBegin();
      if (__stopField.type != TType.STOP) {
        throw new TProtocolException(TProtocolException.INVALID_DATA, "Union 'ValUnion' is missing a STOP byte");
      }
    }
    iprot.readStructEnd();
  }

  @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 = new Val();
          v1.read(iprot);
          return v1;
        }
        break;
      case V2:
        if (__field.type == V2_FIELD_DESC.type) {
          Val v2;
          v2 = new Val();
          v2.read(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 compareTo(ValUnion other) {
    return compareToImpl(other);
  }


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

}
