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 java.util.Collection;
027    
028    import org.ujmp.core.Matrix;
029    import org.ujmp.core.annotation.Annotation;
030    import org.ujmp.core.annotation.DefaultAnnotation;
031    import org.ujmp.core.exceptions.MatrixException;
032    import org.ujmp.core.util.MathUtil;
033    import org.ujmp.core.util.StringUtil;
034    
035    public class Selection extends AbstractObjectCalculation {
036            private static final long serialVersionUID = 4576183558391811345L;
037    
038            private long[][] selection = null;
039    
040            public Selection(Matrix m, String selectionString) {
041                    this(m, StringUtil.parseSelection(selectionString, m.getSize()));
042            }
043    
044            public Selection(Matrix m, Collection<? extends Number>... selection) {
045                    super(m);
046                    this.selection = new long[selection.length][];
047                    if (selection[ROW] != null) {
048                            this.selection[ROW] = MathUtil.collectionToLong(selection[ROW]);
049                    }
050                    if (selection[COLUMN] != null) {
051                            this.selection[COLUMN] = MathUtil.collectionToLong(selection[COLUMN]);
052                    }
053                    createAnnotation();
054            }
055    
056            public Selection(Matrix m, long[]... selection) {
057                    super(m);
058                    this.selection = selection;
059                    createAnnotation();
060            }
061    
062            private void createAnnotation() {
063                    if (getSource().getDimensionCount() != 2) {
064                            throw new MatrixException("only supported for 2d matrices");
065                    }
066                    Annotation a = getSource().getAnnotation();
067                    if (a != null) {
068                            Annotation anew = new DefaultAnnotation(getSize());
069                            anew.setMatrixAnnotation(a.getMatrixAnnotation());
070                            if (selection[ROW] == null) {
071                                    long rowCount = getSource().getRowCount();
072                                    for (int r = 0; r < rowCount; r++) {
073                                            anew.setAxisAnnotation(Matrix.COLUMN, a.getAxisAnnotation(Matrix.COLUMN, r, 0),
074                                                            r, 0);
075                                    }
076                            } else {
077                                    for (int r = 0; r < selection[ROW].length; r++) {
078                                            anew.setAxisAnnotation(Matrix.COLUMN, a.getAxisAnnotation(Matrix.COLUMN,
079                                                            selection[ROW][r], 0), r, 0);
080                                    }
081                            }
082                            if (selection[COLUMN] == null) {
083                                    long colCount = getSource().getColumnCount();
084                                    for (int c = 0; c < colCount; c++) {
085                                            anew.setAxisAnnotation(Matrix.ROW, a.getAxisAnnotation(Matrix.ROW, 0, c), 0, c);
086                                    }
087                            } else {
088                                    for (int c = 0; c < selection[COLUMN].length; c++) {
089                                            anew.setAxisAnnotation(Matrix.ROW, a.getAxisAnnotation(Matrix.ROW, 0,
090                                                            selection[COLUMN][c]), 0, c);
091                                    }
092                            }
093                            setAnnotation(anew);
094                    }
095            }
096    
097            public Object getObject(long... coordinates) throws MatrixException {
098                    if (selection[ROW] != null && selection[COLUMN] != null) {
099                            return getSource().getAsObject(selection[ROW][(int) coordinates[ROW]],
100                                            selection[COLUMN][(int) coordinates[COLUMN]]);
101                    } else {
102                            if (selection[ROW] == null) {
103                                    return getSource().getAsObject(coordinates[ROW],
104                                                    selection[COLUMN][(int) coordinates[COLUMN]]);
105                            } else {
106                                    return getSource().getAsObject(selection[ROW][(int) coordinates[ROW]],
107                                                    coordinates[COLUMN]);
108                            }
109                    }
110            }
111    
112            public long[] getSize() {
113                    if (selection[ROW] != null && selection[COLUMN] != null) {
114                            return new long[] { selection[ROW].length, selection[COLUMN].length };
115                    } else {
116                            if (selection[ROW] == null) {
117                                    return new long[] { getSource().getRowCount(), selection[COLUMN].length };
118                            } else {
119                                    return new long[] { selection[ROW].length, getSource().getColumnCount() };
120                            }
121                    }
122            }
123    
124            public void setObject(Object value, long... coordinates) throws MatrixException {
125                    if (selection[ROW] != null && selection[COLUMN] != null) {
126                            getSource().setAsObject(value, selection[ROW][(int) coordinates[ROW]],
127                                            selection[COLUMN][(int) coordinates[COLUMN]]);
128                    } else {
129                            if (selection[ROW] == null) {
130                                    getSource().setAsObject(value, coordinates[ROW],
131                                                    selection[COLUMN][(int) coordinates[COLUMN]]);
132                            } else {
133                                    getSource().setAsObject(value, selection[ROW][(int) coordinates[ROW]],
134                                                    coordinates[COLUMN]);
135                            }
136                    }
137            }
138    
139    }