001    /*
002     * Copyright 2011 Christian Kumpe http://kumpe.de/christian/java
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *     http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package de.kumpe.hadooptimizer.examples.analysis;
017    
018    import java.io.BufferedReader;
019    import java.io.FileReader;
020    import java.io.IOException;
021    import java.io.PrintWriter;
022    import java.util.Arrays;
023    
024    public class Merge {
025            public static void main(final String[] args) throws IOException {
026                    final BufferedReader[] ins = new BufferedReader[args.length - 1];
027                    for (int i = 0; i < args.length - 1; i++) {
028                            ins[i] = new BufferedReader(new FileReader(args[i]));
029                    }
030                    final PrintWriter out = new PrintWriter(args[args.length - 1]);
031                    String line;
032                    out.println("# merged " + Arrays.toString(args));
033                    outer: for (;;) {
034                            for (final BufferedReader in : ins) {
035                                    line = in.readLine();
036                                    if (null == line) {
037                                            break outer;
038                                    }
039                                    if (line.startsWith("#")) {
040                                            out.println(line);
041                                            continue;
042                                    }
043                                    final double[] values = parseLine(line);
044                                    if (null == values) {
045                                            continue;
046                                    }
047                                    for (final double value : values) {
048                                            out.print(value);
049                                            out.print('\t');
050                                    }
051                            }
052                            out.println();
053                    }
054                    for (final BufferedReader in : ins) {
055                            in.close();
056                    }
057                    out.close();
058            }
059    
060            private static double[] parseLine(final String line) {
061                    final String[] parts = line.split("[ \\t\\[\\],]+");
062                    final double[] result = new double[parts.length];
063                    for (int i = 0; i < parts.length; i++) {
064                            result[i] = Double.parseDouble(parts[i]);
065                    }
066                    return result;
067            }
068    }