class TimestampExtractionPolicyFactory {

  public static TimestampExtractionPolicy create(
      final Schema schema,
      final String timestampColumnName,
      final String timestampFormat) {
    if (timestampColumnName == null) {
      return new MetadataTimestampExtractionPolicy();
    }

    final String fieldName = StringUtil.cleanQuotes(timestampColumnName.toUpperCase());
    final Field timestampField = SchemaUtil.getFieldByName(schema,
        fieldName)
        .orElseThrow(() -> new KsqlException(String.format(
            "No column with the provided timestamp column name in the "
                + "WITH clause, %s, exists in the defined schema.",
            fieldName
        )));

    final Schema.Type timestampFieldType = timestampField.schema().type();
    if (timestampFieldType == Schema.Type.STRING) {
      if (timestampFormat == null) {
        throw new KsqlException("A String timestamp field has been specified without"
            + " also specifying the "
            + DdlConfig.TIMESTAMP_FORMAT_PROPERTY.toLowerCase());
      }
      return new StringTimestampExtractionPolicy(
          fieldName,
          StringUtil.cleanQuotes(timestampFormat));
    }

    if (timestampFieldType == Schema.Type.INT64) {
      return new LongColumnTimestampExtractionPolicy(fieldName);
    }

    throw new KsqlException(
        "Timestamp column, " + timestampColumnName + ", should be LONG(INT64)"
            + " or a String with a "
            + DdlConfig.TIMESTAMP_FORMAT_PROPERTY.toLowerCase()
            + " specified");
  }

}