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.trinidad.model;
20
21 import java.io.Serializable;
22 import org.apache.myfaces.trinidad.logging.TrinidadLogger;
23
24 /**
25 * This class bundles together a property, a direction and strength by which a
26 * CollectionModel can be sorted.
27 * @see CollectionModel#getSortCriteria
28 */
29 public class SortCriterion implements Serializable
30 {
31 /**
32 * Construct SortCriterion instance by the given property and sort direction,
33 * using default Identical sort strength.
34 * @param property sort property, name of the model that will be sorted on.
35 * @param isAscending whether to sort the property in ascending order or not.
36 */
37 public SortCriterion(
38 String property,
39 boolean isAscending)
40 {
41 this(property, isAscending, SortStrength.IDENTICAL);
42 }
43
44 /**
45 * Construct SortCriterion instance by the given property, sort direction,
46 * and sort strength.
47 * @param property sort property, name of the model that will be sorted on.
48 * @param isAscending whether to sort the property in ascending order or not.
49 * @param sortStrength sort strength value when performing sort.
50 */
51 public SortCriterion(
52 String property,
53 boolean isAscending,
54 SortStrength sortStrength)
55 {
56 if (property == null)
57 throw new NullPointerException(_LOG.getMessage(
58 "NULL_PROPERTY"));
59
60 if (sortStrength == null)
61 throw new NullPointerException(_LOG.getMessage(
62 "NULL_SORT_STRENGTH"));
63
64 _property = property;
65 _sortOrder = isAscending;
66 _sortStrength = sortStrength;
67 }
68
69 /**
70 * Gets the direction in which the property of this class is sorted.
71 * @return true if the property identified by this class is sorted in
72 * ascending order.
73 */
74 public boolean isAscending()
75 {
76 return _sortOrder;
77 }
78
79 /**
80 * Gets the property that is identified by this class. This is the property
81 * that must be sorted by. If a collection of beans is being sorted, bean rules
82 * will be used to find a suitable getter method that matches this property.
83 * The value returned by the getter method will be sorted on.
84 * If a collection of Maps is being sorted, this property will be used
85 * as the key into each Map to get at the value being sorted.
86 */
87 public String getProperty()
88 {
89 return _property;
90 }
91
92 /**
93 * Gets the sort strenght of this sort criterion. It controls how this column
94 * should be sorted, what level of difference considered significant during
95 * comparison.
96 */
97 public SortStrength getSortStrength()
98 {
99 return _sortStrength;
100 }
101
102 @Override
103 public boolean equals(Object obj)
104 {
105 if (this == obj)
106 return true;
107
108 if (obj instanceof SortCriterion)
109 {
110 SortCriterion that = (SortCriterion) obj;
111 return (this.getProperty().equals(that.getProperty())) &&
112 (this.isAscending() == that.isAscending()) &&
113 (this.getSortStrength() == that.getSortStrength());
114 }
115
116 return false;
117 }
118
119 @Override
120 public int hashCode()
121 {
122 int hc = getProperty().hashCode();
123 hc = hc * 37 + _sortStrength.hashCode();
124 return isAscending() ? hc : -hc;
125 }
126
127 private final String _property;
128 private final boolean _sortOrder;
129 private final SortStrength _sortStrength;
130 private static final TrinidadLogger _LOG = TrinidadLogger.createTrinidadLogger(
131 SortCriterion.class);
132
133 private static final long serialVersionUID = 1L;
134 }