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