/* * [Join.java] * * Summary: Joins two tables in.html and other.html producing out.html Other is positioned to the right. * * Copyright: (c) 2007-2017 Roedy Green, Canadian Mind Products, http://mindprod.com * * Licence: This software may be copied and used freely for any purpose but military. * http://mindprod.com/contact/nonmil.html * * Requires: JDK 1.8+ * * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/ * * Version History: * 1.1 2007-05-03 */ package com.mindprod.reorg; import java.io.IOException; import java.util.Arrays; import static java.lang.System.*; /** * Joins two tables in.html and other.html producing out.html Other is positioned to the right. *

* In.html is padded with empty * cells on the right as needed so that other's column all align. * Join 0 sorts both on column 9 before blind join. Does not merge based on matching key. * * @author Roedy Green, Canadian Mind Products * @version 1.1 2007-05-03 * @since 2007 */ public final class Join extends Common { private static final int FIRST_COPYRIGHT_YEAR = 2007; /** * non-displaying copyright. */ @SuppressWarnings( { "UnusedDeclaration" } ) private static final String EMBEDDED_COPYRIGHT = "Copyright: (c) 2007-2017 Roedy Green, Canadian Mind Products, http://mindprod.com"; /** * main join two matrices putting them side by side, html is in E:\temp\in.html and E:\temp\other.html result goes * to E:\temp\out.html * * @param args ignored * * @throws java.io.IOException if any problem reading or writing the html */ public static void main( String[] args ) throws IOException { // might heed to sort each matrix first based on column. /** * Which 0-based column we are sorting on */ final int leftColToSort; final int rightColToSort; if ( args.length == 0 ) { leftColToSort = -1; rightColToSort = -1; out.println( "joining without sorting first." ); } else if ( args.length == 1 ) { leftColToSort = Integer.parseInt( args[ 0 ] ); rightColToSort = leftColToSort; out.println( "joining with pre-sort on column " + leftColToSort ); } else { leftColToSort = Integer.parseInt( args[ 0 ] ); rightColToSort = Integer.parseInt( args[ 1 ] ); out.println( "joining with pre-sort on columns " + leftColToSort + " and " + rightColToSort ); } final String[][] leftMatrix = getMatrix( IN_FILENAME ); final int leftRows = rows( leftMatrix ); final int leftCols = cols( leftMatrix ); displayRowAndColCounts( "left", leftRows, leftCols ); final String[][] rightMatrix = getMatrix( OTHER_IN_FILENAME ); final int rightRows = rows( rightMatrix ); final int rightCols = cols( rightMatrix ); displayRowAndColCounts( "right", rightRows, rightCols ); // trim lead/trail spaces each cell. Do not remove any cells. trimMatrix( leftMatrix ); trimMatrix( rightMatrix ); final int joinedRows = Math.max( leftRows, rightRows ); final int joinedCols = leftCols + rightCols; displayRowAndColCounts( "joined", joinedRows, joinedCols ); if ( leftRows != rightRows ) { err.println( "Warning: tables have a different number of columns. Mismatches likely." ); } if ( leftColToSort >= 0 ) { // we need to sort the two inputs before the join final boolean numericSort = isColNumeric( leftMatrix, leftColToSort ) && isColNumeric( rightMatrix, rightColToSort ); Arrays.sort( leftMatrix, numericSort ? new NumMatrixComparator( leftColToSort ) : new AlphaMatrixComparator( leftColToSort ) ); Arrays.sort( rightMatrix, numericSort ? new NumMatrixComparator( rightColToSort ) : new AlphaMatrixComparator( rightColToSort ) ); } final String[][] joinedMatrix = new String[ joinedRows ][ joinedCols ]; // initialise so all slots will have something, // even if nothing copied into them. for ( int row = 0; row < joinedRows; row++ ) { for ( int col = 0; col < joinedCols; col++ ) { joinedMatrix[ row ][ col ] = ""; } } // copy the left matrix into the joined matrix for ( int row = 0; row < leftRows; row++ ) { System.arraycopy( leftMatrix[ row ], 0, joinedMatrix[ row ], 0, leftCols ); } // copy the right matrix beside left one in the joined matrix for ( int row = 0; row < rightRows; row++ ) { System.arraycopy( rightMatrix[ row ], 0, joinedMatrix[ row ], leftCols, rightCols ); } writeMatrix( joinedMatrix, OUT_FILENAME ); } // end main } // Join