View Javadoc

    1   package baseCode.dataStructure.matrix;
    2
    3   import java.util.Iterator;
    4   import java.util.LinkedHashMap;
    5   import java.util.List;
    6   import java.util.Map;
    7   import java.util.Vector;
    8
    9   /***
    10   * <p>
    11   * Copyright (c) 2004 Columbia University
    12   *
    13   * @author pavlidis
    14   * @version $Id: AbstractNamedMatrix.java,v 1.7 2005/03/18 02:53:38 pavlidis Exp $
    15   */
    16  public abstract class AbstractNamedMatrix implements NamedMatrix {
    17
    18     private Vector rowNames;
    19     private Vector colNames;
    20     private Map rowMap; //contains a map of each row and elements in the row
    21     private Map colMap;
    22
    23     private int lastColumnIndex = 0;
    24     private int lastRowIndex = 0;
    25
    26     /***
    27      *
    28      *
    29      */
    30     public AbstractNamedMatrix() {
    31        rowMap = new LinkedHashMap(); //contains a map of each row name to index
    32        // of the row.
    33        colMap = new LinkedHashMap();
    34        rowNames = new Vector();
    35        colNames = new Vector();
    36     }
    37
    38     /***
    39      * Add a column name when we don't care what the index will be. The index will be set by the method. This is useful
    40      * for when we need to set up a matrix before we know how many column or rows there are.
    41      *
    42      * @param s
    43      */
    44     public final void addColumnName( String s ) {
    45
    46        if ( colMap.containsKey( s ) ) {
    47           throw new IllegalArgumentException( "Duplicate column name " + s );
    48        }
    49
    50        this.colNames.add( s );
    51        this.colMap.put( s, new Integer( lastColumnIndex ) );
    52        lastColumnIndex++;
    53
    54     }
    55
    56     public final void addColumnName( String s, int i ) {
    57
    58        if ( colMap.containsKey( s ) ) {
    59           throw new IllegalArgumentException( "Duplicate column name " + s );
    60        }
    61
    62        this.colNames.add( s );
    63        this.colMap.put( s, new Integer( i ) );
    64     }
    65
    66     /***
    67      * Add a row name when we don't care what the index will be. The index will be set by the method. This is useful for
    68      * when we need to set up a matrix before we know how many column or rows there are.
    69      *
    70      * @param s
    71      */
    72     public final void addRowName( String s ) {
    73
    74        if ( rowMap.containsKey( s ) ) {
    75           //     throw new IllegalArgumentException("Duplicate row name " + s);
    76           return;
    77        }
    78
    79        this.rowNames.add( s );
    80        this.rowMap.put( s, new Integer( lastRowIndex ) );
    81        lastRowIndex++;
    82     }
    83
    84     /*
    85      * (non-Javadoc)
    86      *
    87      * @see baseCode.dataStructure.NamedMatrix#addRowName(java.lang.String, int)
    88      */
    89     public final void addRowName( String s, int i ) {
    90
    91        if ( rowMap.containsKey( s ) ) {
    92           //      throw new IllegalArgumentException("Duplicate row name " + s);
    93           return;
    94        }
    95
    96        this.rowNames.add( s );
    97        this.rowMap.put( s, new Integer( i ) );
    98     }
    99
    100    /***
    101     * @param s String
    102     * @return int
    103     */
    104    public final int getRowIndexByName( String s ) {
    105       if ( !rowNames.contains( s ) ) {
    106          throw new IllegalArgumentException( s + " not found" );
    107       }
    108
    109       return ( ( Integer ) rowMap.get( s ) ).intValue();
    110    }
    111
    112    /***
    113     * @param r String
    114     * @return int
    115     */
    116    public final int getColIndexByName( String r ) {
    117       if ( !colNames.contains( r ) ) {
    118          throw new IllegalArgumentException( r + " not found" );
    119       }
    120
    121       return ( ( Integer ) this.colMap.get( r ) ).intValue();
    122    }
    123
    124    /***
    125     * @param i int
    126     * @return java.lang.String
    127     */
    128    public final String getRowName( int i ) {
    129       return ( String ) rowNames.get( i );
    130    }
    131
    132    /***
    133     * @param i int
    134     * @return java.lang.String
    135     */
    136    public final String getColName( int i ) {
    137       return ( String ) colNames.get( i );
    138    }
    139
    140    public final boolean hasRowNames() {
    141       return rowNames.size() == rows();
    142    }
    143
    144    public final boolean hasColNames() {
    145       return colNames.size() == columns();
    146    }
    147
    148    public final void setRowNames( List v ) {
    149       for ( int i = 0; i < v.size(); i++ ) {
    150          addRowName( ( String ) v.get( i ), i );
    151       }
    152    }
    153
    154    public final void setColumnNames( List v ) {
    155       for ( int i = 0; i < v.size(); i++ ) {
    156          addColumnName( ( String ) v.get( i ), i );
    157       }
    158    }
    159
    160    public final List getColNames() {
    161       return colNames;
    162    }
    163
    164    public final List getRowNames() {
    165       return rowNames;
    166    }
    167
    168    public final boolean hasRow( String r ) {
    169       return this.rowMap.containsKey( r );
    170    }
    171
    172    public final Iterator getRowNameMapIterator() {
    173       return this.rowMap.keySet().iterator();
    174    }
    175
    176    public abstract int rows();
    177
    178    public abstract int columns();
    179
    180    public abstract void set( int i, int j, Object val );
    181
    182    public abstract Object[] getRowObj( int i );
    183
    184    public abstract Object[] getColObj( int i );
    185
    186    public abstract boolean isMissing( int i, int j );
    187
    188    public final boolean containsRowName( String rowName ) {
    189       return rowNames.contains( rowName );
    190    }
    191
    192    public final boolean containsColumnName( String columnName ) {
    193       return colNames.contains( columnName );
    194    }
    195
    196    /*
    197     * (non-Javadoc)
    198     *
    199     * @see baseCode.dataStructure.NamedMatrix#numMissing()
    200     */
    201    public int numMissing() {
    202       int count = 0;
    203       int n = this.rows();
    204       int m = this.columns();
    205       for ( int i = 0; i < n; i++ ) {
    206          for ( int j = 0; j < m; j++ ) {
    207             if ( isMissing( i, j ) ) {
    208                count++;
    209             }
    210          }
    211       }
    212       return count;
    213    }
    214
    215 }