/**
   * Returns a result segment of an enumerable collection of files and directories for this
   * directory, using the specified listing details options, request options, and operation context.
   *
   * @param maxResults The maximum number of results to retrieve.
   * @param continuationToken A {@link ResultContinuation} object that represents a continuation
   *     token returned by a previous listing operation.
   * @param options A {@link FileRequestOptions} object that specifies any additional options for
   *     the request. Specifying <code>null</code> will use the default request options from the
   *     associated service client ( {@link CloudFileClient}).
   * @param opContext An {@link OperationContext} object that represents the context for the current
   *     operation. This object is used to track requests to the storage service, and to provide
   *     additional runtime information about the operation.
   * @return A {@link ResultSegment} object that contains a segment of the enumerable collection of
   *     {@link ListFileItem} objects that represent the files and directories in this directory.
   * @throws StorageException If a storage service error occurred.
   */
  public ResultSegment<ListFileItem> listFilesAndDirectoriesSegmented(
      final int maxResults,
      final ResultContinuation continuationToken,
      FileRequestOptions options,
      OperationContext opContext)
      throws StorageException {
    if (opContext == null) {
      opContext = new OperationContext();
    }

    opContext.initialize();
    options = FileRequestOptions.applyDefaults(options, this.fileServiceClient);

    Utility.assertContinuationType(continuationToken, ResultContinuationType.FILE);

    SegmentedStorageRequest segmentedRequest = new SegmentedStorageRequest();
    segmentedRequest.setToken(continuationToken);

    return ExecutionEngine.executeWithRetry(
        this.fileServiceClient,
        this,
        this.listFilesAndDirectoriesSegmentedImpl(maxResults, options, segmentedRequest),
        options.getRetryPolicyFactory(),
        opContext);
  }
  /**
   * Creates the directory using the specified options and operation context.
   *
   * @param options A {@link FileRequestOptions} object that specifies any additional options for
   *     the request. Specifying <code>null</code> will use the default request options from the
   *     associated service client ( {@link CloudFileClient}).
   * @param opContext An {@link OperationContext} object that represents the context for the current
   *     operation. This object is used to track requests to the storage service, and to provide
   *     additional runtime information about the operation.
   * @throws StorageException If a storage service error occurred.
   */
  @DoesServiceRequest
  public void create(FileRequestOptions options, OperationContext opContext)
      throws StorageException {
    if (opContext == null) {
      opContext = new OperationContext();
    }

    opContext.initialize();
    options = FileRequestOptions.applyDefaults(options, this.fileServiceClient);

    ExecutionEngine.executeWithRetry(
        this.fileServiceClient,
        this,
        createDirectoryImpl(options),
        options.getRetryPolicyFactory(),
        opContext);
  }
  /**
   * Downloads the directory's properties using the specified request options and operation context.
   *
   * @param accessCondition An {@link AccessCondition} object that represents the access conditions
   *     for the directory.
   * @param options A {@link FileRequestOptions} object that specifies any additional options for
   *     the request. Specifying <code>null</code> will use the default request options from the
   *     associated service client ( {@link CloudFileClient}).
   * @param opContext An {@link OperationContext} object that represents the context for the current
   *     operation. This object is used to track requests to the storage service, and to provide
   *     additional runtime information about the operation.
   * @throws StorageException If a storage service error occurred.
   */
  @DoesServiceRequest
  public void downloadAttributes(
      AccessCondition accessCondition, FileRequestOptions options, OperationContext opContext)
      throws StorageException {
    if (opContext == null) {
      opContext = new OperationContext();
    }

    opContext.initialize();
    options = FileRequestOptions.applyDefaults(options, this.fileServiceClient);

    ExecutionEngine.executeWithRetry(
        this.fileServiceClient,
        this,
        this.downloadAttributesImpl(accessCondition, options),
        options.getRetryPolicyFactory(),
        opContext);
  }
  /**
   * Returns an enumerable collection of file and directory items for the directory.
   *
   * @param options A {@link FileRequestOptions} object that specifies any additional options for
   *     the request. Specifying <code>null</code> will use the default request options from the
   *     associated service client ( {@link CloudFileClient}).
   * @param opContext An {@link OperationContext} object that represents the context for the current
   *     operation. This object is used to track requests to the storage service, and to provide
   *     additional runtime information about the operation.
   * @return An enumerable collection of {@link ListFileItem} objects that represent the file and
   *     directory items in this directory.
   * @throws StorageException If a storage service error occurred.
   * @throws URISyntaxException If the resource URI is invalid.
   */
  @DoesServiceRequest
  public Iterable<ListFileItem> listFilesAndDirectories(
      FileRequestOptions options, OperationContext opContext) {
    if (opContext == null) {
      opContext = new OperationContext();
    }

    opContext.initialize();
    options = FileRequestOptions.applyDefaults(options, this.fileServiceClient);

    SegmentedStorageRequest segmentedRequest = new SegmentedStorageRequest();

    return new LazySegmentedIterable<CloudFileClient, CloudFileDirectory, ListFileItem>(
        this.listFilesAndDirectoriesSegmentedImpl(-1, options, segmentedRequest),
        this.fileServiceClient,
        this,
        options.getRetryPolicyFactory(),
        opContext);
  }
  @DoesServiceRequest
  private boolean exists(
      final boolean primaryOnly,
      final AccessCondition accessCondition,
      FileRequestOptions options,
      OperationContext opContext)
      throws StorageException {
    if (opContext == null) {
      opContext = new OperationContext();
    }

    opContext.initialize();
    options = FileRequestOptions.applyDefaults(options, this.fileServiceClient);

    return ExecutionEngine.executeWithRetry(
        this.fileServiceClient,
        this,
        this.existsImpl(primaryOnly, accessCondition, options),
        options.getRetryPolicyFactory(),
        opContext);
  }
  /**
   * Deletes the directory if it exists using the specified request options and operation context.
   *
   * @param accessCondition An {@link AccessCondition} object that represents the access conditions
   *     for the directory.
   * @param options A {@link FileRequestOptions} object that specifies any additional options for
   *     the request. Specifying <code>null</code> will use the default request options from the
   *     associated service client ( {@link CloudFileClient}).
   * @param opContext An {@link OperationContext} object that represents the context for the current
   *     operation. This object is used to track requests to the storage service, and to provide
   *     additional runtime information about the operation.
   * @return <code>true</code> if the directory existed and was deleted; otherwise, <code>false
   *     </code>.
   * @throws StorageException If a storage service error occurred.
   */
  @DoesServiceRequest
  public boolean deleteIfExists(
      AccessCondition accessCondition, FileRequestOptions options, OperationContext opContext)
      throws StorageException {
    options = FileRequestOptions.applyDefaults(options, this.fileServiceClient);

    boolean exists = this.exists(true /* primaryOnly */, accessCondition, options, opContext);
    if (exists) {
      try {
        this.delete(accessCondition, options, opContext);
        return true;
      } catch (StorageException e) {
        if (e.getHttpStatusCode() == HttpURLConnection.HTTP_NOT_FOUND
            && StorageErrorCodeStrings.RESOURCE_NOT_FOUND.equals(e.getErrorCode())) {
          return false;
        } else {
          throw e;
        }
      }
    } else {
      return false;
    }
  }
  /**
   * Creates the directory if it does not exist, using the specified request options and operation
   * context.
   *
   * @param options A {@link FileRequestOptions} object that specifies any additional options for
   *     the request. Specifying <code>null</code> will use the default request options from the
   *     associated service client ({@link CloudFileClient}).
   * @param opContext An {@link OperationContext} object that represents the context for the current
   *     operation. This object is used to track requests to the storage service, and to provide
   *     additional runtime information about the operation.
   * @return <code>true</code> if the directory did not already exist and was created; otherwise,
   *     <code>false</code>.
   * @throws StorageException If a storage service error occurred.
   */
  @DoesServiceRequest
  public boolean createIfNotExists(FileRequestOptions options, OperationContext opContext)
      throws StorageException {
    options = FileRequestOptions.applyDefaults(options, this.fileServiceClient);

    boolean exists =
        this.exists(true /* primaryOnly */, null /* accessCondition */, options, opContext);
    if (exists) {
      return false;
    } else {
      try {
        this.create(options, opContext);
        return true;
      } catch (StorageException e) {
        if (e.getHttpStatusCode() == HttpURLConnection.HTTP_CONFLICT
            && StorageErrorCodeStrings.RESOURCE_ALREADY_EXISTS.equals(e.getErrorCode())) {
          return false;
        } else {
          throw e;
        }
      }
    }
  }