View Javadoc

1   /*
2    * Copyright 2004-2005 Emmanouil Batsis
3    * 
4    * Licensed under the GNU General Public 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.gnu.org/licenses/gpl.txt
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   */
17  package com.geekologue.md4j.dao;
18  
19  import java.io.Serializable;
20  import java.util.HashMap;
21  import java.util.Iterator;
22  import java.util.LinkedList;
23  import java.util.List;
24  
25  /***
26   * Utility class to specify the ordering of results in Page implementations, for
27   * example:
28   * 
29   * <pre>
30   * Order order = new Order().addOrder(&quot;name&quot;, true).addOrder(&quot;surname&quot;, true);
31   * </pre>
32   * 
33   * will return the results sorted by name and surname in ascending order.
34   * 
35   * @author manos
36   * 
37   */
38  public class Order implements Serializable {
39      private LinkedList orderProperties = new LinkedList();
40  
41      private HashMap    ascOrDesc       = new HashMap();
42  
43      /***
44       * 
45       * @param propertyName
46       *            the property name to add in the order rules
47       * @param ascending
48       *            whether use ascending order or not
49       * @return
50       */
51      public Order addOrder(String propertyName, boolean ascending) {
52          this.orderProperties.add(propertyName);
53          this.ascOrDesc.put(propertyName, ascending ? Boolean.TRUE
54                  : Boolean.FALSE);
55          return this;
56      }
57  
58      public List getOrderProperties() {
59          return this.orderProperties;
60      }
61  
62      public boolean isAscending(String propertyName) {
63          return ((Boolean) this.ascOrDesc.get(propertyName)).booleanValue();
64      }
65  
66      public String toString() {
67          StringBuffer sb = new StringBuffer(Order.class.getName());
68          for (Iterator iter = this.orderProperties.listIterator(); iter
69                  .hasNext();) {
70              String prop = (String) iter.next();
71              sb.append(prop).append(": ").append(
72                      this.isAscending(prop) ? "asc" : "desc");
73              if (iter.hasNext()) {
74                  sb.append(", ");
75              }
76          }
77          return sb.toString();
78      }
79  }