View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.component.html.ext;
20  
21  import java.io.Serializable;
22  
23  /**
24   *
25   */
26  public final class SortCriterion implements Serializable {
27      
28      private final String _property;
29      private final boolean _sortOrder;
30      
31      public SortCriterion(String property, boolean isAscending) {
32          if (property == null)
33              throw new NullPointerException("property is null");
34          
35          _property = property;
36          _sortOrder = isAscending;
37      }
38      
39      /**
40       * Gets the direction in which the property of this class is sorted.
41       * @return true if the property identified by this class is sorted in
42       * ascending order.
43       */
44      public boolean isAscending() {
45          return _sortOrder;
46      }
47      
48      /**
49       * Gets the property that is identified by this class. This is the property
50       * that must be sorted by. If a collection of beans is being sorted, bean rules
51       * will be used to find a suitable getter method that matches this property.
52       * The value returned by the getter method will be sorted on.
53       * If a collection of Maps is being sorted, this property will be used
54       * as the key into each Map to get at the value being sorted.
55       */
56      public String getProperty() {
57          return _property;
58      }
59      
60      public boolean equals(Object obj) {
61          if (this == obj)
62              return true;
63          
64          if (obj instanceof SortCriterion) {
65              SortCriterion that = (SortCriterion) obj;
66              return (this.getProperty().equals(that.getProperty())) &&
67                      (this.isAscending() == that.isAscending());
68          }
69          
70          return false;
71      }
72      
73      public int hashCode() {
74          int hc = getProperty().hashCode();
75          return isAscending() ? hc : -hc;
76      }       
77  }