View Javadoc

    1   /*
    2    * Created on Jun 16, 2004
    3    */
    4   package baseCode.dataFilter;
    5
    6   import java.util.Set;
    7   import java.util.Vector;
    8
    9   import baseCode.dataStructure.matrix.NamedMatrix;
    10
    11  /***
    12   * Remove or retain rows that are on a list.
    13   *
    14   * @author Paul Pavlidis
    15   * @version $Id: RowNameFilter.java,v 1.10 2004/07/27 03:18:58 pavlidis Exp $
    16   */
    17  public class RowNameFilter extends AbstractFilter implements Filter {
    18
    19     private boolean exclude = false;
    20     private Set filterNames;
    21
    22     /***
    23      * @param namesToFilter
    24      * @param exclude Set to true if you want the list to indicate items to be skipped, rather than selected.
    25      */
    26     public RowNameFilter( Set namesToFilter, boolean exclude ) {
    27        this( namesToFilter );
    28        this.exclude = exclude;
    29     }
    30
    31     /***
    32      * @param namesToFilter
    33      */
    34     public RowNameFilter( Set namesToFilter ) {
    35        filterNames = namesToFilter;
    36     }
    37
    38     public RowNameFilter() {
    39        filterNames = null;
    40     }
    41
    42     public void setFilterNames( Set namesToFilter, boolean exclude ) {
    43        this.filterNames = namesToFilter;
    44        this.exclude = exclude;
    45     }
    46
    47     /***
    48      * Filter according to row names.
    49      *
    50      * @param data
    51      * @return
    52      */
    53     public NamedMatrix filter( NamedMatrix data ) {
    54        Vector MTemp = new Vector();
    55        Vector rowNames = new Vector();
    56        int numRows = data.rows();
    57        int numCols = data.columns();
    58        int numNeeded = filterNames.size();
    59        int kept = 0;
    60        for ( int i = 0; i < numRows; i++ ) {
    61           String name = data.getRowName( i );
    62
    63           // apply the rules.
    64           if ( filterNames.contains( name ) ) {
    65              if ( exclude ) {
    66                 continue;
    67              }
    68              MTemp.add( data.getRowObj( i ) );
    69              rowNames.add( name );
    70              kept++;
    71              if ( kept >= numNeeded ) {
    72                 break; // no use in continuing.
    73              }
    74           }
    75
    76           if ( exclude ) {
    77              MTemp.add( data.getRowObj( i ) );
    78              rowNames.add( name );
    79              kept++;
    80           }
    81        }
    82
    83        NamedMatrix returnval = getOutputMatrix( data, MTemp.size(), numCols );
    84
    85        for ( int i = 0; i < MTemp.size(); i++ ) {
    86           for ( int j = 0; j < numCols; j++ ) {
    87              returnval.set( i, j, ( ( Object[] ) MTemp.get( i ) )[j] );
    88           }
    89        }
    90        returnval.setColumnNames( data.getColNames() );
    91        returnval.setRowNames( rowNames );
    92
    93        log.info( "There are " + kept + " rows left after filtering." );
    94
    95        return ( returnval );
    96     }
    97
    98  }