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    import org.ujmp.core.interfaces.HasColumnMajorDateArray1D;
034    
035    public class DefaultDenseDateMatrix2D extends AbstractDenseDateMatrix2D implements
036                    HasColumnMajorDateArray1D {
037            private static final long serialVersionUID = -3145074963888564555L;
038    
039            private Date[] values = null;
040    
041            private long[] size = null;
042    
043            private int rows = 0;
044    
045            private int cols = 0;
046    
047            public DefaultDenseDateMatrix2D(Matrix m) throws MatrixException {
048                    this.rows = (int) m.getRowCount();
049                    this.cols = (int) m.getColumnCount();
050                    this.size = new long[] { rows, cols };
051                    if (m instanceof DefaultDenseDateMatrix2D) {
052                            Date[] v = ((DefaultDenseDateMatrix2D) m).values;
053                            this.values = new Date[v.length];
054                            System.arraycopy(v, 0, this.values, 0, v.length);
055                    } else {
056                            this.values = new Date[rows * cols];
057                            for (long[] c : m.allCoordinates()) {
058                                    setDate(m.getAsDate(c), c);
059                            }
060                    }
061            }
062    
063            public DefaultDenseDateMatrix2D(long... size) {
064                    this.rows = (int) size[ROW];
065                    this.cols = (int) size[COLUMN];
066                    this.size = new long[] { rows, cols };
067                    this.values = new Date[rows * cols];
068                    Arrays.fill(values, DateMatrix.DATE0);
069            }
070    
071            public DefaultDenseDateMatrix2D(Date[] v, int rows, int cols) {
072                    this.rows = rows;
073                    this.cols = cols;
074                    this.size = new long[] { rows, cols };
075                    this.values = v;
076            }
077    
078            public long[] getSize() {
079                    return size;
080            }
081    
082            public long getRowCount() {
083                    return rows;
084            }
085    
086            public long getColumnCount() {
087                    return cols;
088            }
089    
090            public Date getDate(long row, long column) {
091                    return values[(int) (column * rows + row)];
092            }
093    
094            public void setDate(Date value, long row, long column) {
095                    values[(int) (column * rows + row)] = value;
096            }
097    
098            public Date getDate(int row, int column) {
099                    return values[column * rows + row];
100            }
101    
102            public void setDate(Date value, int row, int column) {
103                    values[column * rows + row] = value;
104            }
105    
106            public final Matrix copy() throws MatrixException {
107                    Date[] result = new Date[values.length];
108                    System.arraycopy(values, 0, result, 0, values.length);
109                    Matrix m = new DefaultDenseDateMatrix2D(result, rows, cols);
110                    if (getAnnotation() != null) {
111                            m.setAnnotation(getAnnotation().clone());
112                    }
113                    return m;
114            }
115    
116            public final Matrix transpose() {
117                    final Date[] result = new Date[cols * rows];
118                    for (int c = rows; --c != -1;) {
119                            for (int r = cols; --r != -1;) {
120                                    result[c * cols + r] = values[r * rows + c];
121                            }
122                    }
123                    return new DefaultDenseDateMatrix2D(result, cols, rows);
124            }
125    
126            public Date[] getColumnMajorDateArray1D() {
127                    return values;
128            }
129    
130    }