1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.spreadsheet.common.matrix;
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;
21 import org.simantics.spreadsheet.Matrix;
23 public class MatrixImpl implements Matrix, Externalizable {
27 private Object[] data;
33 public MatrixImpl(int rows, int columns) {
35 this.columns = columns;
36 this.data = new Object[rows*columns];
40 public int getColumnCount() {
45 public int getRowCount() {
50 public void readExternal(ObjectInput in) throws IOException,
51 ClassNotFoundException {
53 columns = in.readInt();
54 data = new Object[rows*columns];
55 for(int i=0;i<data.length;i++) data[i] = in.readObject();
59 public void writeExternal(ObjectOutput out) throws IOException {
61 out.writeInt(columns);
62 for(Object o : data) out.writeObject(o);
66 public Object get(int row, int column) {
67 return data[column*rows + row];
70 public void setColumn(int column, Collection<Object> objects) {
71 int index = rows*column;
72 for(Object o : objects) {
77 public void set(int row, int column, Object object) {
78 data[rows*column + row] = object;
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]);
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]);