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