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
10
11
12
13 public class SortCriteria implements Serializable
14 {
15 private static final long serialVersionUID = 34L;
16
17
18
19
20
21
22
23 public SortCriteria(final String sortBy)
24 {
25 this(sortBy, true);
26 }
27
28
29
30
31
32
33
34
35 public SortCriteria(
36 final String sortBy,
37 final boolean nullsFirst)
38 {
39 this(sortBy, Ordering.ASCENDING, nullsFirst);
40 }
41
42
43
44
45
46
47
48
49 public SortCriteria(
50 final String sortBy,
51 final Ordering ordering)
52 {
53 this(sortBy, ordering, true);
54 }
55
56
57
58
59
60
61
62
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
97
98 private Ordering ordering;
99
100
101
102
103
104
105 public Ordering getOrdering()
106 {
107 return ordering;
108 }
109
110
111
112
113
114
115 public void setOrdering(final Ordering ordering)
116 {
117 this.ordering = ordering;
118 }
119
120
121
122
123 private String sortBy;
124
125
126
127
128
129
130 public String getSortBy()
131 {
132 return sortBy;
133 }
134
135
136
137
138
139
140 public void setSortBy(final String sortBy)
141 {
142 this.sortBy = sortBy;
143 }
144
145 private boolean nullsFirst;
146
147
148
149
150 public boolean isNullsFirst()
151 {
152 return nullsFirst;
153 }
154
155
156
157
158 public void setNullsFirst(boolean nullsFirst)
159 {
160 this.nullsFirst = nullsFirst;
161 }
162
163
164
165
166
167
168
169 public static final class Ordering
170 {
171
172
173
174 public static final Ordering ASCENDING = new Ordering("ASCENDING");
175
176
177
178
179 public static final Ordering DESCENDING = new Ordering("DESCENDING");
180
181
182
183
184 private String value;
185
186 private Ordering(final String ordering)
187 {
188 this.value = ordering;
189 }
190
191
192
193
194 public String toString()
195 {
196 return this.value;
197 }
198 }
199 }