/*******************************************************************************
 * Copyright (c) 2015, 2016 CNRS, IRIT, IRT AESE (IRT Saint Exupéry).
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the LGPL 3.0
 * which accompanies this distribution, and is available at
 * https://www.gnu.org/licenses/lgpl-3.0-standalone.html
 *
 * Contributors:
 *      Faiez Zalila - initial API and implementation and/or initial documentation
 *******************************************************************************/
/**
 * Autogenerated by Thrift Compiler (0.9.2)
 *
 * DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
 *  @generated
 */
package tts;

import java.util.ArrayList;
import java.util.BitSet;
import java.util.Collections;
import java.util.EnumMap;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Generated;

import org.apache.thrift.EncodingUtils;
import org.apache.thrift.protocol.TTupleProtocol;
import org.apache.thrift.scheme.IScheme;
import org.apache.thrift.scheme.SchemeFactory;
import org.apache.thrift.scheme.StandardScheme;
import org.apache.thrift.scheme.TupleScheme;

@SuppressWarnings({"cast", "rawtypes", "serial", "unchecked"})
@Generated(value = "Autogenerated by Thrift Compiler (0.9.2)", date = "2016-2-8")
public class Interval implements org.apache.thrift.TBase<Interval, Interval._Fields>, java.io.Serializable, Cloneable, Comparable<Interval> {
  private static final org.apache.thrift.protocol.TStruct STRUCT_DESC = new org.apache.thrift.protocol.TStruct("Interval");

  private static final org.apache.thrift.protocol.TField OPEN_LOW_FIELD_DESC = new org.apache.thrift.protocol.TField("open_low", org.apache.thrift.protocol.TType.BOOL, (short)1);
  private static final org.apache.thrift.protocol.TField LOW_FIELD_DESC = new org.apache.thrift.protocol.TField("low", org.apache.thrift.protocol.TType.STRUCT, (short)2);
  private static final org.apache.thrift.protocol.TField OPEN_HIGH_FIELD_DESC = new org.apache.thrift.protocol.TField("open_high", org.apache.thrift.protocol.TType.BOOL, (short)3);
  private static final org.apache.thrift.protocol.TField HIGH_FIELD_DESC = new org.apache.thrift.protocol.TField("high", org.apache.thrift.protocol.TType.STRUCT, (short)4);

  private static final Map<Class<? extends IScheme>, SchemeFactory> schemes = new HashMap<Class<? extends IScheme>, SchemeFactory>();
  static {
    schemes.put(StandardScheme.class, new IntervalStandardSchemeFactory());
    schemes.put(TupleScheme.class, new IntervalTupleSchemeFactory());
  }

  public boolean open_low; // required
  public Clock low; // required
  public boolean open_high; // required
  public Clock high; // optional

  /** The set of fields this struct contains, along with convenience methods for finding and manipulating them. */
  public enum _Fields implements org.apache.thrift.TFieldIdEnum {
    OPEN_LOW((short)1, "open_low"),
    LOW((short)2, "low"),
    OPEN_HIGH((short)3, "open_high"),
    HIGH((short)4, "high");

    private static final Map<String, _Fields> byName = new HashMap<String, _Fields>();

    static {
      for (_Fields field : EnumSet.allOf(_Fields.class)) {
        byName.put(field.getFieldName(), field);
      }
    }

    /**
     * Find the _Fields constant that matches fieldId, or null if its not found.
     */
    public static _Fields findByThriftId(int fieldId) {
      switch(fieldId) {
        case 1: // OPEN_LOW
          return OPEN_LOW;
        case 2: // LOW
          return LOW;
        case 3: // OPEN_HIGH
          return OPEN_HIGH;
        case 4: // HIGH
          return HIGH;
        default:
          return null;
      }
    }

    /**
     * Find the _Fields constant that matches fieldId, throwing an exception
     * if it is not found.
     */
    public static _Fields findByThriftIdOrThrow(int fieldId) {
      _Fields fields = findByThriftId(fieldId);
      if (fields == null) throw new IllegalArgumentException("Field " + fieldId + " doesn't exist!");
      return fields;
    }

    /**
     * Find the _Fields constant that matches name, or null if its not found.
     */
    public static _Fields findByName(String name) {
      return byName.get(name);
    }

    private final short _thriftId;
    private final String _fieldName;

    _Fields(short thriftId, String fieldName) {
      _thriftId = thriftId;
      _fieldName = fieldName;
    }

    public short getThriftFieldId() {
      return _thriftId;
    }

    public String getFieldName() {
      return _fieldName;
    }
  }

