

package co.cask.tigon.sql.flowlet;

import co.cask.tigon.internal.io.Schema;
import com.google.common.primitives.Doubles;
import com.google.common.primitives.Ints;
import com.google.common.primitives.Longs;

/**
 * PrimitiveTypes used in the GDAT format.
 */
public enum GDATFieldType {
  BOOL("bool", Ints.BYTES, Schema.of(Schema.Type.BOOLEAN)),
  INT("int", Ints.BYTES, Schema.of(Schema.Type.INT)),
  LONG("llong", Longs.BYTES, Schema.of(Schema.Type.LONG)),
  DOUBLE("float", Doubles.BYTES, Schema.of(Schema.Type.DOUBLE)),
  STRING("v_str", 3 * Ints.BYTES, Schema.of(Schema.Type.STRING));

  private String type;
  private int size;
  private Schema schema;

  //Field Type.
  private GDATFieldType(String type, int size, Schema schema) {
    this.type = type;
    this.size = size;
    this.schema = schema;
  }

  /**
   * Return the field type name (bool, float, etc..).
   * @return Field Type Name.
   */
  public String getTypeName() {
    return type;
  }

  /**
   * Return the size occupied by this field in a GDAT Record.
   * @return Size in Bytes.
   */
  public int getSize() {
    return size;
  }

  /**
   * Return the {@link co.cask.tigon.internal.io.Schema} of this GDAT field
   * @return {@link co.cask.tigon.internal.io.Schema}
   */
  public Schema getSchema() {
    return schema;
  }

  /**
   * Return the {@link GDATFieldType} given a stream engine field type name.
   * @param typeName Field Type Name used in Stream Engine.
   * @return {@link GDATFieldType}
   */
  public static GDATFieldType getGDATFieldType(String typeName) {
    for (GDATFieldType gdatFieldType : GDATFieldType.values()) {
      if (gdatFieldType.getTypeName().equals(typeName.toLowerCase())) {
        return gdatFieldType;
      }
    }
    return null;
  }
}
