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.calculation;
025    
026    import org.ujmp.core.Coordinates;
027    import org.ujmp.core.Matrix;
028    import org.ujmp.core.MatrixFactory;
029    import org.ujmp.core.annotation.DefaultAnnotation;
030    import org.ujmp.core.enums.ValueType;
031    import org.ujmp.core.exceptions.MatrixException;
032    
033    public class IncludeAnnotation extends AbstractObjectCalculation {
034            private static final long serialVersionUID = -2165678807795583946L;
035    
036            private long[] size = null;
037    
038            public IncludeAnnotation(Matrix m, int dim) {
039                    super(dim, m);
040                    size = Coordinates.copyOf(m.getSize());
041                    size[dim]++;
042                    setAnnotation(new DefaultAnnotation(getSize()));
043                    getAnnotation().setMatrixAnnotation(m.getMatrixAnnotation());
044            }
045    
046            public Object getObject(long... coordinates) throws MatrixException {
047                    coordinates = Coordinates.copyOf(coordinates);
048                    if (coordinates[getDimension()] == 0) {
049                            if (getDimension() == ROW) {
050                                    return getSource().getAxisAnnotation(ROW, coordinates);
051                            } else if (getDimension() == COLUMN) {
052                                    return getSource().getAxisAnnotation(COLUMN, coordinates);
053                            } else {
054                                    throw new MatrixException("only possible for Matrix.ROW and Matrix.COLUMN");
055                            }
056                    } else {
057                            coordinates[getDimension()]--;
058                            return getSource().getAsObject(coordinates);
059                    }
060            }
061    
062            public long[] getSize() {
063                    return size;
064            }
065    
066            public static void main(String[] args) throws Exception {
067                    Matrix m = MatrixFactory.zeros(ValueType.OBJECT, 5, 5);
068                    m.randn(Ret.ORIG);
069                    m.setLabel("test");
070                    m.setColumnLabel(0, "col0");
071                    m.setColumnLabel(1, "col1");
072                    m.setColumnLabel(2, "col2");
073                    m.setColumnLabel(3, "col3");
074                    m.setColumnLabel(4, "col4");
075                    m.setRowLabel(0, "row0");
076                    m.setRowLabel(1, "row1");
077                    m.setRowLabel(2, "row2");
078                    m.setRowLabel(3, "row3");
079                    m.setRowLabel(4, "row4");
080                    m.setAsDouble(Double.NaN, 2, 2);
081                    m.setAsDouble(Double.NEGATIVE_INFINITY, 3, 2);
082                    System.out.println(m);
083                    System.out.println(m.includeAnnotation(Ret.NEW, Matrix.COLUMN));
084            }
085    
086    }