package search; import java.io.FileInputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.util.List; /** * Compares two search recordings. * * @author Franck van Breugel */ public class CompareSearchRecordings { /** * Compares two search recordings. * * @param args[0] name of the file containing the first serialized search recording * @param args[1] name of the file containing the second serialized search recording */ public static void main(String[] args) { if (args.length != 2) { System.out.println("Use: java Generate "); } else { try { FileInputStream file = new FileInputStream(args[0]); ObjectInputStream stream = new ObjectInputStream(file); List first = (List) stream.readObject(); file = new FileInputStream(args[1]); stream = new ObjectInputStream(file); List second = (List) stream.readObject(); stream.close(); if (!first.equals(second)) { // ensure first.size() >= second.size() if (first.size() < second.size()) { List temp = first; first = second; second = temp; } // find the index where they differ int index = 0; while (index < second.size() && first.get(index).equals(second.get(index))) { index++; } // print both lists in parallel and indicate where they differ first for (int i = 0; i < index; i++) { System.out.printf("%s %s%n", get(first, i), get(second, i)); } System.out.printf("%s <=> %s%n", get(first, index), get(second, index)); for (int i = index + 1; i < first.size(); i++) { System.out.printf("%s %s%n", get(first, i), get(second, i)); } } } catch (IOException | ClassNotFoundException e) { System.out.println("Something went wrong with the file"); } } } /** * Returns the element with the given index of the given list * padded with spaces so that result is 25 characters if the * element exists. Otherwise, 25 spaces. * * @param list a list * @param index an index * @return the element with the given index of the given list, padded with spaces */ private static String get(List list, int index) { final int LENGTH = 25; String result; if (index < list.size()) { result = list.get(index); } else { result = ""; } while (result.length() < LENGTH) { result += " "; } return result; } }