  // isset id assignments
  private static final int __OPEN_LOW_ISSET_ID = 0;
  private static final int __OPEN_HIGH_ISSET_ID = 1;
  private byte __isset_bitfield = 0;
  private static final _Fields optionals[] = {_Fields.HIGH};
  public static final Map<_Fields, org.apache.thrift.meta_data.FieldMetaData> metaDataMap;
  static {
    Map<_Fields, org.apache.thrift.meta_data.FieldMetaData> tmpMap = new EnumMap<_Fields, org.apache.thrift.meta_data.FieldMetaData>(_Fields.class);
    tmpMap.put(_Fields.OPEN_LOW, new org.apache.thrift.meta_data.FieldMetaData("open_low", org.apache.thrift.TFieldRequirementType.REQUIRED, 
        new org.apache.thrift.meta_data.FieldValueMetaData(org.apache.thrift.protocol.TType.BOOL)));
    tmpMap.put(_Fields.LOW, new org.apache.thrift.meta_data.FieldMetaData("low", org.apache.thrift.TFieldRequirementType.REQUIRED, 
        new org.apache.thrift.meta_data.StructMetaData(org.apache.thrift.protocol.TType.STRUCT, Clock.class)));
    tmpMap.put(_Fields.OPEN_HIGH, new org.apache.thrift.meta_data.FieldMetaData("open_high", org.apache.thrift.TFieldRequirementType.REQUIRED, 
        new org.apache.thrift.meta_data.FieldValueMetaData(org.apache.thrift.protocol.TType.BOOL)));
    tmpMap.put(_Fields.HIGH, new org.apache.thrift.meta_data.FieldMetaData("high", org.apache.thrift.TFieldRequirementType.OPTIONAL, 
        new org.apache.thrift.meta_data.StructMetaData(org.apache.thrift.protocol.TType.STRUCT, Clock.class)));
    metaDataMap = Collections.unmodifiableMap(tmpMap);
    org.apache.thrift.meta_data.FieldMetaData.addStructMetaDataMap(Interval.class, metaDataMap);
  }

  public Interval() {
  }

  public Interval(
    boolean open_low,
    Clock low,
    boolean open_high)
  {
    this();
    this.open_low = open_low;
    setOpen_lowIsSet(true);
    this.low = low;
    this.open_high = open_high;
    setOpen_highIsSet(true);
  }

  /**
   * Performs a deep copy on <i>other</i>.
   */
  public Interval(Interval other) {
    __isset_bitfield = other.__isset_bitfield;
    this.open_low = other.open_low;
    if (other.isSetLow()) {
      this.low = new Clock(other.low);
    }
    this.open_high = other.open_high;
    if (other.isSetHigh()) {
      this.high = new Clock(other.high);
    }
  }

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

  @Override
  public void clear() {
    setOpen_lowIsSet(false);
    this.open_low = false;
    this.low = null;
    setOpen_highIsSet(false);
    this.open_high = false;
    this.high = null;
  }

  public boolean isOpen_low() {
    return this.open_low;
  }

  public Interval setOpen_low(boolean open_low) {
    this.open_low = open_low;
    setOpen_lowIsSet(true);
    return this;
  }

  public void unsetOpen_low() {
    __isset_bitfield = EncodingUtils.clearBit(__isset_bitfield, __OPEN_LOW_ISSET_ID);
  }

  /** Returns true if field open_low is set (has been assigned a value) and false otherwise */
  public boolean isSetOpen_low() {
    return EncodingUtils.testBit(__isset_bitfield, __OPEN_LOW_ISSET_ID);
  }

  public void setOpen_lowIsSet(boolean value) {
    __isset_bitfield = EncodingUtils.setBit(__isset_bitfield, __OPEN_LOW_ISSET_ID, value);
  }

  public Clock getLow() {
    return this.low;
  }

  public Interval setLow(Clock low) {
    this.low = low;
    return this;
  }

  public void unsetLow() {
    this.low = null;
  }

  /** Returns true if field low is set (has been assigned a value) and false otherwise */
  public boolean isSetLow() {
    return this.low != null;
  }

  public void setLowIsSet(boolean value) {
    if (!value) {
      this.low = null;
    }
  }

  public boolean isOpen_high() {
    return this.open_high;
  }

  public Interval setOpen_high(boolean open_high) {
    this.open_high = open_high;
    setOpen_highIsSet(true);
    return this;
  }

  public void unsetOpen_high() {
    __isset_bitfield = EncodingUtils.clearBit(__isset_bitfield, __OPEN_HIGH_ISSET_ID);
  }

