Created
February 18, 2016 13:18
-
-
Save craigotis/4815eddf7c191051377e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.Collection; | |
import java.util.Collections; | |
import java.util.Comparator; | |
import java.util.List; | |
import java.util.Optional; | |
import java.util.stream.Collectors; | |
import org.apache.commons.lang.StringUtils; | |
/** | |
* A quick took to diff two Eclipse run configurations. You need to open the two configurations and extract | |
* the "selected_target_plugins" strings, and have those handy. | |
* | |
* @author Craig Otis | |
* | |
*/ | |
public class RunConfigDiffer { | |
private static Comparator<Entry> PLUGIN_IDENTITY_COMPARATOR = (Entry o1, Entry o2) -> { | |
if (!StringUtils.equals(o1.pluginVersion, o2.pluginVersion)) { | |
return 1; | |
} | |
return StringUtils.equals(o1.pluginName, o2.pluginName) ? 0 : 1; | |
}; | |
private static Comparator<Entry> DEEP_COMPARATOR = (Entry o1, Entry o2) -> { | |
if (!StringUtils.equals(o1.runlevel, o2.runlevel)) { | |
return 1; | |
} | |
if (!StringUtils.equals(o1.startByDefault, o2.startByDefault)) { | |
return 1; | |
} | |
return 0; | |
}; | |
public static void main(String[] args) throws IOException { | |
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); | |
System.out.print( | |
"Please enter the VALUE of your \"selected_target_plugins\" entry from the first run configuration: "); | |
String first = bufferedReader.readLine(); | |
System.out.print("Hey, thanks. How about the second: "); | |
String second = bufferedReader.readLine(); | |
List<String> firstStrings = Arrays.asList(StringUtils.split(first, ",")); | |
Collections.sort(firstStrings); | |
List<String> secondStrings = Arrays.asList(StringUtils.split(second, ",")); | |
Collections.sort(secondStrings); | |
List<Entry> firstEntries = firstStrings.stream().map(s -> Entry.fromString(s)) | |
.collect(Collectors.toList()); | |
List<Entry> secondEntries = secondStrings.stream().map(s -> Entry.fromString(s)) | |
.collect(Collectors.toList()); | |
System.out.println(" ################## "); | |
System.out.println("Entries in first: " + firstEntries.size()); | |
System.out.println("Entries in second: " + firstEntries.size()); | |
System.out.println(" ################## "); | |
System.out.println( | |
"Only in first: " + subtract(firstEntries, secondEntries, PLUGIN_IDENTITY_COMPARATOR)); | |
System.out.println( | |
"Only in second: " + subtract(secondEntries, firstEntries, PLUGIN_IDENTITY_COMPARATOR)); | |
System.out.println("Differences: "); | |
for (Pair<?> pair : unionDiff(firstEntries, secondEntries, PLUGIN_IDENTITY_COMPARATOR, | |
DEEP_COMPARATOR)) { | |
System.out.println(" " + pair); | |
} | |
} | |
public static <T> List<Pair<T>> unionDiff(Collection<T> first, Collection<T> second, | |
Comparator<T> comparator, Comparator<T> deepComparator) { | |
List<Pair<T>> union = new ArrayList<>(); | |
first.stream().forEach(o1 -> { | |
second.stream().forEach(o2 -> { | |
if (comparator.compare(o1, o2) == 0) { | |
if (deepComparator.compare(o1, o2) != 0) { | |
union.add(new Pair<T>(o1, o2)); | |
} | |
} | |
}); | |
}); | |
return union; | |
} | |
public static <T> List<T> subtract(Collection<T> first, Collection<T> second, Comparator<T> comparator) { | |
List<T> onlyInFirst = new ArrayList<T>(); | |
first.stream().forEach(p -> { | |
Optional<T> findAny = second.stream().filter(s -> { | |
return comparator.compare(p, s) == 0; | |
}).findAny(); | |
if (!findAny.isPresent()) { | |
onlyInFirst.add(p); | |
} | |
}); | |
return onlyInFirst; | |
} | |
public static class Pair<T> { | |
private T one; | |
private T two; | |
public Pair(T one, T two) { | |
this.one = one; | |
this.two = two; | |
} | |
@Override | |
public String toString() { | |
return one + " < - > " + two; | |
} | |
} | |
public static class Entry { | |
public final String pluginName; | |
public final String pluginVersion; | |
public final String runlevel; | |
public final String startByDefault; | |
public Entry(String pluginName, String pluginVersion, String runlevel, String startByDefault) { | |
this.pluginName = pluginName; | |
this.pluginVersion = pluginVersion; | |
this.runlevel = runlevel; | |
this.startByDefault = startByDefault; | |
} | |
@Override | |
public String toString() { | |
return pluginName + " v" + pluginVersion + " RunLevel: " + runlevel + " AutoStart: " | |
+ startByDefault; | |
} | |
public static Entry fromString(String s) { | |
String[] initialSplit = StringUtils.split(s, "@"); | |
String[] nameAndVersion = StringUtils.split(initialSplit[0], "*"); | |
String pluginName = nameAndVersion[0]; | |
String pluginVersion = nameAndVersion.length > 1 ? nameAndVersion[1] : StringUtils.EMPTY; | |
String runlevel = StringUtils.split(initialSplit[1], ":")[0]; | |
String startByDefault = StringUtils.split(initialSplit[1], ":")[1]; | |
return new Entry(pluginName, pluginVersion, runlevel, startByDefault); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment