]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.spreadsheet.common/src/org/simantics/spreadsheet/common/matrix/MatrixImpl.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.spreadsheet.common / src / org / simantics / spreadsheet / common / matrix / MatrixImpl.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.spreadsheet.common.matrix;
13
14 import java.io.Externalizable;
15 import java.io.IOException;
16 import java.io.ObjectInput;
17 import java.io.ObjectOutput;
18 import java.util.ArrayList;
19 import java.util.Collection;
20
21 import org.simantics.spreadsheet.Matrix;
22
23 public class MatrixImpl implements Matrix, Externalizable {
24
25         private int rows;
26         private int columns;
27         private Object[] data;
28         
29         public MatrixImpl() {
30                 
31         }
32         
33         public MatrixImpl(int rows, int columns) {
34                 this.rows = rows;
35                 this.columns = columns;
36                 this.data = new Object[rows*columns];
37         }
38
39         @Override
40         public int getColumnCount() {
41                 return columns;
42         }
43
44         @Override
45         public int getRowCount() {
46                 return rows;
47         }
48
49         @Override
50         public void readExternal(ObjectInput in) throws IOException,
51                         ClassNotFoundException {
52                 rows = in.readInt();
53                 columns = in.readInt();
54                 data = new Object[rows*columns];
55                 for(int i=0;i<data.length;i++) data[i] = in.readObject();
56         }
57
58         @Override
59         public void writeExternal(ObjectOutput out) throws IOException {
60                 out.writeInt(rows);
61                 out.writeInt(columns);
62                 for(Object o : data) out.writeObject(o);
63         }
64
65         @Override
66         public Object get(int row, int column) {
67                 return data[column*rows + row];
68         }
69         
70         public void setColumn(int column, Collection<Object> objects) {
71                 int index = rows*column;
72                 for(Object o : objects) {
73                         data[index++] = o;
74                 }
75         }
76
77         public void set(int row, int column, Object object) {
78                 data[rows*column + row] = object;
79         }
80
81         public Collection<Object> getColumn(int column) {
82                 ArrayList<Object> result = new ArrayList<Object>();
83                 for(int i=0;i<rows;i++) {
84                         result.add(data[rows*column+i]);
85                 }
86                 return result;
87         }
88
89         public Collection<Object> getRow(int row) {
90                 ArrayList<Object> result = new ArrayList<Object>();
91                 for(int i=0;i<columns;i++) {
92                         result.add(data[row + columns*i]);
93                 }
94                 return result;
95         }
96         
97 }