1 package org.andromda.utils.beans;
2
3 import java.io.Serializable;
4 import java.util.Collection;
5 import org.andromda.core.common.ClassUtils;
6 import org.andromda.core.common.ExceptionUtils;
7
8 /**
9 * Used to contain sort criteria.
10 *
11 * @author Chad Brandon
12 */
13 public class SortCriteria implements Serializable
14 {
15 private static final long serialVersionUID = 34L;
16 /**
17 * Creates a SortCriteria object with the default ascending ordering and nulls
18 * placed first.
19 *
20 * @param sortBy the property to sort by, this can
21 * be nested (i.e. person.name.firstName will be sorted).
22 */
23 public SortCriteria(final String sortBy)
24 {
25 this(sortBy, true);
26 }
27
28 /**
29 * Creates a new instance of this SortCriteria class.
30 *
31 * @param sortBy the property to sort by, this can
32 * be nested (i.e. person.name.firstName will be sorted).
33 * @param nullsFirst whether or not nulls will be placed first or last when sorting occurs.
34 */
35 public SortCriteria(
36 final String sortBy,
37 final boolean nullsFirst)
38 {
39 this(sortBy, Ordering.ASCENDING, nullsFirst);
40 }
41
42 /**
43 * Creates a new instance of this SortCriteria class.
44 *
45 * @param sortBy the property to sort by, this can
46 * be nested (i.e. person.name.firstName will be sorted).
47 * @param ordering the ordering to sort by: {@link Ordering#ASCENDING} or {@link Ordering#DESCENDING}.
48 */
49 public SortCriteria(
50 final String sortBy,
51 final Ordering ordering)
52 {
53 this(sortBy, ordering, true);
54 }
55
56 /**
57 * Creates a new instance of this SortCriteria class.
58 *
59 * @param sortBy the property to sort by, this can
60 * be nested (i.e. person.name.firstName will be sorted).
61 * @param ordering the ordering to sort by: {@link Ordering#ASCENDING} or {@link Ordering#DESCENDING}.
62 * @param nullsFirst whether or not nulls will be placed first or last when sorting occurs.
63 */
64 public SortCriteria(
65 final String sortBy,
66 final Ordering ordering,
67 final boolean nullsFirst)
68 {
69 ExceptionUtils.checkEmpty(
70 "sortBy",
71 sortBy);
72 try
73 {
74 if (ordering != null)
75 {
76 final Collection validOrderings = ClassUtils.getStaticFieldValues(
77 String.class,
78 SortCriteria.class);
79 if (validOrderings.contains(ordering))
80 {
81 throw new IllegalArgumentException("ordering must be of one of the following types: " +
82 validOrderings);
83 }
84 }
85 }
86 catch (final Throwable throwable)
87 {
88 throw new SortException(throwable);
89 }
90 this.sortBy = sortBy;
91 this.ordering = ordering;
92 this.nullsFirst = nullsFirst;
93 }
94
95 /**
96 * The ordering by which sorting shall occur.
97 */
98 private Ordering ordering;
99
100 /**
101 * Gets the current ordering to be used.
102 *
103 * @return Ordering
104 */
105 public Ordering getOrdering()
106 {
107 return ordering;
108 }
109
110 /**
111 * Sets the ordering to use for sorting.
112 *
113 * @param ordering the ordering.
114 */
115 public void setOrdering(final Ordering ordering)
116 {
117 this.ordering = ordering;
118 }
119
120 /**
121 * Stores the name of the property to sort by.
122 */
123 private String sortBy;
124
125 /**
126 * Gets the sort by name.
127 *
128 * @return String
129 */
130 public String getSortBy()
131 {
132 return sortBy;
133 }
134
135 /**
136 * Sets the name of the property by which to sort.
137 *
138 * @param sortBy the name of the property by which to sort.
139 */
140 public void setSortBy(final String sortBy)
141 {
142 this.sortBy = sortBy;
143 }
144
145 private boolean nullsFirst;
146
147 /**
148 * @return the nullsFirst
149 */
150 public boolean isNullsFirst()
151 {
152 return nullsFirst;
153 }
154
155 /**
156 * @param nullsFirst the nullsFirst to set
157 */
158 public void setNullsFirst(boolean nullsFirst)
159 {
160 this.nullsFirst = nullsFirst;
161 }
162
163 /**
164 * Represents the types of ordering that may occur when sorting
165 * with the {@link BeanSorter}.
166 *
167 * @author Chad Brandon
168 */
169 public static final class Ordering
170 {
171 /**
172 * Indicates sorting should be performed <em>ascending</em>.
173 */
174 public static final Ordering ASCENDING = new Ordering("ASCENDING");
175
176 /**
177 * Indicates sorting should be performed <em>descending</em>.
178 */
179 public static final Ordering DESCENDING = new Ordering("DESCENDING");
180
181 /**
182 * The actual value of the enumeration.
183 */
184 private String value;
185
186 private Ordering(final String ordering)
187 {
188 this.value = ordering;
189 }
190
191 /**
192 * @see Object#toString()
193 */
194 public String toString()
195 {
196 return this.value;
197 }
198 }
199 }