View Javadoc

1   package org.apache.torque.sql;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.Serializable;
23  
24  import org.apache.commons.lang.builder.EqualsBuilder;
25  import org.apache.commons.lang.builder.HashCodeBuilder;
26  import org.apache.torque.Column;
27  import org.apache.torque.criteria.SqlEnum;
28  
29  /**
30   * An order by clause.
31   * @version $Id: OrderBy.java 1331187 2012-04-27 02:30:47Z tfischer $
32   *
33   */
34  public class OrderBy implements Serializable
35  {
36      /** SerialVersionUID. */
37      private static final long serialVersionUID = 1L;
38  
39      /** The column to order by. */
40      private Column column;
41  
42      /** The order to order by (ascending or descending). */
43      private SqlEnum order;
44  
45      /** Whether case should be ignored for String columns. */
46      private boolean ignoreCase = true;
47  
48      /**
49       * Constructor.
50       *
51       * @param column the column to order by, not null.
52       * @param order the order, either SqlEnum.DESC or SqlEnum.ASC, not null.
53       * @param ignoreCase whether case should be ignored for String columns
54       *
55       * @throws NullPointerException if null is passed.
56       * @throws IllegalArgumentException if an unknown order is passed.
57       */
58      public OrderBy(Column column, SqlEnum order, boolean ignoreCase)
59      {
60          if (column == null)
61          {
62              throw new NullPointerException("column is null");
63          }
64          if (order == null)
65          {
66              throw new NullPointerException("order is null");
67          }
68          if (SqlEnum.DESC != order && SqlEnum.ASC != order)
69          {
70              throw new IllegalArgumentException("unknown order: " + order);
71          }
72          this.column = column;
73          this.order = order;
74          this.ignoreCase = ignoreCase;
75      }
76  
77      /**
78       * Returns the column to order by.
79       *
80       * @return the column to order by, not null.
81       */
82      public Column getColumn()
83      {
84          return column;
85      }
86  
87      /**
88       * Returns the order to order by (ASC or DESC).
89       *
90       * @return the order, either SqlEnum.DESC or SqlEnum.ASC, not null.
91       */
92      public SqlEnum getOrder()
93      {
94          return order;
95      }
96  
97      /**
98       * Returns whether case should be ignored for String columns.
99       *
100      * @return true if case should be ignored for String columns,
101      *         false otherwise.
102      */
103     public boolean isIgnoreCase()
104     {
105         return ignoreCase;
106     }
107 
108     @Override
109     public int hashCode()
110     {
111         return new HashCodeBuilder()
112             .append(this.column.getSqlExpression())
113             .append(this.column.getSchemaName())
114             .append(this.column.getTableName())
115             .append(this.column.getColumnName())
116             .append(this.order)
117             .append(this.ignoreCase)
118             .toHashCode();
119     }
120 
121     /**
122      * Checks whether two orderBy are equal.
123      * This is true if and only if the orders are equal and if the contained
124      * columns have the same schema name, table name, column name
125      * and sql expression.
126      *
127      * @param obj the object to compare to.
128      *
129      * @return true if this object is equal to obj, false otherwise.
130      */
131     @Override
132     public boolean equals(Object obj)
133     {
134         if (this == obj)
135         {
136             return true;
137         }
138         if (obj == null)
139         {
140             return false;
141         }
142         if (getClass() != obj.getClass())
143         {
144             return false;
145         }
146         OrderBy other = (OrderBy) obj;
147         return new EqualsBuilder()
148             .append(this.column.getSqlExpression(),
149                     other.column.getSqlExpression())
150             .append(this.column.getSchemaName(),
151                     other.column.getSchemaName())
152             .append(this.column.getTableName(),
153                     other.column.getTableName())
154             .append(this.column.getColumnName(),
155                     other.column.getColumnName())
156             .append(this.order, other.order)
157             .append(this.ignoreCase, other.ignoreCase)
158             .isEquals();
159     }
160 }