001    /*
002      Copyright (C) 2003 Renaud Pawlak <renaud@aopsys.com>
003    
004      This program is free software; you can redistribute it and/or modify
005      it under the terms of the GNU Lesser General Public License as
006      published by the Free Software Foundation; either version 2 of the
007      License, or (at your option) any later version.
008    
009      This program is distributed in the hope that it will be useful,
010      but WITHOUT ANY WARRANTY; without even the implied warranty of
011      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012      GNU Lesser General Public License for more details.
013    
014      You should have received a copy of the GNU Lesser General Public License
015      along with this program; if not, write to the Free Software
016      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
017    
018    package org.objectweb.jac.util;
019    
020    import java.util.Vector;
021    import org.apache.log4j.Logger;
022    import java.util.List;
023    
024    /**
025     * This class represents a wrappable matrix of object
026     *
027     * <p>The implementation is simple and not optimized.
028     */
029    public class Matrix {
030        Logger logger = Logger.getLogger("matrix");
031    
032        public Matrix() {
033            rowCount = 0;
034            columnCount = 0;
035        }
036    
037        public Matrix(int cCount, int rCount) {
038            allocate(cCount,rCount);
039        }
040    
041        /**
042         * Sets the number of columns and the number of rows. 
043         *
044         * <p>Old data is discarded and all cells are set to null</p>
045         * @param cCount number of columns
046         * @param rCount number of rows
047         */
048        public void allocate(int cCount, int rCount) {
049            logger.debug("allocate("+cCount+","+rCount+")");
050            this.columnCount = cCount;
051            this.rowCount = rCount;
052            Vector cols = new Vector(cCount);
053            cols.setSize(cCount);
054            for (int i=0; i<cCount; i++) {
055                Vector col = new Vector(rCount);
056                col.setSize(rCount);
057                cols.set(i, col);
058            }
059            this.cols = cols;
060        }
061    
062        /**
063         * Inserts an element in the matrix.
064         * 
065         * @param i the row of insertion 
066         * @param j the columm of insertion
067         * @param value the object to insert in the matrix
068         */
069        public void set(int i, int j, Object value)
070            throws IndexOutOfBoundsException 
071        {
072            checkBounds(i, j);
073            ((Vector)cols.get(i)).set(j,value);
074        }
075    
076        /** 
077         *  Gets an element in the matrix.
078         * 
079         * @param i the row
080         * @param j the columm
081         * @return value the element at the given place
082         */
083        public Object get(int i, int j) throws IndexOutOfBoundsException {
084            checkBounds(i, j);
085            return ((Vector) cols.get(i)).get(j);
086        }
087    
088        void checkBounds(int i, int j) throws IndexOutOfBoundsException {
089            if (i >= columnCount) {
090                throw new IndexOutOfBoundsException(
091                    "Column index out of bound for "+this+": "+
092                    i + " >= " + columnCount);
093            }
094            if (j >= rowCount) {
095                throw new IndexOutOfBoundsException(
096                    "Row index out of bound for "+this+": "+
097                    j + " >= " + rowCount);
098            }
099        }
100    
101        List cols;
102    
103        int rowCount;
104    
105        /**
106         * Gets the row count.
107         */
108        public int getRowCount() {
109            return rowCount;
110        }
111    
112        int columnCount;
113    
114        /**
115         * Gets column count.
116         */
117        public int getColumnCount() {
118            return columnCount;
119        }
120    }