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.datematrix.impl;
025    
026    import java.util.Arrays;
027    import java.util.Date;
028    
029    import org.ujmp.core.Matrix;
030    import org.ujmp.core.datematrix.DateMatrix;
031    import org.ujmp.core.datematrix.stub.AbstractDenseDateMatrix2D;
032    import org.ujmp.core.exceptions.MatrixException;
033    
034    public class SimpleDenseDateMatrix2D extends AbstractDenseDateMatrix2D {
035            private static final long serialVersionUID = -3791410138052404116L;
036    
037            private Date[][] values = null;
038    
039            public SimpleDenseDateMatrix2D(Matrix m) throws MatrixException {
040                    if (m instanceof SimpleDenseDateMatrix2D) {
041                            Date[][] v = ((SimpleDenseDateMatrix2D) m).values;
042                            this.values = new Date[v.length][v[0].length];
043                            for (int r = v.length; --r >= 0;) {
044                                    for (int c = v[0].length; --c >= 0;) {
045                                            values[r][c] = v[r][c];
046                                    }
047                            }
048                    } else {
049                            values = new Date[(int) m.getRowCount()][(int) m.getColumnCount()];
050                            for (long[] c : m.allCoordinates()) {
051                                    setAsDate(m.getAsDate(c), c);
052                            }
053                    }
054            }
055    
056            public SimpleDenseDateMatrix2D(Date[]... v) {
057                    this.values = v;
058            }
059    
060            public SimpleDenseDateMatrix2D(long... size) {
061                    values = new Date[(int) size[ROW]][(int) size[COLUMN]];
062                    for (int r = values.length; --r != -1;) {
063                            Arrays.fill(values[r], DateMatrix.DATE0);
064                    }
065            }
066    
067            public SimpleDenseDateMatrix2D(Date[] v) {
068                    this.values = new Date[v.length][1];
069                    for (int r = v.length; --r >= 0;) {
070                            values[r][0] = v[r];
071                    }
072            }
073    
074            public long[] getSize() {
075                    return new long[] { values.length, values.length == 0 ? 0 : values[0].length };
076            }
077    
078            public long getRowCount() {
079                    return values.length;
080            }
081    
082            public long getColumnCount() {
083                    return values.length == 0 ? 0 : values[0].length;
084            }
085    
086            public Date getDate(long row, long column) {
087                    return values[(int) row][(int) column];
088            }
089    
090            public void setDate(Date value, long row, long column) {
091                    values[(int) row][(int) column] = value;
092            }
093    
094            public Date getDate(int row, int column) {
095                    return values[row][column];
096            }
097    
098            public void setDate(Date value, int row, int column) {
099                    values[row][column] = value;
100            }
101    
102            public final Matrix transpose() {
103                    Date[][] result = new Date[values[0].length][values.length];
104                    for (int r = result.length; --r >= 0;) {
105                            for (int c = result[0].length; --c >= 0;) {
106                                    result[r][c] = values[c][r];
107                            }
108                    }
109                    return new SimpleDenseDateMatrix2D(result);
110            }
111    
112    }