KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > contrib > table > model > simple > ColumnComparator


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 JavaDoc;
18
19 /**
20  * In order to provide more generic behaviour, ITableColumn
21  * has no "column value" concept. The comparator it returns
22  * compares two table rows, rather than values specific to the column.
23  * <p>
24  * SimpleTableColumn introduces the concept of "column value" and
25  * allows one to extract that "column value" from the row using
26  * the getColumnValue() method. In practice comparisons are also typically
27  * done between these values rather than the full row objects.
28  * <p>
29  * This comparator extracts the column values from the rows passed
30  * and uses the provided comparator to compare the values.
31  * It therefore allows a comparator designed for comparing column values to be
32  * quickly wrapped and used as a comparator comparing rows, which is what
33  * ITableColumn is expected to return.
34  * <p>
35  * Example:
36  * <p>
37  * objColumn.setComparator(new ColumnComparator(objColumn, objBeanComparator));
38  *
39  * @author mindbridge
40  *
41  */

42 public class ColumnComparator implements Comparator JavaDoc
43 {
44     private SimpleTableColumn m_objColumn;
45     private Comparator JavaDoc m_objComparator;
46
47     public ColumnComparator(SimpleTableColumn objColumn, Comparator JavaDoc objComparator)
48     {
49         m_objColumn = objColumn;
50         m_objComparator = objComparator;
51     }
52
53     /**
54      * @see java.util.Comparator#compare(Object, Object)
55      */

56     public int compare(Object JavaDoc objRow1, Object JavaDoc objRow2)
57     {
58         Object JavaDoc objValue1 = m_objColumn.getColumnValue(objRow1);
59         Object JavaDoc objValue2 = m_objColumn.getColumnValue(objRow2);
60                 
61         return m_objComparator.compare(objValue1, objValue2);
62     }
63
64 }
65
Popular Tags