KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > util > Matrix


1 /*
2   Copyright (C) 2003 Renaud Pawlak <renaud@aopsys.com>
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12   GNU Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public License
15   along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */

17
18 package org.objectweb.jac.util;
19
20 import java.util.Vector JavaDoc;
21 import org.apache.log4j.Logger;
22 import java.util.List JavaDoc;
23
24 /**
25  * This class represents a wrappable matrix of object
26  *
27  * <p>The implementation is simple and not optimized.
28  */

29 public class Matrix {
30     Logger logger = Logger.getLogger("matrix");
31
32     public Matrix() {
33         rowCount = 0;
34         columnCount = 0;
35     }
36
37     public Matrix(int cCount, int rCount) {
38         allocate(cCount,rCount);
39     }
40
41     /**
42      * Sets the number of columns and the number of rows.
43      *
44      * <p>Old data is discarded and all cells are set to null</p>
45      * @param cCount number of columns
46      * @param rCount number of rows
47      */

48     public void allocate(int cCount, int rCount) {
49         logger.debug("allocate("+cCount+","+rCount+")");
50         this.columnCount = cCount;
51         this.rowCount = rCount;
52         Vector JavaDoc cols = new Vector JavaDoc(cCount);
53         cols.setSize(cCount);
54         for (int i=0; i<cCount; i++) {
55             Vector JavaDoc col = new Vector JavaDoc(rCount);
56             col.setSize(rCount);
57             cols.set(i, col);
58         }
59         this.cols = cols;
60     }
61
62     /**
63      * Inserts an element in the matrix.
64      *
65      * @param i the row of insertion
66      * @param j the columm of insertion
67      * @param value the object to insert in the matrix
68      */

69     public void set(int i, int j, Object JavaDoc value)
70         throws IndexOutOfBoundsException JavaDoc
71     {
72         checkBounds(i, j);
73         ((Vector JavaDoc)cols.get(i)).set(j,value);
74     }
75
76     /**
77      * Gets an element in the matrix.
78      *
79      * @param i the row
80      * @param j the columm
81      * @return value the element at the given place
82      */

83     public Object JavaDoc get(int i, int j) throws IndexOutOfBoundsException JavaDoc {
84         checkBounds(i, j);
85         return ((Vector JavaDoc) cols.get(i)).get(j);
86     }
87
88     void checkBounds(int i, int j) throws IndexOutOfBoundsException JavaDoc {
89         if (i >= columnCount) {
90             throw new IndexOutOfBoundsException JavaDoc(
91                 "Column index out of bound for "+this+": "+
92                 i + " >= " + columnCount);
93         }
94         if (j >= rowCount) {
95             throw new IndexOutOfBoundsException JavaDoc(
96                 "Row index out of bound for "+this+": "+
97                 j + " >= " + rowCount);
98         }
99     }
100
101     List JavaDoc 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 }
121
Popular Tags