package com.google.cloud.hadoop.io.bigquery;

import java.util.HashMap;
import java.util.Map;
import java.util.StringJoiner;
import org.apache.hadoop.classification.InterfaceStability;

/** An enum to describe file formats supported by the BigQuery api. */
@InterfaceStability.Unstable
public enum BigQueryFileFormat {

  /** Comma separated value exports */
  CSV(".csv", "CSV"),

  /** Newline delimited JSON */
  NEWLINE_DELIMITED_JSON(".json", "NEWLINE_DELIMITED_JSON"),

  /** Avro container files */
  AVRO(".avro", "AVRO");

  /** Map used for simple deserialization of strings into BigQueryFileFormats. */
  private static final Map<String, BigQueryFileFormat> NAMES_MAP =
      new HashMap<String, BigQueryFileFormat>();

  /** A formatted string of the accepted file formats. */
  private static final String ACCEPTED_FORMATS;

  static {
    StringJoiner joiner = new StringJoiner(",", "[", "]");
    for (BigQueryFileFormat format : BigQueryFileFormat.values()) {
      NAMES_MAP.put(format.name(), format);
      joiner.add(format.name());
    }

    ACCEPTED_FORMATS = joiner.toString();
  }

  private final String extension;
  private final String formatIdentifier;

  private BigQueryFileFormat(String extension, String formatIdentifier) {
    this.extension = extension;
    this.formatIdentifier = formatIdentifier;
  }

  /** Get the default extension to denote the file format. */
  public String getExtension() {
    return extension;
  }

  /** Get the identifier to specify in API requests. */
  public String getFormatIdentifier() {
    return formatIdentifier;
  }

  /**
   * Deserializes the name of a BigQueryFileFormat. If there is no matching BigQueryFileFormat for
   * the given name, an IllegalArugmentException is thrown.
   *
   * @param name the name of the BigQueryFileFormat as returned from {@link
   *     BigQueryFileFormat#name()}.
   * @return the associated BigQueryFileFormat.
   */
  public static BigQueryFileFormat fromName(String name) {
    BigQueryFileFormat entry = NAMES_MAP.get(name);
    if (entry == null) {
      throw new IllegalArgumentException(
          "Unable to find BigQueryFileFormat for '"
              + name
              + "'. Accepted formats are: "
              + ACCEPTED_FORMATS);
    }
    return entry;
  }
}

@@ -39,16 +39,54 @@
 query;%0A%0A
+import com.google.common.base.Joiner;%0A
 import j
@@ -94,23 +94,25 @@
 va.util.
-HashMap
+ArrayList
 ;%0Aimport
@@ -122,16 +122,20 @@
 va.util.
+Hash
 Map;%0Aimp
@@ -148,28 +148,42 @@
 va.util.
-StringJoiner
+List;%0Aimport java.util.Map
 ;%0Aimport
@@ -908,60 +908,53 @@
     
+List%3C
 String
-Joiner joiner = new StringJoiner(%22,%22, %22%5B%22, %22%5D%22
+%3E formats = new ArrayList%3CString%3E(
 );%0A 
@@ -1070,22 +1070,23 @@
 ;%0A      
-joiner
+formats
 .add(for
@@ -1132,24 +1132,35 @@
 S = 
-j
+J
 oiner.
-toString(
+on(',').join(formats
 );%0A 
@@ -2277,16 +2277,18 @@
 ats are:
+ %5B
  %22%0A     
@@ -2314,16 +2314,37 @@
 _FORMATS
+%0A              + %22 %5D%22
 );%0A    %7D

Update for Java 7 compatibility.