001package com.box.sdk;
002
003import static com.box.sdk.BoxFolder.SortDirection.ASC;
004import static com.box.sdk.BoxFolder.SortDirection.DESC;
005
006/** Represents sorting parameters. */
007public final class SortParameters {
008  private static final SortParameters NONE = new SortParameters(null, null);
009  private final String fieldName;
010  private final BoxFolder.SortDirection sortDirection;
011
012  /**
013   * Creates sorting parameters.
014   *
015   * @param fieldName Name of the field used to sort.
016   * @param sortDirection Direction of the sort.
017   */
018  private SortParameters(String fieldName, BoxFolder.SortDirection sortDirection) {
019    this.fieldName = fieldName;
020    this.sortDirection = sortDirection;
021  }
022
023  /**
024   * Creates ascending sorting by specified field name.
025   *
026   * @param fieldName Name of the field used to sort.
027   * @return Sort parameters.
028   */
029  public static SortParameters ascending(String fieldName) {
030    return new SortParameters(fieldName, ASC);
031  }
032
033  /**
034   * Creates descending sorting by specified field name.
035   *
036   * @param fieldName Name of the field used to sort.
037   * @return Sort parameters.
038   */
039  public static SortParameters descending(String fieldName) {
040    return new SortParameters(fieldName, DESC);
041  }
042
043  /**
044   * Creates empty sorting parameters that will not set any sort params in the query.
045   *
046   * @return Sort parameters.
047   */
048  public static SortParameters none() {
049    return NONE;
050  }
051
052  QueryStringBuilder asQueryStringBuilder() {
053    if (fieldName == null || sortDirection == null) {
054      return new QueryStringBuilder();
055    }
056    return new QueryStringBuilder()
057        .appendParam("sort", fieldName)
058        .appendParam("direction", sortDirection.name());
059  }
060}