/**
 * 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 MyUnion extends TUnion<MyUnion> {
  private static final TStruct STRUCT_DESC = new TStruct("MyUnion");
  private static final TField MY_ENUM_FIELD_DESC = new TField("myEnum", TType.I32, (short)1);
  private static final TField MY_STRUCT_FIELD_DESC = new TField("myStruct", TType.STRUCT, (short)2);
  private static final TField MY_DATA_ITEM_FIELD_DESC = new TField("myDataItem", TType.STRUCT, (short)3);
  private static final TField COMPLEX_NESTED_STRUCT_FIELD_DESC = new TField("complexNestedStruct", TType.STRUCT, (short)4);
  private static final TField LONG_VALUE_FIELD_DESC = new TField("longValue", TType.I64, (short)5);
  private static final TField INT_VALUE_FIELD_DESC = new TField("intValue", TType.I32, (short)6);

  public static final int MYENUM = 1;
  public static final int MYSTRUCT = 2;
  public static final int MYDATAITEM = 3;
  public static final int COMPLEXNESTEDSTRUCT = 4;
  public static final int LONGVALUE = 5;
  public static final int INTVALUE = 6;

  public static final Map<Integer, FieldMetaData> metaDataMap;

  static {
    Map<Integer, FieldMetaData> tmpMetaDataMap = new HashMap<Integer, FieldMetaData>();
    tmpMetaDataMap.put(MYENUM, new FieldMetaData("myEnum", TFieldRequirementType.DEFAULT, 
        new FieldValueMetaData(TType.I32)));
    tmpMetaDataMap.put(MYSTRUCT, new FieldMetaData("myStruct", TFieldRequirementType.DEFAULT, 
        new StructMetaData(TType.STRUCT, MyStruct.class)));
    tmpMetaDataMap.put(MYDATAITEM, new FieldMetaData("myDataItem", TFieldRequirementType.DEFAULT, 
        new StructMetaData(TType.STRUCT, MyDataItem.class)));
    tmpMetaDataMap.put(COMPLEXNESTEDSTRUCT, new FieldMetaData("complexNestedStruct", TFieldRequirementType.DEFAULT, 
        new StructMetaData(TType.STRUCT, ComplexNestedStruct.class)));
    tmpMetaDataMap.put(LONGVALUE, new FieldMetaData("longValue", TFieldRequirementType.DEFAULT, 
        new FieldValueMetaData(TType.I64)));
    tmpMetaDataMap.put(INTVALUE, new FieldMetaData("intValue", TFieldRequirementType.DEFAULT, 
        new FieldValueMetaData(TType.I32)));
    metaDataMap = Collections.unmodifiableMap(tmpMetaDataMap);
  }

  public MyUnion() {
    super();
  }

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

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

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

  public static MyUnion myEnum(MyEnum __value) {
    MyUnion x = new MyUnion();
    x.setMyEnum(__value);
    return x;
  }

  public static MyUnion myStruct(MyStruct __value) {
    MyUnion x = new MyUnion();
    x.setMyStruct(__value);
    return x;
  }

  public static MyUnion myDataItem(MyDataItem __value) {
    MyUnion x = new MyUnion();
    x.setMyDataItem(__value);
    return x;
  }

  public static MyUnion complexNestedStruct(ComplexNestedStruct __value) {
    MyUnion x = new MyUnion();
    x.setComplexNestedStruct(__value);
    return x;
  }

  public static MyUnion longValue(long __value) {
    MyUnion x = new MyUnion();
    x.setLongValue(__value);
    return x;
  }

  public static MyUnion intValue(int __value) {
    MyUnion x = new MyUnion();
    x.setIntValue(__value);
    return x;
  }


  @Override
  protected void checkType(short setField, Object __value) throws ClassCastException {
    switch (setField) {
      case MYENUM:
        if (__value instanceof MyEnum) {
          break;
        }
        throw new ClassCastException("Was expecting value of type MyEnum for field 'myEnum', but got " + __value.getClass().getSimpleName());
      case MYSTRUCT:
        if (__value instanceof MyStruct) {
          break;
        }
        throw new ClassCastException("Was expecting value of type MyStruct for field 'myStruct', but got " + __value.getClass().getSimpleName());
      case MYDATAITEM:
        if (__value instanceof MyDataItem) {
          break;
        }
        throw new ClassCastException("Was expecting value of type MyDataItem for field 'myDataItem', but got " + __value.getClass().getSimpleName());
      case COMPLEXNESTEDSTRUCT:
        if (__value instanceof ComplexNestedStruct) {
          break;
        }
        throw new ClassCastException("Was expecting value of type ComplexNestedStruct for field 'complexNestedStruct', but got " + __value.getClass().getSimpleName());
      case LONGVALUE:
        if (__value instanceof Long) {
          break;
        }
        throw new ClassCastException("Was expecting value of type Long for field 'longValue', but got " + __value.getClass().getSimpleName());
      case INTVALUE:
        if (__value instanceof Integer) {
          break;
        }
        throw new ClassCastException("Was expecting value of type Integer for field 'intValue', 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 MYENUM:
            if (__field.type == MY_ENUM_FIELD_DESC.type) {
              setField_ = __field.id;
            }
            break;
          case MYSTRUCT:
            if (__field.type == MY_STRUCT_FIELD_DESC.type) {
              setField_ = __field.id;
            }
            break;
          case MYDATAITEM:
            if (__field.type == MY_DATA_ITEM_FIELD_DESC.type) {
              setField_ = __field.id;
            }
            break;
          case COMPLEXNESTEDSTRUCT:
            if (__field.type == COMPLEX_NESTED_STRUCT_FIELD_DESC.type) {
              setField_ = __field.id;
            }
            break;
          case LONGVALUE:
            if (__field.type == LONG_VALUE_FIELD_DESC.type) {
              setField_ = __field.id;
            }
            break;
          case INTVALUE:
            if (__field.type == INT_VALUE_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 'MyUnion' is missing a STOP byte");
      }
    }
    iprot.readStructEnd();
  }

  @Override
  protected Object readValue(TProtocol iprot, TField __field) throws TException {
    switch (__field.id) {
      case MYENUM:
        if (__field.type == MY_ENUM_FIELD_DESC.type) {
          MyEnum myEnum;
          myEnum = MyEnum.findByValue(iprot.readI32());
          return myEnum;
        }
        break;
      case MYSTRUCT:
        if (__field.type == MY_STRUCT_FIELD_DESC.type) {
          MyStruct myStruct;
          myStruct = new MyStruct();
          myStruct.read(iprot);
          return myStruct;
        }
        break;
      case MYDATAITEM:
        if (__field.type == MY_DATA_ITEM_FIELD_DESC.type) {
          MyDataItem myDataItem;
          myDataItem = new MyDataItem();
          myDataItem.read(iprot);
          return myDataItem;
        }
        break;
      case COMPLEXNESTEDSTRUCT:
        if (__field.type == COMPLEX_NESTED_STRUCT_FIELD_DESC.type) {
          ComplexNestedStruct complexNestedStruct;
          complexNestedStruct = new ComplexNestedStruct();
          complexNestedStruct.read(iprot);
          return complexNestedStruct;
        }
        break;
      case LONGVALUE:
        if (__field.type == LONG_VALUE_FIELD_DESC.type) {
          Long longValue;
          longValue = iprot.readI64();
          return longValue;
        }
        break;
      case INTVALUE:
        if (__field.type == INT_VALUE_FIELD_DESC.type) {
          Integer intValue;
          intValue = iprot.readI32();
          return intValue;
        }
        break;
    }
    TProtocolUtil.skip(iprot, __field.type);
    return null;
  }

  @Override
  protected void writeValue(TProtocol oprot, short setField, Object __value) throws TException {
    switch (setField) {
      case MYENUM:
        MyEnum myEnum = (MyEnum)getFieldValue();
        oprot.writeI32(myEnum == null ? 0 : myEnum.getValue());
        return;
      case MYSTRUCT:
        MyStruct myStruct = (MyStruct)getFieldValue();
        myStruct.write(oprot);
        return;
      case MYDATAITEM:
        MyDataItem myDataItem = (MyDataItem)getFieldValue();
        myDataItem.write(oprot);
        return;
      case COMPLEXNESTEDSTRUCT:
        ComplexNestedStruct complexNestedStruct = (ComplexNestedStruct)getFieldValue();
        complexNestedStruct.write(oprot);
        return;
      case LONGVALUE:
        Long longValue = (Long)getFieldValue();
        oprot.writeI64(longValue);
        return;
      case INTVALUE:
        Integer intValue = (Integer)getFieldValue();
        oprot.writeI32(intValue);
        return;
      default:
        throw new IllegalStateException("Cannot write union with unknown field " + setField);
    }
  }

  @Override
  protected TField getFieldDesc(int setField) {
    switch (setField) {
      case MYENUM:
        return MY_ENUM_FIELD_DESC;
      case MYSTRUCT:
        return MY_STRUCT_FIELD_DESC;
      case MYDATAITEM:
        return MY_DATA_ITEM_FIELD_DESC;
      case COMPLEXNESTEDSTRUCT:
        return COMPLEX_NESTED_STRUCT_FIELD_DESC;
      case LONGVALUE:
        return LONG_VALUE_FIELD_DESC;
      case INTVALUE:
        return INT_VALUE_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;
  }

  /**
   * 
   * @see MyEnum
   */
  public MyEnum getMyEnum() {
    return (MyEnum) __getValue(MYENUM);
  }

  /**
   * 
   * @see MyEnum
   */
  public void setMyEnum(MyEnum __value) {
    __setValue(MYENUM, __value);
  }

  public MyStruct getMyStruct() {
    return (MyStruct) __getValue(MYSTRUCT);
  }

  public void setMyStruct(MyStruct __value) {
    __setValue(MYSTRUCT, __value);
  }

  public MyDataItem getMyDataItem() {
    return (MyDataItem) __getValue(MYDATAITEM);
  }

  public void setMyDataItem(MyDataItem __value) {
    __setValue(MYDATAITEM, __value);
  }

  public ComplexNestedStruct getComplexNestedStruct() {
    return (ComplexNestedStruct) __getValue(COMPLEXNESTEDSTRUCT);
  }

  public void setComplexNestedStruct(ComplexNestedStruct __value) {
    __setValue(COMPLEXNESTEDSTRUCT, __value);
  }

  public long getLongValue() {
    return (Long) __getValue(LONGVALUE);
  }

  public void setLongValue(long __value) {
    setField_ = LONGVALUE;
    value_ = __value;
  }

  public int getIntValue() {
    return (Integer) __getValue(INTVALUE);
  }

  public void setIntValue(int __value) {
    setField_ = INTVALUE;
    value_ = __value;
  }

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

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


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

}
