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.io.File;
027    import java.util.HashMap;
028    import java.util.LinkedList;
029    import java.util.List;
030    import java.util.Map;
031    
032    import org.ujmp.core.Matrix;
033    import org.ujmp.core.MatrixFactory;
034    import org.ujmp.core.enums.FileFormat;
035    import org.ujmp.core.exceptions.MatrixException;
036    
037    public class Join extends AbstractObjectCalculation {
038            private static final long serialVersionUID = -4037364843847848445L;
039    
040            private Matrix result = null;
041    
042            private long column1 = 0;
043    
044            private long column2 = 0;
045    
046            public Join(Matrix m1, Matrix m2, long column1, long column2) {
047                    super(m1, m2);
048                    this.column1 = column1;
049                    this.column2 = column2;
050            }
051    
052            
053            public Object getObject(long... coordinates) throws MatrixException {
054                    if (result == null) {
055                            createMatrix();
056                    }
057                    return result.getAsObject(coordinates);
058            }
059    
060            
061            public long[] getSize() {
062                    if (result == null) {
063                            createMatrix();
064                    }
065                    return result.getSize();
066            }
067    
068            private void createMatrix() {
069                    Matrix m1 = getSource();
070                    Matrix m2 = getSources()[1];
071    
072                    Map<Object, List<Long>> right = new HashMap<Object, List<Long>>();
073    
074                    for (long r = 0; r < m2.getRowCount(); r++) {
075                            Object o = m2.getAsObject(r, column2);
076                            List<Long> list = right.get(o);
077                            if (list == null) {
078                                    list = new LinkedList<Long>();
079                                    right.put(o, list);
080                            }
081                            list.add(r);
082                    }
083    
084                    result = MatrixFactory.dense(getValueType(), m1.getRowCount(), m1.getColumnCount()
085                                    + m2.getColumnCount());
086    
087                    for (long[] c : m1.allCoordinates()) {
088                            result.setAsObject(m1.getAsObject(c), c);
089                    }
090    
091                    for (long r = 0; r < m1.getRowCount(); r++) {
092                            Object o = m1.getAsObject(r, column1);
093                            List<Long> list = right.get(o);
094                            if (list != null) {
095                                    long row2 = list.iterator().next();
096                                    for (long c = 0; c < m2.getColumnCount(); c++) {
097                                            result.setAsObject(m2.getAsObject(row2, c), r, c + m1.getColumnCount());
098                                    }
099                            }
100                    }
101            }
102    
103            public static void main(String[] args) throws Exception {
104                    Matrix m1 = MatrixFactory.importFromFile(FileFormat.CSV, new File(
105                                    "c:/Documents and Settings/holger/Desktop/original.log"), "\t");
106                    Matrix m2 = MatrixFactory.importFromFile(FileFormat.CSV, new File(
107                                    "c:/Documents and Settings/holger/Desktop/js.log"), "\t");
108    
109                    new Join(m1, m2, 3, 3).calc(Ret.NEW).exportToFile("c:/test.txt");
110            }
111    }