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 015 package org.apache.tapestry.contrib.table.model.simple; 016 017 import java.util.Comparator; 018 019 /** 020 * In order to provide more generic behaviour, ITableColumn has no "column 021 * value" concept. The comparator it returns compares two table rows, rather 022 * than values specific to the column. 023 * <p> 024 * SimpleTableColumn introduces the concept of "column value" and allows one to 025 * extract that "column value" from the row using the getColumnValue() method. 026 * In practice comparisons are also typically done between these values rather 027 * than the full row objects. 028 * <p> 029 * This comparator extracts the column values from the rows passed and uses the 030 * provided comparator to compare the values. It therefore allows a comparator 031 * designed for comparing column values to be quickly wrapped and used as a 032 * comparator comparing rows, which is what ITableColumn is expected to return. 033 * <p> 034 * Example: 035 * <p> 036 * objColumn.setComparator(new ColumnComparator(objColumn, objBeanComparator)); 037 * 038 * @author mindbridge 039 */ 040 public class ColumnComparator implements Comparator 041 { 042 043 private SimpleTableColumn m_objColumn; 044 private Comparator m_objComparator; 045 046 public ColumnComparator(SimpleTableColumn objColumn, 047 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 }