package org.atlasapi.content;

import java.util.Arrays;

import org.atlasapi.util.ImmutableCollectors;

import com.google.common.collect.ImmutableList;

public class QueryOrdering {

    public enum Direction {
        ASC,
        DESC
    }

    public static class Node {
        private final String path;
        private final Direction direction;

        public Node(String path, Direction direction) {
            this.path = path;
            this.direction = direction;
        }

        public boolean isAscending() {
            return direction == Direction.ASC;
        }

        public String getPath() {
            return path;
        }
    }

    private final ImmutableList<Node> sortOrder;

    public QueryOrdering(ImmutableList<Node> sortOrder) {
        this.sortOrder = sortOrder;
    }

    public ImmutableList<Node> getSortOrder() {
        return sortOrder;
    }

    public static QueryOrdering fromOrderBy(String orderBy) {
        return new QueryOrdering(Arrays.stream(orderBy.split(","))
                .map(clause -> {
                    int lastDot = clause.lastIndexOf(".");
                    if (lastDot == -1) {
                        throw new IllegalArgumentException("Missing .asc or .desc operator after " + clause);
                    }

                   String path = clause.substring(0, lastDot);
                   Direction direction = Direction.valueOf(clause.substring(lastDot + 1, clause.length()).toUpperCase());

                    return new Node(path, direction);
                })
                .collect(ImmutableCollectors.toList()));

    }
}

@@ -24,16 +24,82 @@
 ntent;%0A%0A
+import static com.google.common.base.Preconditions.checkNotNull;%0A%0A
 import j
@@ -115,16 +115,16 @@
 Arrays;%0A
-
 %0Aimport 
@@ -493,20 +493,34 @@
 .path = 
+checkNotNull(
 path
+)
 ;%0A      
@@ -542,16 +542,29 @@
 ction = 
+checkNotNull(
 directio
@@ -564,16 +564,17 @@
 irection
+)
 ;%0A      
@@ -884,16 +884,29 @@
 Order = 
+checkNotNull(
 sortOrde
@@ -898,32 +898,33 @@
 otNull(sortOrder
+)
 ;%0A    %7D%0A%0A    pub

Add checkNotNull for QueryOrdering constructor arguments