KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > webapp > taglib > core > grid > util > SortField


1 /*
2  * Copyright 2004 Blandware (http://www.blandware.com)
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package com.blandware.atleap.webapp.taglib.core.grid.util;
17
18 import org.apache.commons.lang.builder.ToStringBuilder;
19 import org.apache.commons.lang.builder.ToStringStyle;
20
21 import java.io.Serializable JavaDoc;
22 import java.util.Collections JavaDoc;
23 import java.util.LinkedList JavaDoc;
24 import java.util.List JavaDoc;
25
26 /**
27  * <p>Represents sort field in grid. Sort order may be ascending or descending. By default it is ascending.
28  * </p>
29  * <p><a HREF="SortField.java.htm"><i>View Source</i></a></p>
30  *
31  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
32  * @version $Revision: 1.9 $ $Date: 2005/08/05 17:48:33 $
33  */

34 public class SortField implements Serializable JavaDoc {
35
36     // ~ Static variables
37

38     /**
39      * Indicates that sort order is ascending
40      */

41     public static final String JavaDoc ORDER_ASCENDING = "asc";
42
43     /**
44      * Indicates that sort order is descending
45      */

46     public static final String JavaDoc ORDER_DESCENDING = "desc";
47
48     // ~ Instance variables
49

50     /**
51      * Name of field to sort by
52      */

53     protected String JavaDoc fieldName;
54
55     /**
56      * Name of property to lookup value in order to compare entities, when collection is not taken from persistent storage
57      */

58     protected String JavaDoc property;
59
60     /**
61      * Sorting order (ascending or descending)
62      */

63     protected String JavaDoc order = ORDER_ASCENDING;
64
65     /**
66      * Has or has not been order changed between two subsequent calls to this instance
67      */

68     protected boolean orderChanged = false;
69
70     /**
71      * List of identifiers of rowIterators this sort field is applicable to
72      */

73     protected List JavaDoc rowIterators = Collections.synchronizedList(new LinkedList JavaDoc());
74
75     /**
76      * Creates new instance of SortField
77      *
78      * @param fieldName Name of field to sort by
79      */

80     public SortField(String JavaDoc fieldName) {
81         this(fieldName, ORDER_ASCENDING);
82     }
83
84     /**
85      * Creates new instance of SortField with given sort order
86      *
87      * @param fieldName Name of field to sort by
88      * @param order Sorting order (ascending or descending)
89      */

90     public SortField(String JavaDoc fieldName, String JavaDoc order) {
91         this.fieldName = fieldName;
92         if ( ORDER_ASCENDING.equalsIgnoreCase(order) ) {
93             this.order = ORDER_ASCENDING;
94         } else {
95             this.order = ORDER_DESCENDING;
96         }
97     }
98
99     /**
100      * Returns name of field
101      *
102      * @return field name
103      * @see #fieldName
104      */

105     public String JavaDoc getFieldName() {
106         return fieldName;
107     }
108
109     /**
110      * Sets name of field
111      *
112      * @param fieldName field name to set
113      * @see #fieldName
114      */

115     public void setFieldName(String JavaDoc fieldName) {
116         this.fieldName = fieldName;
117     }
118
119     /**
120      * Returns property name
121      *
122      * @return property name
123      * @see #property
124      */

125     public String JavaDoc getProperty() {
126         return property;
127     }
128
129     /**
130      * Sets property name
131      *
132      * @param property property name to set
133      * @see #property
134      */

135     public void setProperty(String JavaDoc property) {
136         this.property = property;
137     }
138
139     /**
140      * Returns sort order
141      *
142      * @return sort order
143      * @see #order
144      */

145     public String JavaDoc getOrder() {
146         return order;
147     }
148
149     /**
150      * Sets sort order
151      *
152      * @return sort order
153      * @see #order
154      */

155     public List JavaDoc getRowIterators() {
156         return rowIterators;
157     }
158
159     /**
160      * Returns list of row iterators
161      *
162      * @param rowIterators row iterators list
163      * @see #rowIterators
164      */

165     public void setRowIterators(List JavaDoc rowIterators) {
166         this.rowIterators = rowIterators;
167     }
168
169     /**
170      * Sets list of row iterators
171      *
172      * @param rowIteratorId row iterators list
173      * @see #rowIterators
174      */

175     public void addRowIterator(String JavaDoc rowIteratorId) {
176         this.rowIterators.add(rowIteratorId);
177     }
178
179     /**
180      * Gets part of ORDER BY clause
181      *
182      * @param rowIteratorId ID of row iterator
183      * @return part of ORDER BY clause corresponding to row iterator with
184      * specified ID. If now iterator with such ID was found, <code>null</code>
185      */

186     public String JavaDoc getClause(String JavaDoc rowIteratorId) {
187         if ( (rowIteratorId == null) || (rowIterators.size() == 0) || (rowIterators.contains(rowIteratorId)) ) {
188             return fieldName + " " + order;
189         } else {
190             return null;
191         }
192     }
193
194     /**
195      * Returns <code>true</code> if order is ascending
196      *
197      * @return True if order is ascending and false, if descending
198      */

199     public boolean isOrderAscending() {
200         return order.equalsIgnoreCase(ORDER_ASCENDING);
201     }
202
203     /**
204      * Reverses order from ascending to descending and vice versa
205      */

206     public void reverseOrder() {
207         if ( order.equalsIgnoreCase(ORDER_ASCENDING) ) {
208             order = ORDER_DESCENDING;
209         } else {
210             order = ORDER_ASCENDING;
211         }
212         orderChanged = true;
213     }
214
215     /**
216      * Returns <code>true</code> if order has been changed between two subsequent calls to this instance
217      *
218      * @return <code>True</code> if order has been changed between two subsequent calls to this instance
219      */

220     public boolean hasOrderChanged() {
221         boolean orderChanged = this.orderChanged;
222
223         // restore value, because on the next call we need correct information
224
this.orderChanged = false;
225         return orderChanged;
226     }
227
228     public String JavaDoc toString() {
229         return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);
230     }
231
232     /**
233      * Compares this instance with another object
234      *
235      * @param anObject Object to compare with
236      * @return True, if anObject is instance of SortField and has the same values of fieldName and order fields
237      * in case insensitive manner. Otherwise, returns false;
238      */

239     public boolean equals(Object JavaDoc anObject) {
240         if ( anObject instanceof SortField ) {
241             SortField sortField = (SortField) anObject;
242             return (this.fieldName.equalsIgnoreCase(sortField.getFieldName())) && (this.order.equalsIgnoreCase(sortField.getOrder()));
243         } else {
244             return false;
245         }
246     }
247
248     /**
249      * Returns hash code of this instance
250      *
251      * @return hash code
252      */

253     public int hashCode() {
254         int result;
255         result = (fieldName != null ? fieldName.hashCode() : 0);
256         result = 29 * result + (order != null ? order.hashCode() : 0);
257         return result;
258     }
259
260
261 }
262
Popular Tags