Classes in this File | Line Coverage | Branch Coverage | Complexity | |||||||
ColumnComparator |
|
| 1.0;1 |
1 | // Copyright 2004, 2005 The Apache Software Foundation |
|
2 | // |
|
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
|
4 | // you may not use this file except in compliance with the License. |
|
5 | // You may obtain a copy of the License at |
|
6 | // |
|
7 | // http://www.apache.org/licenses/LICENSE-2.0 |
|
8 | // |
|
9 | // Unless required by applicable law or agreed to in writing, software |
|
10 | // distributed under the License is distributed on an "AS IS" BASIS, |
|
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
12 | // See the License for the specific language governing permissions and |
|
13 | // limitations under the License. |
|
14 | ||
15 | package org.apache.tapestry.contrib.table.model.simple; |
|
16 | ||
17 | import java.util.Comparator; |
|
18 | ||
19 | /** |
|
20 | * In order to provide more generic behaviour, ITableColumn has no "column |
|
21 | * value" concept. The comparator it returns compares two table rows, rather |
|
22 | * than values specific to the column. |
|
23 | * <p> |
|
24 | * SimpleTableColumn introduces the concept of "column value" and allows one to |
|
25 | * extract that "column value" from the row using the getColumnValue() method. |
|
26 | * In practice comparisons are also typically done between these values rather |
|
27 | * than the full row objects. |
|
28 | * <p> |
|
29 | * This comparator extracts the column values from the rows passed and uses the |
|
30 | * provided comparator to compare the values. It therefore allows a comparator |
|
31 | * designed for comparing column values to be quickly wrapped and used as a |
|
32 | * comparator comparing rows, which is what ITableColumn is expected to return. |
|
33 | * <p> |
|
34 | * Example: |
|
35 | * <p> |
|
36 | * objColumn.setComparator(new ColumnComparator(objColumn, objBeanComparator)); |
|
37 | * |
|
38 | * @author mindbridge |
|
39 | */ |
|
40 | public class ColumnComparator implements Comparator |
|
41 | { |
|
42 | ||
43 | private SimpleTableColumn m_objColumn; |
|
44 | private Comparator m_objComparator; |
|
45 | ||
46 | public ColumnComparator(SimpleTableColumn objColumn, |
|
47 | Comparator objComparator) |
|
48 | 0 | { |
49 | 0 | m_objColumn = objColumn; |
50 | 0 | m_objComparator = objComparator; |
51 | 0 | } |
52 | ||
53 | /** |
|
54 | * @see java.util.Comparator#compare(Object, Object) |
|
55 | */ |
|
56 | public int compare(Object objRow1, Object objRow2) |
|
57 | { |
|
58 | 0 | Object objValue1 = m_objColumn.getColumnValue(objRow1); |
59 | 0 | Object objValue2 = m_objColumn.getColumnValue(objRow2); |
60 | ||
61 | 0 | return m_objComparator.compare(objValue1, objValue2); |
62 | } |
|
63 | ||
64 | } |