001 package com.croftsoft.core.gui.table; 002 003 import java.util.*; 004 005 /********************************************************************* 006 * Sorts rows based upon comparison of elements within a column. 007 * 008 * <p /> 009 * 010 * @version 011 * 2001-08-08 012 * @since 013 * 2001-08-07 014 * @author 015 * <a href="https://www.croftsoft.com/">David Wallace Croft</a> 016 *********************************************************************/ 017 018 public final class RowSortComparator 019 implements Comparator 020 ////////////////////////////////////////////////////////////////////// 021 ////////////////////////////////////////////////////////////////////// 022 { 023 024 private int index; 025 026 private boolean reverse; 027 028 ////////////////////////////////////////////////////////////////////// 029 ////////////////////////////////////////////////////////////////////// 030 031 /********************************************************************* 032 * Main constructor. 033 *********************************************************************/ 034 public RowSortComparator ( 035 int index, 036 boolean reverse ) 037 ////////////////////////////////////////////////////////////////////// 038 { 039 this.index = index; 040 041 this.reverse = reverse; 042 } 043 044 /********************************************************************* 045 * Convenience constructor. 046 * 047 * <pre> 048 * this ( 0, false ); 049 * </pre> 050 *********************************************************************/ 051 public RowSortComparator ( ) 052 ////////////////////////////////////////////////////////////////////// 053 { 054 this ( 0, false ); 055 } 056 057 ////////////////////////////////////////////////////////////////////// 058 ////////////////////////////////////////////////////////////////////// 059 060 /********************************************************************* 061 * The column index on which the rows are to be sorted. 062 *********************************************************************/ 063 public void setIndex ( int index ) 064 ////////////////////////////////////////////////////////////////////// 065 { 066 this.index = index; 067 } 068 069 /********************************************************************* 070 * Reverses the sort order. 071 *********************************************************************/ 072 public void setReverse ( boolean reverse ) 073 ////////////////////////////////////////////////////////////////////// 074 { 075 this.reverse = reverse; 076 } 077 078 ////////////////////////////////////////////////////////////////////// 079 ////////////////////////////////////////////////////////////////////// 080 081 /********************************************************************* 082 * Compares two rows based upon column values. 083 * 084 * <p> 085 * The row arguments should implement the java.util.List interface. 086 * Row elements must implement the java.util.Comparable interface. 087 * </p> 088 *********************************************************************/ 089 public int compare ( Object o1, Object o2 ) 090 ////////////////////////////////////////////////////////////////////// 091 { 092 Comparable e1 = ( Comparable ) ( ( List ) o1 ).get ( index ); 093 094 Comparable e2 = ( Comparable ) ( ( List ) o2 ).get ( index ); 095 096 if ( ( e1 == null ) 097 && ( e2 == null ) ) 098 { 099 return 0; 100 } 101 102 if ( e1 == null ) 103 { 104 return reverse ? 1 : -1; 105 } 106 107 if ( e2 == null ) 108 { 109 return reverse ? -1 : 1; 110 } 111 112 return e1.compareTo ( e2 ) * ( reverse ? -1 : 1 ); 113 } 114 115 ////////////////////////////////////////////////////////////////////// 116 ////////////////////////////////////////////////////////////////////// 117 }