diff options
Diffstat (limited to 'src/main/java/cuchaz/enigma/convert/ClassMatching.java')
| -rw-r--r-- | src/main/java/cuchaz/enigma/convert/ClassMatching.java | 256 |
1 files changed, 128 insertions, 128 deletions
diff --git a/src/main/java/cuchaz/enigma/convert/ClassMatching.java b/src/main/java/cuchaz/enigma/convert/ClassMatching.java index b05df87..f302f13 100644 --- a/src/main/java/cuchaz/enigma/convert/ClassMatching.java +++ b/src/main/java/cuchaz/enigma/convert/ClassMatching.java | |||
| @@ -8,12 +8,14 @@ | |||
| 8 | * Contributors: | 8 | * Contributors: |
| 9 | * Jeff Martin - initial API and implementation | 9 | * Jeff Martin - initial API and implementation |
| 10 | ******************************************************************************/ | 10 | ******************************************************************************/ |
| 11 | |||
| 11 | package cuchaz.enigma.convert; | 12 | package cuchaz.enigma.convert; |
| 12 | 13 | ||
| 13 | import com.google.common.collect.BiMap; | 14 | import com.google.common.collect.BiMap; |
| 14 | import com.google.common.collect.HashBiMap; | 15 | import com.google.common.collect.HashBiMap; |
| 15 | import com.google.common.collect.Lists; | 16 | import com.google.common.collect.Lists; |
| 16 | import com.google.common.collect.Sets; | 17 | import com.google.common.collect.Sets; |
| 18 | import cuchaz.enigma.mapping.ClassEntry; | ||
| 17 | 19 | ||
| 18 | import java.util.ArrayList; | 20 | import java.util.ArrayList; |
| 19 | import java.util.Collection; | 21 | import java.util.Collection; |
| @@ -21,134 +23,132 @@ import java.util.List; | |||
| 21 | import java.util.Map.Entry; | 23 | import java.util.Map.Entry; |
| 22 | import java.util.Set; | 24 | import java.util.Set; |
| 23 | 25 | ||
| 24 | import cuchaz.enigma.mapping.ClassEntry; | ||
| 25 | |||
| 26 | public class ClassMatching { | 26 | public class ClassMatching { |
| 27 | 27 | ||
| 28 | private ClassForest sourceClasses; | 28 | private ClassForest sourceClasses; |
| 29 | private ClassForest destClasses; | 29 | private ClassForest destClasses; |
| 30 | private BiMap<ClassEntry, ClassEntry> knownMatches; | 30 | private BiMap<ClassEntry, ClassEntry> knownMatches; |
| 31 | 31 | ||
| 32 | public ClassMatching(ClassIdentifier sourceIdentifier, ClassIdentifier destIdentifier) { | 32 | public ClassMatching(ClassIdentifier sourceIdentifier, ClassIdentifier destIdentifier) { |
| 33 | sourceClasses = new ClassForest(sourceIdentifier); | 33 | sourceClasses = new ClassForest(sourceIdentifier); |
| 34 | destClasses = new ClassForest(destIdentifier); | 34 | destClasses = new ClassForest(destIdentifier); |
| 35 | knownMatches = HashBiMap.create(); | 35 | knownMatches = HashBiMap.create(); |
| 36 | } | 36 | } |
| 37 | 37 | ||
| 38 | public void addKnownMatches(BiMap<ClassEntry, ClassEntry> knownMatches) { | 38 | public void addKnownMatches(BiMap<ClassEntry, ClassEntry> knownMatches) { |
| 39 | this.knownMatches.putAll(knownMatches); | 39 | this.knownMatches.putAll(knownMatches); |
| 40 | } | 40 | } |
| 41 | 41 | ||
| 42 | public void match(Iterable<ClassEntry> sourceClasses, Iterable<ClassEntry> destClasses) { | 42 | public void match(Iterable<ClassEntry> sourceClasses, Iterable<ClassEntry> destClasses) { |
| 43 | for (ClassEntry sourceClass : sourceClasses) { | 43 | for (ClassEntry sourceClass : sourceClasses) { |
| 44 | if (!knownMatches.containsKey(sourceClass)) { | 44 | if (!knownMatches.containsKey(sourceClass)) { |
| 45 | this.sourceClasses.add(sourceClass); | 45 | this.sourceClasses.add(sourceClass); |
| 46 | } | 46 | } |
| 47 | } | 47 | } |
| 48 | for (ClassEntry destClass : destClasses) { | 48 | for (ClassEntry destClass : destClasses) { |
| 49 | if (!knownMatches.containsValue(destClass)) { | 49 | if (!knownMatches.containsValue(destClass)) { |
| 50 | this.destClasses.add(destClass); | 50 | this.destClasses.add(destClass); |
| 51 | } | 51 | } |
| 52 | } | 52 | } |
| 53 | } | 53 | } |
| 54 | 54 | ||
| 55 | public Collection<ClassMatch> matches() { | 55 | public Collection<ClassMatch> matches() { |
| 56 | List<ClassMatch> matches = Lists.newArrayList(); | 56 | List<ClassMatch> matches = Lists.newArrayList(); |
| 57 | for (Entry<ClassEntry, ClassEntry> entry : knownMatches.entrySet()) { | 57 | for (Entry<ClassEntry, ClassEntry> entry : knownMatches.entrySet()) { |
| 58 | matches.add(new ClassMatch( | 58 | matches.add(new ClassMatch( |
| 59 | entry.getKey(), | 59 | entry.getKey(), |
| 60 | entry.getValue() | 60 | entry.getValue() |
| 61 | )); | 61 | )); |
| 62 | } | 62 | } |
| 63 | for (ClassIdentity identity : sourceClasses.identities()) { | 63 | for (ClassIdentity identity : sourceClasses.identities()) { |
| 64 | matches.add(new ClassMatch( | 64 | matches.add(new ClassMatch( |
| 65 | sourceClasses.getClasses(identity), | 65 | sourceClasses.getClasses(identity), |
| 66 | destClasses.getClasses(identity) | 66 | destClasses.getClasses(identity) |
| 67 | )); | 67 | )); |
| 68 | } | 68 | } |
| 69 | for (ClassIdentity identity : destClasses.identities()) { | 69 | for (ClassIdentity identity : destClasses.identities()) { |
| 70 | if (!sourceClasses.containsIdentity(identity)) { | 70 | if (!sourceClasses.containsIdentity(identity)) { |
| 71 | matches.add(new ClassMatch( | 71 | matches.add(new ClassMatch( |
| 72 | new ArrayList<>(), | 72 | new ArrayList<>(), |
| 73 | destClasses.getClasses(identity) | 73 | destClasses.getClasses(identity) |
| 74 | )); | 74 | )); |
| 75 | } | 75 | } |
| 76 | } | 76 | } |
| 77 | return matches; | 77 | return matches; |
| 78 | } | 78 | } |
| 79 | 79 | ||
| 80 | public Collection<ClassEntry> sourceClasses() { | 80 | public Collection<ClassEntry> sourceClasses() { |
| 81 | Set<ClassEntry> classes = Sets.newHashSet(); | 81 | Set<ClassEntry> classes = Sets.newHashSet(); |
| 82 | for (ClassMatch match : matches()) { | 82 | for (ClassMatch match : matches()) { |
| 83 | classes.addAll(match.sourceClasses); | 83 | classes.addAll(match.sourceClasses); |
| 84 | } | 84 | } |
| 85 | return classes; | 85 | return classes; |
| 86 | } | 86 | } |
| 87 | 87 | ||
| 88 | public Collection<ClassEntry> destClasses() { | 88 | public Collection<ClassEntry> destClasses() { |
| 89 | Set<ClassEntry> classes = Sets.newHashSet(); | 89 | Set<ClassEntry> classes = Sets.newHashSet(); |
| 90 | for (ClassMatch match : matches()) { | 90 | for (ClassMatch match : matches()) { |
| 91 | classes.addAll(match.destClasses); | 91 | classes.addAll(match.destClasses); |
| 92 | } | 92 | } |
| 93 | return classes; | 93 | return classes; |
| 94 | } | 94 | } |
| 95 | 95 | ||
| 96 | public BiMap<ClassEntry, ClassEntry> uniqueMatches() { | 96 | public BiMap<ClassEntry, ClassEntry> uniqueMatches() { |
| 97 | BiMap<ClassEntry, ClassEntry> uniqueMatches = HashBiMap.create(); | 97 | BiMap<ClassEntry, ClassEntry> uniqueMatches = HashBiMap.create(); |
| 98 | for (ClassMatch match : matches()) { | 98 | for (ClassMatch match : matches()) { |
| 99 | if (match.isMatched() && !match.isAmbiguous()) { | 99 | if (match.isMatched() && !match.isAmbiguous()) { |
| 100 | uniqueMatches.put(match.getUniqueSource(), match.getUniqueDest()); | 100 | uniqueMatches.put(match.getUniqueSource(), match.getUniqueDest()); |
| 101 | } | 101 | } |
| 102 | } | 102 | } |
| 103 | return uniqueMatches; | 103 | return uniqueMatches; |
| 104 | } | 104 | } |
| 105 | 105 | ||
| 106 | public Collection<ClassMatch> ambiguousMatches() { | 106 | public Collection<ClassMatch> ambiguousMatches() { |
| 107 | List<ClassMatch> ambiguousMatches = Lists.newArrayList(); | 107 | List<ClassMatch> ambiguousMatches = Lists.newArrayList(); |
| 108 | for (ClassMatch match : matches()) { | 108 | for (ClassMatch match : matches()) { |
| 109 | if (match.isMatched() && match.isAmbiguous()) { | 109 | if (match.isMatched() && match.isAmbiguous()) { |
| 110 | ambiguousMatches.add(match); | 110 | ambiguousMatches.add(match); |
| 111 | } | 111 | } |
| 112 | } | 112 | } |
| 113 | return ambiguousMatches; | 113 | return ambiguousMatches; |
| 114 | } | 114 | } |
| 115 | 115 | ||
| 116 | public Collection<ClassEntry> unmatchedSourceClasses() { | 116 | public Collection<ClassEntry> unmatchedSourceClasses() { |
| 117 | List<ClassEntry> classes = Lists.newArrayList(); | 117 | List<ClassEntry> classes = Lists.newArrayList(); |
| 118 | for (ClassMatch match : matches()) { | 118 | for (ClassMatch match : matches()) { |
| 119 | if (!match.isMatched() && !match.sourceClasses.isEmpty()) { | 119 | if (!match.isMatched() && !match.sourceClasses.isEmpty()) { |
| 120 | classes.addAll(match.sourceClasses); | 120 | classes.addAll(match.sourceClasses); |
| 121 | } | 121 | } |
| 122 | } | 122 | } |
| 123 | return classes; | 123 | return classes; |
| 124 | } | 124 | } |
| 125 | 125 | ||
| 126 | public Collection<ClassEntry> unmatchedDestClasses() { | 126 | public Collection<ClassEntry> unmatchedDestClasses() { |
| 127 | List<ClassEntry> classes = Lists.newArrayList(); | 127 | List<ClassEntry> classes = Lists.newArrayList(); |
| 128 | for (ClassMatch match : matches()) { | 128 | for (ClassMatch match : matches()) { |
| 129 | if (!match.isMatched() && !match.destClasses.isEmpty()) { | 129 | if (!match.isMatched() && !match.destClasses.isEmpty()) { |
| 130 | classes.addAll(match.destClasses); | 130 | classes.addAll(match.destClasses); |
| 131 | } | 131 | } |
| 132 | } | 132 | } |
| 133 | return classes; | 133 | return classes; |
| 134 | } | 134 | } |
| 135 | 135 | ||
| 136 | @Override | 136 | @Override |
| 137 | public String toString() { | 137 | public String toString() { |
| 138 | 138 | ||
| 139 | // count the ambiguous classes | 139 | // count the ambiguous classes |
| 140 | int numAmbiguousSource = 0; | 140 | int numAmbiguousSource = 0; |
| 141 | int numAmbiguousDest = 0; | 141 | int numAmbiguousDest = 0; |
| 142 | for (ClassMatch match : ambiguousMatches()) { | 142 | for (ClassMatch match : ambiguousMatches()) { |
| 143 | numAmbiguousSource += match.sourceClasses.size(); | 143 | numAmbiguousSource += match.sourceClasses.size(); |
| 144 | numAmbiguousDest += match.destClasses.size(); | 144 | numAmbiguousDest += match.destClasses.size(); |
| 145 | } | 145 | } |
| 146 | 146 | ||
| 147 | String buf = String.format("%20s%8s%8s\n", "", "Source", "Dest") + String | 147 | String buf = String.format("%20s%8s%8s\n", "", "Source", "Dest") + String |
| 148 | .format("%20s%8d%8d\n", "Classes", sourceClasses().size(), destClasses().size()) + String | 148 | .format("%20s%8d%8d\n", "Classes", sourceClasses().size(), destClasses().size()) + String |
| 149 | .format("%20s%8d%8d\n", "Uniquely matched", uniqueMatches().size(), uniqueMatches().size()) + String | 149 | .format("%20s%8d%8d\n", "Uniquely matched", uniqueMatches().size(), uniqueMatches().size()) + String |
| 150 | .format("%20s%8d%8d\n", "Ambiguously matched", numAmbiguousSource, numAmbiguousDest) + String | 150 | .format("%20s%8d%8d\n", "Ambiguously matched", numAmbiguousSource, numAmbiguousDest) + String |
| 151 | .format("%20s%8d%8d\n", "Unmatched", unmatchedSourceClasses().size(), unmatchedDestClasses().size()); | 151 | .format("%20s%8d%8d\n", "Unmatched", unmatchedSourceClasses().size(), unmatchedDestClasses().size()); |
| 152 | return buf; | 152 | return buf; |
| 153 | } | 153 | } |
| 154 | } | 154 | } |