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