001    /*
002     * Copyright (C) 2008-2010 by Holger Arndt
003     *
004     * This file is part of the Universal Java Matrix Package (UJMP).
005     * See the NOTICE file distributed with this work for additional
006     * information regarding copyright ownership and licensing.
007     *
008     * UJMP is free software; you can redistribute it and/or modify
009     * it under the terms of the GNU Lesser General Public License as
010     * published by the Free Software Foundation; either version 2
011     * of the License, or (at your option) any later version.
012     *
013     * UJMP is distributed in the hope that it will be useful,
014     * but WITHOUT ANY WARRANTY; without even the implied warranty of
015     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016     * GNU Lesser General Public License for more details.
017     *
018     * You should have received a copy of the GNU Lesser General Public
019     * License along with UJMP; if not, write to the
020     * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
021     * Boston, MA  02110-1301  USA
022     */
023    
024    package org.ujmp.core.objectmatrix.impl;
025    
026    import org.ujmp.core.Matrix;
027    import org.ujmp.core.exceptions.MatrixException;
028    import org.ujmp.core.objectmatrix.stub.AbstractDenseObjectMatrix2D;
029    
030    public class SimpleDenseObjectMatrix2D extends AbstractDenseObjectMatrix2D {
031            private static final long serialVersionUID = -7051381548902586972L;
032    
033            private Object[][] values = null;
034    
035            public SimpleDenseObjectMatrix2D(Object[]... values) {
036                    this.values = values;
037            }
038    
039            public SimpleDenseObjectMatrix2D(long... size) {
040                    values = new Object[(int) size[ROW]][(int) size[COLUMN]];
041            }
042    
043            public SimpleDenseObjectMatrix2D(Matrix m) throws MatrixException {
044                    if (m instanceof SimpleDenseObjectMatrix2D) {
045                            Object[][] v = ((SimpleDenseObjectMatrix2D) m).values;
046                            this.values = new Object[v.length][v[0].length];
047                            for (int r = v.length; --r >= 0;) {
048                                    for (int c = v[0].length; --c >= 0;) {
049                                            values[r][c] = v[r][c];
050                                    }
051                            }
052                    } else {
053                            values = new Object[(int) m.getRowCount()][(int) m.getColumnCount()];
054                            for (long[] c : m.allCoordinates()) {
055                                    setObject(m.getAsObject(c), c);
056                            }
057                    }
058            }
059    
060            public long[] getSize() {
061                    return new long[] { values.length, values.length == 0 ? 0 : values[0].length };
062            }
063    
064            
065            public long getRowCount() {
066                    return values.length;
067            }
068    
069            
070            public long getColumnCount() {
071                    return values.length == 0 ? 0 : values[0].length;
072            }
073    
074            public Object getObject(long row, long column) {
075                    return values[(int) row][(int) column];
076            }
077    
078            public void setObject(Object value, long row, long column) {
079                    values[(int) row][(int) column] = value;
080            }
081    
082            public Object getObject(int row, int column) {
083                    return values[row][column];
084            }
085    
086            public void setObject(Object value, int row, int column) {
087                    values[row][column] = value;
088            }
089    
090    }