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.ArrayList;
027    import java.util.Collections;
028    import java.util.List;
029    
030    import org.ujmp.core.Matrix;
031    import org.ujmp.core.MatrixFactory;
032    import org.ujmp.core.annotation.Annotation;
033    import org.ujmp.core.enums.ValueType;
034    import org.ujmp.core.exceptions.MatrixException;
035    import org.ujmp.core.longmatrix.LongMatrix2D;
036    import org.ujmp.core.util.Sortable;
037    
038    /**
039     * Sorts the rows of a matrix
040     */
041    public class Sortrows extends AbstractObjectCalculation {
042            private static final long serialVersionUID = -6935375114060680121L;
043    
044            private LongMatrix2D index = null;
045    
046            private long column = 0;
047    
048            private boolean reverse = false;
049    
050            public Sortrows(Matrix m, long column, boolean reverse) {
051                    super(m);
052                    this.column = Math.abs(column);
053                    this.reverse = reverse;
054                    createSortIndex();
055            }
056    
057            public Object getObject(long... coordinates) throws MatrixException {
058                    return getSource().getAsObject(index.getLong(coordinates[ROW], 0), coordinates[COLUMN]);
059            }
060    
061            @SuppressWarnings("unchecked")
062            private void createSortIndex() {
063                    Matrix m = getSource();
064                    long rowCount = m.getRowCount();
065                    List<Sortable> rows = new ArrayList<Sortable>();
066    
067                    switch (m.getValueType()) {
068                    case BIGDECIMAL:
069                            for (long r = 0; r < rowCount; r++) {
070                                    Comparable<?> c = (Comparable<?>) m.getAsBigDecimal(r, column);
071                                    Sortable s = new Sortable(c, r, true);
072                                    rows.add(s);
073                            }
074                            break;
075                    case BIGINTEGER:
076                            for (long r = 0; r < rowCount; r++) {
077                                    Comparable<?> c = (Comparable<?>) m.getAsBigInteger(r, column);
078                                    Sortable s = new Sortable(c, r, true);
079                                    rows.add(s);
080                            }
081                            break;
082                    case DATE:
083                            for (long r = 0; r < rowCount; r++) {
084                                    Comparable<?> c = (Comparable<?>) m.getAsDate(r, column);
085                                    Sortable s = new Sortable(c, r, true);
086                                    rows.add(s);
087                            }
088                            break;
089                    case DOUBLE:
090                            for (long r = 0; r < rowCount; r++) {
091                                    Comparable<?> c = (Comparable<?>) m.getAsDouble(r, column);
092                                    Sortable s = new Sortable(c, r, true);
093                                    rows.add(s);
094                            }
095                            break;
096                    case INT:
097                            for (long r = 0; r < rowCount; r++) {
098                                    Comparable<?> c = (Comparable<?>) m.getAsInt(r, column);
099                                    Sortable s = new Sortable(c, r, true);
100                                    rows.add(s);
101                            }
102                            break;
103                    case FLOAT:
104                            for (long r = 0; r < rowCount; r++) {
105                                    Comparable<?> c = (Comparable<?>) m.getAsFloat(r, column);
106                                    Sortable s = new Sortable(c, r, true);
107                                    rows.add(s);
108                            }
109                            break;
110                    case CHAR:
111                            for (long r = 0; r < rowCount; r++) {
112                                    Comparable<?> c = (Comparable<?>) m.getAsChar(r, column);
113                                    Sortable s = new Sortable(c, r, true);
114                                    rows.add(s);
115                            }
116                            break;
117                    case BYTE:
118                            for (long r = 0; r < rowCount; r++) {
119                                    Comparable<?> c = (Comparable<?>) m.getAsByte(r, column);
120                                    Sortable s = new Sortable(c, r, true);
121                                    rows.add(s);
122                            }
123                            break;
124                    case BOOLEAN:
125                            for (long r = 0; r < rowCount; r++) {
126                                    Comparable<?> c = (Comparable<?>) m.getAsBoolean(r, column);
127                                    Sortable s = new Sortable(c, r, true);
128                                    rows.add(s);
129                            }
130                            break;
131                    case LONG:
132                            for (long r = 0; r < rowCount; r++) {
133                                    Comparable<?> c = (Comparable<?>) m.getAsLong(r, column);
134                                    Sortable s = new Sortable(c, r, true);
135                                    rows.add(s);
136                            }
137                            break;
138                    case SHORT:
139                            for (long r = 0; r < rowCount; r++) {
140                                    Comparable<?> c = (Comparable<?>) m.getAsShort(r, column);
141                                    Sortable s = new Sortable(c, r, true);
142                                    rows.add(s);
143                            }
144                            break;
145                    default:
146                            for (long r = 0; r < rowCount; r++) {
147                                    Comparable<?> c = (Comparable<?>) m.getAsString(r, column);
148                                    Sortable s = new Sortable(c, r, true);
149                                    rows.add(s);
150                            }
151                            break;
152                    }
153    
154                    Collections.sort(rows);
155                    if (reverse) {
156                            Collections.reverse(rows);
157                    }
158    
159                    LongMatrix2D indexMatrix = (LongMatrix2D) MatrixFactory.zeros(ValueType.LONG, rows.size(),
160                                    1);
161    
162                    Annotation annotation = m.getAnnotation();
163                    if (annotation != null) {
164                            annotation = m.getAnnotation().clone();
165                            setAnnotation(annotation);
166                    }
167    
168                    for (int r = 0; r < rows.size(); r++) {
169                            indexMatrix.setLong((Long) (rows.get(r)).getObject(), r, 0);
170                            if (annotation != null) {
171                                    Object o = m.getAxisAnnotation(Matrix.COLUMN, new long[] {
172                                                    (Long) (rows.get(r)).getObject(), 0 });
173                                    annotation.setAxisAnnotation(Matrix.COLUMN, o, new long[] { r, 0 });
174                            }
175                    }
176    
177                    this.index = indexMatrix;
178            }
179    
180            public LongMatrix2D getIndex() {
181                    return index;
182            }
183    
184    }