View Javadoc
1   /*
2    * Licensed to The Apereo Foundation under one or more contributor license
3    * agreements. See the NOTICE file distributed with this work for additional
4    * information regarding copyright ownership.
5    *
6    *
7    * The Apereo Foundation licenses this file to you under the Educational
8    * Community License, Version 2.0 (the "License"); you may not use this file
9    * except in compliance with the License. You may obtain a copy of the License
10   * at:
11   *
12   *   http://opensource.org/licenses/ecl2.txt
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
17   * License for the specific language governing permissions and limitations under
18   * the License.
19   *
20   */
21  
22  
23  package org.opencastproject.util.requests;
24  
25  import static java.util.Objects.requireNonNull;
26  
27  import java.util.Objects;
28  
29  /**
30   * A sort criterion represents the combination of a field name and a sort {@link Order}
31   */
32  public final class SortCriterion {
33  
34    /**
35     * Sort order definitions.
36     */
37    public enum Order {
38      None, Ascending, Descending
39    }
40  
41    private final String fieldName;
42    private final Order order;
43  
44    /**
45     * Parse a string representation of a sort criterion.
46     *
47     * @param sortCriterion
48     *          the sort criterion string
49     * @return the sort criterion
50     */
51    public static SortCriterion parse(final String sortCriterion) {
52      requireNonNull(sortCriterion);
53  
54      String[] parts = sortCriterion.split(":");
55      if (parts.length != 2) {
56        throw new IllegalArgumentException("sortOrder must be of form <field name>:ASC/DESC");
57      }
58  
59      if ("ASC".equalsIgnoreCase(parts[1]) || "Ascending".equalsIgnoreCase(parts[1])) {
60        return new SortCriterion(parts[0].trim(), Order.Ascending);
61      }
62      if ("DESC".equalsIgnoreCase(parts[1]) || "Descending".equalsIgnoreCase(parts[1])) {
63        return new SortCriterion(parts[0].trim(), Order.Descending);
64      }
65  
66      throw new IllegalArgumentException("Invalid order " + parts[1]);
67    }
68  
69    /**
70     * Create a order criterion based on the given field name and order.
71     *
72     * @param fieldName
73     *          the field name
74     * @param order
75     *          the order
76     */
77    public SortCriterion(String fieldName, Order order) {
78      this.fieldName = fieldName;
79      this.order = order;
80    }
81  
82    public String getFieldName() {
83      return fieldName;
84    }
85  
86    public Order getOrder() {
87      return order;
88    }
89  
90    @Override
91    public boolean equals(Object o) {
92      if (o == this) {
93        return true;
94      }
95  
96      if (!(o instanceof SortCriterion)) {
97        return false;
98      }
99  
100     SortCriterion that = (SortCriterion) o;
101     return Objects.equals(this.fieldName, that.fieldName) && Objects.equals(this.order, that.order);
102   }
103 
104   @Override
105   public int hashCode() {
106     return Objects.hash(fieldName, order);
107   }
108 
109   @Override
110   public String toString() {
111     if (order.equals(Order.Ascending)) {
112       return fieldName + ":ASC";
113     }
114     if (order.equals(Order.Descending)) {
115       return fieldName + ":DESC";
116     }
117     return fieldName + ":NONE";
118   }
119 
120 }