diff options
Diffstat (limited to 'src/cuchaz/enigma/convert/ClassMatches.java')
| -rw-r--r-- | src/cuchaz/enigma/convert/ClassMatches.java | 163 |
1 files changed, 163 insertions, 0 deletions
diff --git a/src/cuchaz/enigma/convert/ClassMatches.java b/src/cuchaz/enigma/convert/ClassMatches.java new file mode 100644 index 0000000..f70c180 --- /dev/null +++ b/src/cuchaz/enigma/convert/ClassMatches.java | |||
| @@ -0,0 +1,163 @@ | |||
| 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 | * | ||
| 8 | * Contributors: | ||
| 9 | * Jeff Martin - initial API and implementation | ||
| 10 | ******************************************************************************/ | ||
| 11 | package cuchaz.enigma.convert; | ||
| 12 | |||
| 13 | import java.util.ArrayList; | ||
| 14 | import java.util.Collection; | ||
| 15 | import java.util.Iterator; | ||
| 16 | import java.util.Map; | ||
| 17 | import java.util.Set; | ||
| 18 | |||
| 19 | import com.google.common.collect.BiMap; | ||
| 20 | import com.google.common.collect.HashBiMap; | ||
| 21 | import com.google.common.collect.Maps; | ||
| 22 | import com.google.common.collect.Sets; | ||
| 23 | |||
| 24 | import cuchaz.enigma.mapping.ClassEntry; | ||
| 25 | |||
| 26 | |||
| 27 | public class ClassMatches implements Iterable<ClassMatch> { | ||
| 28 | |||
| 29 | Collection<ClassMatch> m_matches; | ||
| 30 | Map<ClassEntry,ClassMatch> m_matchesBySource; | ||
| 31 | Map<ClassEntry,ClassMatch> m_matchesByDest; | ||
| 32 | BiMap<ClassEntry,ClassEntry> m_uniqueMatches; | ||
| 33 | Map<ClassEntry,ClassMatch> m_ambiguousMatchesBySource; | ||
| 34 | Map<ClassEntry,ClassMatch> m_ambiguousMatchesByDest; | ||
| 35 | Set<ClassEntry> m_unmatchedSourceClasses; | ||
| 36 | Set<ClassEntry> m_unmatchedDestClasses; | ||
| 37 | |||
| 38 | public ClassMatches() { | ||
| 39 | this(new ArrayList<ClassMatch>()); | ||
| 40 | } | ||
| 41 | |||
| 42 | public ClassMatches(Collection<ClassMatch> matches) { | ||
| 43 | m_matches = matches; | ||
| 44 | m_matchesBySource = Maps.newHashMap(); | ||
| 45 | m_matchesByDest = Maps.newHashMap(); | ||
| 46 | m_uniqueMatches = HashBiMap.create(); | ||
| 47 | m_ambiguousMatchesBySource = Maps.newHashMap(); | ||
| 48 | m_ambiguousMatchesByDest = Maps.newHashMap(); | ||
| 49 | m_unmatchedSourceClasses = Sets.newHashSet(); | ||
| 50 | m_unmatchedDestClasses = Sets.newHashSet(); | ||
| 51 | |||
| 52 | for (ClassMatch match : matches) { | ||
| 53 | indexMatch(match); | ||
| 54 | } | ||
| 55 | } | ||
| 56 | |||
| 57 | public void add(ClassMatch match) { | ||
| 58 | m_matches.add(match); | ||
| 59 | indexMatch(match); | ||
| 60 | } | ||
| 61 | |||
| 62 | public void remove(ClassMatch match) { | ||
| 63 | for (ClassEntry sourceClass : match.sourceClasses) { | ||
| 64 | m_matchesBySource.remove(sourceClass); | ||
| 65 | m_uniqueMatches.remove(sourceClass); | ||
| 66 | m_ambiguousMatchesBySource.remove(sourceClass); | ||
| 67 | m_unmatchedSourceClasses.remove(sourceClass); | ||
| 68 | } | ||
| 69 | for (ClassEntry destClass : match.destClasses) { | ||
| 70 | m_matchesByDest.remove(destClass); | ||
| 71 | m_uniqueMatches.inverse().remove(destClass); | ||
| 72 | m_ambiguousMatchesByDest.remove(destClass); | ||
| 73 | m_unmatchedDestClasses.remove(destClass); | ||
| 74 | } | ||
| 75 | m_matches.remove(match); | ||
| 76 | } | ||
| 77 | |||
| 78 | public int size() { | ||
| 79 | return m_matches.size(); | ||
| 80 | } | ||
| 81 | |||
| 82 | @Override | ||
| 83 | public Iterator<ClassMatch> iterator() { | ||
| 84 | return m_matches.iterator(); | ||
| 85 | } | ||
| 86 | |||
| 87 | private void indexMatch(ClassMatch match) { | ||
| 88 | if (!match.isMatched()) { | ||
| 89 | // unmatched | ||
| 90 | m_unmatchedSourceClasses.addAll(match.sourceClasses); | ||
| 91 | m_unmatchedDestClasses.addAll(match.destClasses); | ||
| 92 | } else { | ||
| 93 | if (match.isAmbiguous()) { | ||
| 94 | // ambiguously matched | ||
| 95 | for (ClassEntry entry : match.sourceClasses) { | ||
| 96 | m_ambiguousMatchesBySource.put(entry, match); | ||
| 97 | } | ||
| 98 | for (ClassEntry entry : match.destClasses) { | ||
| 99 | m_ambiguousMatchesByDest.put(entry, match); | ||
| 100 | } | ||
| 101 | } else { | ||
| 102 | // uniquely matched | ||
| 103 | m_uniqueMatches.put(match.getUniqueSource(), match.getUniqueDest()); | ||
| 104 | } | ||
| 105 | } | ||
| 106 | for (ClassEntry entry : match.sourceClasses) { | ||
| 107 | m_matchesBySource.put(entry, match); | ||
| 108 | } | ||
| 109 | for (ClassEntry entry : match.destClasses) { | ||
| 110 | m_matchesByDest.put(entry, match); | ||
| 111 | } | ||
| 112 | } | ||
| 113 | |||
| 114 | public BiMap<ClassEntry,ClassEntry> getUniqueMatches() { | ||
| 115 | return m_uniqueMatches; | ||
| 116 | } | ||
| 117 | |||
| 118 | public Set<ClassEntry> getUnmatchedSourceClasses() { | ||
| 119 | return m_unmatchedSourceClasses; | ||
| 120 | } | ||
| 121 | |||
| 122 | public Set<ClassEntry> getUnmatchedDestClasses() { | ||
| 123 | return m_unmatchedDestClasses; | ||
| 124 | } | ||
| 125 | |||
| 126 | public Set<ClassEntry> getAmbiguouslyMatchedSourceClasses() { | ||
| 127 | return m_ambiguousMatchesBySource.keySet(); | ||
| 128 | } | ||
| 129 | |||
| 130 | public ClassMatch getAmbiguousMatchBySource(ClassEntry sourceClass) { | ||
| 131 | return m_ambiguousMatchesBySource.get(sourceClass); | ||
| 132 | } | ||
| 133 | |||
| 134 | public ClassMatch getMatchBySource(ClassEntry sourceClass) { | ||
| 135 | return m_matchesBySource.get(sourceClass); | ||
| 136 | } | ||
| 137 | |||
| 138 | public ClassMatch getMatchByDest(ClassEntry destClass) { | ||
| 139 | return m_matchesByDest.get(destClass); | ||
| 140 | } | ||
| 141 | |||
| 142 | public void removeSource(ClassEntry sourceClass) { | ||
| 143 | ClassMatch match = m_matchesBySource.get(sourceClass); | ||
| 144 | if (match != null) { | ||
| 145 | remove(match); | ||
| 146 | match.sourceClasses.remove(sourceClass); | ||
| 147 | if (!match.sourceClasses.isEmpty() || !match.destClasses.isEmpty()) { | ||
| 148 | add(match); | ||
| 149 | } | ||
| 150 | } | ||
| 151 | } | ||
| 152 | |||
| 153 | public void removeDest(ClassEntry destClass) { | ||
| 154 | ClassMatch match = m_matchesByDest.get(destClass); | ||
| 155 | if (match != null) { | ||
| 156 | remove(match); | ||
| 157 | match.destClasses.remove(destClass); | ||
| 158 | if (!match.sourceClasses.isEmpty() || !match.destClasses.isEmpty()) { | ||
| 159 | add(match); | ||
| 160 | } | ||
| 161 | } | ||
| 162 | } | ||
| 163 | } | ||