/*
 * Licensed to the University of California, Berkeley under one or more contributor license
 * agreements. See the NOTICE file distributed with this work for additional information regarding
 * copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance with the License. You may obtain a
 * copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */

package tachyon.client.file.options;

import tachyon.Constants;
import tachyon.annotation.PublicApi;
import tachyon.client.ClientContext;
import tachyon.client.UnderStorageType;
import tachyon.client.WriteType;
import tachyon.conf.TachyonConf;
import tachyon.thrift.MkdirTOptions;

@PublicApi
public final class MkdirOptions {
  public static class Builder implements OptionsBuilder<MkdirOptions> {
    private boolean mRecursive;
    private UnderStorageType mUnderStorageType;

    /**
     * Creates a new builder for {@link MkdirOptions}.
     */
    public Builder() {
      this(ClientContext.getConf());
    }

    /**
     * Creates a new builder for {@link MkdirOptions}.
     *
     * @param conf a Tachyon configuration
     */
    public Builder(TachyonConf conf) {
      mRecursive = false;
      WriteType defaultWriteType =
          conf.getEnum(Constants.USER_FILE_WRITE_TYPE_DEFAULT, WriteType.class);
      mUnderStorageType = defaultWriteType.getUnderStorageType();
    }

    /**
     * @param recursive the recursive flag value to use; it specifies whether parent directories
     *        should be created if they do not already exist
     * @return the builder
     */
    public Builder setRecursive(boolean recursive) {
      mRecursive = recursive;
      return this;
    }

    /**
     * This is an advanced API, use {@link Builder#setWriteType} when possible.
     *
     * @param underStorageType the under storage type to use
     * @return the builder
     */
    public Builder setUnderStorageType(UnderStorageType underStorageType) {
      mUnderStorageType = underStorageType;
      return this;
    }

    /**
     * @param writeType the write type to use
     * @return the builder
     */
    public Builder setWriteType(WriteType writeType) {
      mUnderStorageType = writeType.getUnderStorageType();
      return this;
    }

    /**
     * Builds a new instance of {@code MkdirOptions}.
     *
     * @return a {@code MkdirOptions} instance
     */
    @Override
    public MkdirOptions build() {
      return new MkdirOptions(this);
    }
  }

  /**
   * @return the default {@code MkdirOptions}
   */
  public static MkdirOptions defaults() {
    return new Builder().build();
  }

  private final boolean mRecursive;
  private final UnderStorageType mUnderStorageType;

  private MkdirOptions(MkdirOptions.Builder builder) {
    mRecursive = builder.mRecursive;
    mUnderStorageType = builder.mUnderStorageType;
  }

  /**
   * @return the recursive flag value; it specifies whether parent directories should be created if
   *         they do not already exist
   */
  public boolean isRecursive() {
    return mRecursive;
  }

  /**
   * @return the under storage type
   */
  public UnderStorageType getUnderStorageType() {
    return mUnderStorageType;
  }

  /**
   * @return the name : value pairs for all the fields
   */
  @Override
  public String toString() {
    StringBuilder sb = new StringBuilder("MkdirOptions(");
    sb.append(super.toString()).append(", Recursive: ").append(mRecursive);
    sb.append(", UnderStorageType: ").append(mUnderStorageType.toString());
    sb.append(")");
    return sb.toString();
  }

  /**
   * @return Thrift representation of the options
   */
  public MkdirTOptions toThrift() {
    MkdirTOptions options = new MkdirTOptions();
    options.setPersisted(mUnderStorageType.isSyncPersist());
    options.setRecursive(mRecursive);
    return options;
  }
}

@@ -1185,16 +1185,50 @@
 ions%3E %7B%0A
+    private boolean mAllowExists;%0A
     priv
@@ -1247,24 +1247,24 @@
 mRecursive;%0A
-
     private 
@@ -1624,16 +1624,44 @@
  false;%0A
+      mAllowExists = false;%0A
       Wr
@@ -1833,32 +1833,362 @@
 eType();%0A    %7D%0A%0A
+    /**%0A     * @param allowExists the allowExists flag value to use; it specifies whether an exception%0A     *        should be thrown if the directory being made already exists.%0A     * @return the builder%0A     */%0A    public Builder setAllowExists(boolean allowExists) %7B%0A      mAllowExists = allowExists;%0A      return this;%0A    %7D%0A%0A
     /**%0A     * @
@@ -3406,16 +3406,54 @@
 );%0A  %7D%0A%0A
+  private final boolean mAllowExists;%0A
   privat
@@ -3584,24 +3584,65 @@
  builder) %7B%0A
+    mAllowExists = builder.mAllowExists;%0A
     mRecursi
@@ -3714,32 +3714,255 @@
 orageType;%0A  %7D%0A%0A
+  /**%0A   * @return the allowExists flag value; it specifies whether an exception should be thrown if the%0A   *         directory being made already exists%0A   */%0A  public boolean isAllowExists() %7B%0A    return mRecursive;%0A  %7D%0A%0A
   /**%0A   * @retu
@@ -4453,24 +4453,104 @@
 Options(%22);%0A
+    sb.append(super.toString()).append(%22, AllowExists: %22).append(mAllowExists);%0A
     sb.appen
@@ -4883,24 +4883,104 @@
 TOptions();%0A
+    options.setAllowExists(mAllowExists);%0A    options.setRecursive(mRecursive);%0A
     options.
@@ -5032,46 +5032,8 @@
 ));%0A
-    options.setRecursive(mRecursive);%0A
     

Add AllowExists to the client-side mkdir options