  /** Returns true if field open_high is set (has been assigned a value) and false otherwise */
  public boolean isSetOpen_high() {
    return EncodingUtils.testBit(__isset_bitfield, __OPEN_HIGH_ISSET_ID);
  }

  public void setOpen_highIsSet(boolean value) {
    __isset_bitfield = EncodingUtils.setBit(__isset_bitfield, __OPEN_HIGH_ISSET_ID, value);
  }

  public Clock getHigh() {
    return this.high;
  }

  public Interval setHigh(Clock high) {
    this.high = high;
    return this;
  }

  public void unsetHigh() {
    this.high = null;
  }

  /** Returns true if field high is set (has been assigned a value) and false otherwise */
  public boolean isSetHigh() {
    return this.high != null;
  }

  public void setHighIsSet(boolean value) {
    if (!value) {
      this.high = null;
    }
  }

  public void setFieldValue(_Fields field, Object value) {
    switch (field) {
    case OPEN_LOW:
      if (value == null) {
        unsetOpen_low();
      } else {
        setOpen_low((Boolean)value);
      }
      break;

    case LOW:
      if (value == null) {
        unsetLow();
      } else {
        setLow((Clock)value);
      }
      break;

    case OPEN_HIGH:
      if (value == null) {
        unsetOpen_high();
      } else {
        setOpen_high((Boolean)value);
      }
      break;

    case HIGH:
      if (value == null) {
        unsetHigh();
      } else {
        setHigh((Clock)value);
      }
      break;

    }
  }

  public Object getFieldValue(_Fields field) {
    switch (field) {
    case OPEN_LOW:
      return Boolean.valueOf(isOpen_low());

    case LOW:
      return getLow();

    case OPEN_HIGH:
      return Boolean.valueOf(isOpen_high());

    case HIGH:
      return getHigh();

    }
    throw new IllegalStateException();
  }

  /** Returns true if field corresponding to fieldID is set (has been assigned a value) and false otherwise */
  public boolean isSet(_Fields field) {
    if (field == null) {
      throw new IllegalArgumentException();
    }

    switch (field) {
    case OPEN_LOW:
      return isSetOpen_low();
    case LOW:
      return isSetLow();
    case OPEN_HIGH:
      return isSetOpen_high();
    case HIGH:
      return isSetHigh();
    }
    throw new IllegalStateException();
  }

  @Override
  public boolean equals(Object that) {
    if (that == null)
      return false;
    if (that instanceof Interval)
      return this.equals((Interval)that);
    return false;
  }

  public boolean equals(Interval that) {
    if (that == null)
      return false;

    boolean this_present_open_low = true;
    boolean that_present_open_low = true;
    if (this_present_open_low || that_present_open_low) {
      if (!(this_present_open_low && that_present_open_low))
        return false;
      if (this.open_low != that.open_low)
        return false;
    }

    boolean this_present_low = true && this.isSetLow();
    boolean that_present_low = true && that.isSetLow();
    if (this_present_low || that_present_low) {
      if (!(this_present_low && that_present_low))
        return false;
      if (!this.low.equals(that.low))
        return false;
    }

    boolean this_present_open_high = true;
    boolean that_present_open_high = true;
    if (this_present_open_high || that_present_open_high) {
      if (!(this_present_open_high && that_present_open_high))
        return false;
      if (this.open_high != that.open_high)
        return false;
    }

    boolean this_present_high = true && this.isSetHigh();
    boolean that_present_high = true && that.isSetHigh();
    if (this_present_high || that_present_high) {
      if (!(this_present_high && that_present_high))
        return false;
      if (!this.high.equals(that.high))
        return false;
    }

    return true;
  }

  @Override
  public int hashCode() {
    List<Object> list = new ArrayList<Object>();

    boolean present_open_low = true;
    list.add(present_open_low);
    if (present_open_low)
      list.add(open_low);

    boolean present_low = true && (isSetLow());
    list.add(present_low);
    if (present_low)
      list.add(low);

    boolean present_open_high = true;
    list.add(present_open_high);
    if (present_open_high)
      list.add(open_high);

    boolean present_high = true && (isSetHigh());
    list.add(present_high);
    if (present_high)
      list.add(high);

    return list.hashCode();
  }

