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.genericmatrix.impl; 025 026 import org.ujmp.core.Matrix; 027 import org.ujmp.core.enums.ValueType; 028 import org.ujmp.core.exceptions.MatrixException; 029 import org.ujmp.core.genericmatrix.stub.AbstractDenseGenericMatrix2D; 030 031 public class DefaultDenseGenericMatrix2D<A> extends AbstractDenseGenericMatrix2D<A> { 032 private static final long serialVersionUID = 3132491298449205914L; 033 034 private Object[][] values = null; 035 036 public DefaultDenseGenericMatrix2D(Matrix m) throws MatrixException { 037 values = new Object[(int) m.getRowCount()][(int) m.getColumnCount()]; 038 for (long[] c : m.allCoordinates()) { 039 setAsObject(m.getAsObject(c), c); 040 } 041 } 042 043 public DefaultDenseGenericMatrix2D(A[][] values) throws MatrixException { 044 this.values = values; 045 } 046 047 public DefaultDenseGenericMatrix2D(long... size) { 048 values = new Object[(int) size[ROW]][(int) size[COLUMN]]; 049 } 050 051 public long[] getSize() { 052 return new long[] { values.length, values.length == 0 ? 0 : values[0].length }; 053 } 054 055 056 public long getRowCount() { 057 return values.length; 058 } 059 060 061 public long getColumnCount() { 062 return values.length == 0 ? 0 : values[0].length; 063 } 064 065 @SuppressWarnings("unchecked") 066 public A getObject(long row, long column) { 067 return (A) values[(int) row][(int) column]; 068 } 069 070 public void setObject(Object value, long row, long column) { 071 values[(int) row][(int) column] = value; 072 } 073 074 @SuppressWarnings("unchecked") 075 public A getObject(int row, int column) { 076 return (A) values[row][column]; 077 } 078 079 public void setObject(A value, int row, int column) { 080 values[row][column] = value; 081 } 082 083 084 public ValueType getValueType() { 085 return ValueType.OBJECT; 086 } 087 088 }