001// Copyright 2004, 2005 The Apache Software Foundation
002//
003// Licensed under the Apache License, Version 2.0 (the "License");
004// you may not use this file except in compliance with the License.
005// You may obtain a copy of the License at
006//
007//     http://www.apache.org/licenses/LICENSE-2.0
008//
009// Unless required by applicable law or agreed to in writing, software
010// distributed under the License is distributed on an "AS IS" BASIS,
011// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012// See the License for the specific language governing permissions and
013// limitations under the License.
014
015package org.apache.tapestry.contrib.table.model.simple;
016
017import java.util.Comparator;
018
019/**
020 * In order to provide more generic behaviour, ITableColumn 
021 * has no "column value" concept. The comparator it returns 
022 * compares two table rows, rather than values specific to the column. 
023 * <p>
024 * SimpleTableColumn introduces the concept of "column value" and 
025 * allows one to extract that "column value" from the row using 
026 * the getColumnValue() method. In practice comparisons are also typically 
027 * done between these values rather than the full row objects.
028 * <p>
029 * This comparator extracts the column values from the rows passed 
030 * and uses the provided comparator to compare the values.
031 * It therefore allows a comparator designed for comparing column values to be
032 * quickly wrapped and used as a comparator comparing rows, which is what
033 * ITableColumn is expected to return.
034 * <p>
035 * Example:
036 * <p>
037 * objColumn.setComparator(new ColumnComparator(objColumn, objBeanComparator));    
038 * 
039 * @author mindbridge
040 *
041 */
042public class ColumnComparator implements Comparator
043{
044    private SimpleTableColumn m_objColumn;
045        private Comparator m_objComparator;
046
047        public ColumnComparator(SimpleTableColumn objColumn, Comparator objComparator)
048        {
049        m_objColumn = objColumn;
050                m_objComparator = objComparator;
051        }
052
053        /**
054         * @see java.util.Comparator#compare(Object, Object)
055         */
056        public int compare(Object objRow1, Object objRow2)
057        {
058        Object objValue1 = m_objColumn.getColumnValue(objRow1);
059        Object objValue2 = m_objColumn.getColumnValue(objRow2);
060                
061                return m_objComparator.compare(objValue1, objValue2);
062        }
063
064}