  @Override
  public int compareTo(Interval other) {
    if (!getClass().equals(other.getClass())) {
      return getClass().getName().compareTo(other.getClass().getName());
    }

    int lastComparison = 0;

    lastComparison = Boolean.valueOf(isSetOpen_low()).compareTo(other.isSetOpen_low());
    if (lastComparison != 0) {
      return lastComparison;
    }
    if (isSetOpen_low()) {
      lastComparison = org.apache.thrift.TBaseHelper.compareTo(this.open_low, other.open_low);
      if (lastComparison != 0) {
        return lastComparison;
      }
    }
    lastComparison = Boolean.valueOf(isSetLow()).compareTo(other.isSetLow());
    if (lastComparison != 0) {
      return lastComparison;
    }
    if (isSetLow()) {
      lastComparison = org.apache.thrift.TBaseHelper.compareTo(this.low, other.low);
      if (lastComparison != 0) {
        return lastComparison;
      }
    }
    lastComparison = Boolean.valueOf(isSetOpen_high()).compareTo(other.isSetOpen_high());
    if (lastComparison != 0) {
      return lastComparison;
    }
    if (isSetOpen_high()) {
      lastComparison = org.apache.thrift.TBaseHelper.compareTo(this.open_high, other.open_high);
      if (lastComparison != 0) {
        return lastComparison;
      }
    }
    lastComparison = Boolean.valueOf(isSetHigh()).compareTo(other.isSetHigh());
    if (lastComparison != 0) {
      return lastComparison;
    }
    if (isSetHigh()) {
      lastComparison = org.apache.thrift.TBaseHelper.compareTo(this.high, other.high);
      if (lastComparison != 0) {
        return lastComparison;
      }
    }
    return 0;
  }

  public _Fields fieldForId(int fieldId) {
    return _Fields.findByThriftId(fieldId);
  }

  public void read(org.apache.thrift.protocol.TProtocol iprot) throws org.apache.thrift.TException {
    schemes.get(iprot.getScheme()).getScheme().read(iprot, this);
  }

  public void write(org.apache.thrift.protocol.TProtocol oprot) throws org.apache.thrift.TException {
    schemes.get(oprot.getScheme()).getScheme().write(oprot, this);
  }

  @Override
  public String toString() {
    StringBuilder sb = new StringBuilder("Interval(");
    boolean first = true;

    sb.append("open_low:");
    sb.append(this.open_low);
    first = false;
    if (!first) sb.append(", ");
    sb.append("low:");
    if (this.low == null) {
      sb.append("null");
    } else {
      sb.append(this.low);
    }
    first = false;
    if (!first) sb.append(", ");
    sb.append("open_high:");
    sb.append(this.open_high);
    first = false;
    if (isSetHigh()) {
      if (!first) sb.append(", ");
      sb.append("high:");
      if (this.high == null) {
        sb.append("null");
      } else {
        sb.append(this.high);
      }
      first = false;
    }
    sb.append(")");
    return sb.toString();
  }

  public void validate() throws org.apache.thrift.TException {
    // check for required fields
    // alas, we cannot check 'open_low' because it's a primitive and you chose the non-beans generator.
    if (low == null) {
      throw new org.apache.thrift.protocol.TProtocolException("Required field 'low' was not present! Struct: " + toString());
    }
    // alas, we cannot check 'open_high' because it's a primitive and you chose the non-beans generator.
    // check for sub-struct validity
    if (low != null) {
      low.validate();
    }
    if (high != null) {
      high.validate();
    }
  }

  private void writeObject(java.io.ObjectOutputStream out) throws java.io.IOException {
    try {
      write(new org.apache.thrift.protocol.TCompactProtocol(new org.apache.thrift.transport.TIOStreamTransport(out)));
    } catch (org.apache.thrift.TException te) {
      throw new java.io.IOException(te);
    }
  }

  private void readObject(java.io.ObjectInputStream in) throws java.io.IOException, ClassNotFoundException {
    try {
      // it doesn't seem like you should have to do this, but java serialization is wacky, and doesn't call the default constructor.
      __isset_bitfield = 0;
      read(new org.apache.thrift.protocol.TCompactProtocol(new org.apache.thrift.transport.TIOStreamTransport(in)));
    } catch (org.apache.thrift.TException te) {
      throw new java.io.IOException(te);
    }
  }

  private static class IntervalStandardSchemeFactory implements SchemeFactory {
    public IntervalStandardScheme getScheme() {
      return new IntervalStandardScheme();
    }
  }

  private static class IntervalStandardScheme extends StandardScheme<Interval> {

    public void read(org.apache.thrift.protocol.TProtocol iprot, Interval struct) throws org.apache.thrift.TException {
      org.apache.thrift.protocol.TField schemeField;
      iprot.readStructBegin();
      while (true)
      {
        schemeField = iprot.readFieldBegin();
        if (schemeField.type == org.apache.thrift.protocol.TType.STOP) { 
          break;
        }
        switch (schemeField.id) {
          case 1: // OPEN_LOW
            if (schemeField.type == org.apache.thrift.protocol.TType.BOOL) {
              struct.open_low = iprot.readBool();
              struct.setOpen_lowIsSet(true);
            } else { 
              org.apache.thrift.protocol.TProtocolUtil.skip(iprot, schemeField.type);
            }
            break;
          case 2: // LOW
            if (schemeField.type == org.apache.thrift.protocol.TType.STRUCT) {
              struct.low = new Clock();
              struct.low.read(iprot);
              struct.setLowIsSet(true);
            } else { 
              org.apache.thrift.protocol.TProtocolUtil.skip(iprot, schemeField.type);
            }
            break;
          case 3: // OPEN_HIGH
            if (schemeField.type == org.apache.thrift.protocol.TType.BOOL) {
              struct.open_high = iprot.readBool();
              struct.setOpen_highIsSet(true);
            } else { 
              org.apache.thrift.protocol.TProtocolUtil.skip(iprot, schemeField.type);
            }
            break;
          case 4: // HIGH
            if (schemeField.type == org.apache.thrift.protocol.TType.STRUCT) {
              struct.high = new Clock();
              struct.high.read(iprot);
              struct.setHighIsSet(true);
            } else { 
              org.apache.thrift.protocol.TProtocolUtil.skip(iprot, schemeField.type);
            }
            break;
          default:
            org.apache.thrift.protocol.TProtocolUtil.skip(iprot, schemeField.type);
        }
        iprot.readFieldEnd();
      }
      iprot.readStructEnd();

      // check for required fields of primitive type, which can't be checked in the validate method
      if (!struct.isSetOpen_low()) {
        throw new org.apache.thrift.protocol.TProtocolException("Required field 'open_low' was not found in serialized data! Struct: " + toString());
      }
      if (!struct.isSetOpen_high()) {
        throw new org.apache.thrift.protocol.TProtocolException("Required field 'open_high' was not found in serialized data! Struct: " + toString());
      }
      struct.validate();
    }

    public void write(org.apache.thrift.protocol.TProtocol oprot, Interval struct) throws org.apache.thrift.TException {
      struct.validate();

      oprot.writeStructBegin(STRUCT_DESC);
      oprot.writeFieldBegin(OPEN_LOW_FIELD_DESC);
      oprot.writeBool(struct.open_low);
      oprot.writeFieldEnd();
      if (struct.low != null) {
        oprot.writeFieldBegin(LOW_FIELD_DESC);
        struct.low.write(oprot);
        oprot.writeFieldEnd();
      }
      oprot.writeFieldBegin(OPEN_HIGH_FIELD_DESC);
      oprot.writeBool(struct.open_high);
      oprot.writeFieldEnd();
      if (struct.high != null) {
        if (struct.isSetHigh()) {
          oprot.writeFieldBegin(HIGH_FIELD_DESC);
          struct.high.write(oprot);
          oprot.writeFieldEnd();
        }
      }
      oprot.writeFieldStop();
      oprot.writeStructEnd();
    }

  }

  private static class IntervalTupleSchemeFactory implements SchemeFactory {
    public IntervalTupleScheme getScheme() {
      return new IntervalTupleScheme();
    }
  }

  private static class IntervalTupleScheme extends TupleScheme<Interval> {

    @Override
    public void write(org.apache.thrift.protocol.TProtocol prot, Interval struct) throws org.apache.thrift.TException {
      TTupleProtocol oprot = (TTupleProtocol) prot;
      oprot.writeBool(struct.open_low);
      struct.low.write(oprot);
      oprot.writeBool(struct.open_high);
      BitSet optionals = new BitSet();
      if (struct.isSetHigh()) {
        optionals.set(0);
      }
      oprot.writeBitSet(optionals, 1);
      if (struct.isSetHigh()) {
        struct.high.write(oprot);
      }
    }

    @Override
    public void read(org.apache.thrift.protocol.TProtocol prot, Interval struct) throws org.apache.thrift.TException {
      TTupleProtocol iprot = (TTupleProtocol) prot;
      struct.open_low = iprot.readBool();
      struct.setOpen_lowIsSet(true);
      struct.low = new Clock();
      struct.low.read(iprot);
      struct.setLowIsSet(true);
      struct.open_high = iprot.readBool();
      struct.setOpen_highIsSet(true);
      BitSet incoming = iprot.readBitSet(1);
      if (incoming.get(0)) {
        struct.high = new Clock();
        struct.high.read(iprot);
        struct.setHighIsSet(true);
      }
    }
  }

}

