diff options
Diffstat (limited to 'src/cuchaz/enigma/convert/MemberMatches.java')
| -rw-r--r-- | src/cuchaz/enigma/convert/MemberMatches.java | 145 |
1 files changed, 145 insertions, 0 deletions
diff --git a/src/cuchaz/enigma/convert/MemberMatches.java b/src/cuchaz/enigma/convert/MemberMatches.java new file mode 100644 index 0000000..1078ab7 --- /dev/null +++ b/src/cuchaz/enigma/convert/MemberMatches.java | |||
| @@ -0,0 +1,145 @@ | |||
| 1 | package cuchaz.enigma.convert; | ||
| 2 | |||
| 3 | import java.util.Collection; | ||
| 4 | import java.util.Set; | ||
| 5 | |||
| 6 | import com.google.common.collect.BiMap; | ||
| 7 | import com.google.common.collect.HashBiMap; | ||
| 8 | import com.google.common.collect.HashMultimap; | ||
| 9 | import com.google.common.collect.Multimap; | ||
| 10 | import com.google.common.collect.Sets; | ||
| 11 | |||
| 12 | import cuchaz.enigma.mapping.ClassEntry; | ||
| 13 | import cuchaz.enigma.mapping.Entry; | ||
| 14 | |||
| 15 | |||
| 16 | public class MemberMatches<T extends Entry> { | ||
| 17 | |||
| 18 | private BiMap<T,T> m_matches; | ||
| 19 | private Multimap<ClassEntry,T> m_matchedSourceEntries; | ||
| 20 | private Multimap<ClassEntry,T> m_unmatchedSourceEntries; | ||
| 21 | private Multimap<ClassEntry,T> m_unmatchedDestEntries; | ||
| 22 | private Multimap<ClassEntry,T> m_unmatchableSourceEntries; | ||
| 23 | |||
| 24 | public MemberMatches() { | ||
| 25 | m_matches = HashBiMap.create(); | ||
| 26 | m_matchedSourceEntries = HashMultimap.create(); | ||
| 27 | m_unmatchedSourceEntries = HashMultimap.create(); | ||
| 28 | m_unmatchedDestEntries = HashMultimap.create(); | ||
| 29 | m_unmatchableSourceEntries = HashMultimap.create(); | ||
| 30 | } | ||
| 31 | |||
| 32 | public void addMatch(T srcEntry, T destEntry) { | ||
| 33 | boolean wasAdded = m_matches.put(srcEntry, destEntry) == null; | ||
| 34 | assert (wasAdded); | ||
| 35 | wasAdded = m_matchedSourceEntries.put(srcEntry.getClassEntry(), srcEntry); | ||
| 36 | assert (wasAdded); | ||
| 37 | } | ||
| 38 | |||
| 39 | public void addUnmatchedSourceEntry(T sourceEntry) { | ||
| 40 | boolean wasAdded = m_unmatchedSourceEntries.put(sourceEntry.getClassEntry(), sourceEntry); | ||
| 41 | assert (wasAdded); | ||
| 42 | } | ||
| 43 | |||
| 44 | public void addUnmatchedSourceEntries(Iterable<T> sourceEntries) { | ||
| 45 | for (T sourceEntry : sourceEntries) { | ||
| 46 | addUnmatchedSourceEntry(sourceEntry); | ||
| 47 | } | ||
| 48 | } | ||
| 49 | |||
| 50 | public void addUnmatchedDestEntry(T destEntry) { | ||
| 51 | boolean wasAdded = m_unmatchedDestEntries.put(destEntry.getClassEntry(), destEntry); | ||
| 52 | assert (wasAdded); | ||
| 53 | } | ||
| 54 | |||
| 55 | public void addUnmatchedDestEntries(Iterable<T> destEntriesntries) { | ||
| 56 | for (T entry : destEntriesntries) { | ||
| 57 | addUnmatchedDestEntry(entry); | ||
| 58 | } | ||
| 59 | } | ||
| 60 | |||
| 61 | public void addUnmatchableSourceEntry(T sourceEntry) { | ||
| 62 | boolean wasAdded = m_unmatchableSourceEntries.put(sourceEntry.getClassEntry(), sourceEntry); | ||
| 63 | assert (wasAdded); | ||
| 64 | } | ||
| 65 | |||
| 66 | public Set<ClassEntry> getSourceClassesWithUnmatchedEntries() { | ||
| 67 | return m_unmatchedSourceEntries.keySet(); | ||
| 68 | } | ||
| 69 | |||
| 70 | public Collection<ClassEntry> getSourceClassesWithoutUnmatchedEntries() { | ||
| 71 | Set<ClassEntry> out = Sets.newHashSet(); | ||
| 72 | out.addAll(m_matchedSourceEntries.keySet()); | ||
| 73 | out.removeAll(m_unmatchedSourceEntries.keySet()); | ||
| 74 | return out; | ||
| 75 | } | ||
| 76 | |||
| 77 | public Collection<T> getUnmatchedSourceEntries() { | ||
| 78 | return m_unmatchedSourceEntries.values(); | ||
| 79 | } | ||
| 80 | |||
| 81 | public Collection<T> getUnmatchedSourceEntries(ClassEntry sourceClass) { | ||
| 82 | return m_unmatchedSourceEntries.get(sourceClass); | ||
| 83 | } | ||
| 84 | |||
| 85 | public Collection<T> getUnmatchedDestEntries() { | ||
| 86 | return m_unmatchedDestEntries.values(); | ||
| 87 | } | ||
| 88 | |||
| 89 | public Collection<T> getUnmatchedDestEntries(ClassEntry destClass) { | ||
| 90 | return m_unmatchedDestEntries.get(destClass); | ||
| 91 | } | ||
| 92 | |||
| 93 | public Collection<T> getUnmatchableSourceEntries() { | ||
| 94 | return m_unmatchableSourceEntries.values(); | ||
| 95 | } | ||
| 96 | |||
| 97 | public boolean hasSource(T sourceEntry) { | ||
| 98 | return m_matches.containsKey(sourceEntry) || m_unmatchedSourceEntries.containsValue(sourceEntry); | ||
| 99 | } | ||
| 100 | |||
| 101 | public boolean hasDest(T destEntry) { | ||
| 102 | return m_matches.containsValue(destEntry) || m_unmatchedDestEntries.containsValue(destEntry); | ||
| 103 | } | ||
| 104 | |||
| 105 | public BiMap<T,T> matches() { | ||
| 106 | return m_matches; | ||
| 107 | } | ||
| 108 | |||
| 109 | public boolean isMatchedSourceEntry(T sourceEntry) { | ||
| 110 | return m_matches.containsKey(sourceEntry); | ||
| 111 | } | ||
| 112 | |||
| 113 | public boolean isMatchedDestEntry(T destEntry) { | ||
| 114 | return m_matches.containsValue(destEntry); | ||
| 115 | } | ||
| 116 | |||
| 117 | public void makeMatch(T sourceEntry, T destEntry) { | ||
| 118 | boolean wasRemoved = m_unmatchedSourceEntries.remove(sourceEntry.getClassEntry(), sourceEntry); | ||
| 119 | assert (wasRemoved); | ||
| 120 | wasRemoved = m_unmatchedDestEntries.remove(destEntry.getClassEntry(), destEntry); | ||
| 121 | assert (wasRemoved); | ||
| 122 | addMatch(sourceEntry, destEntry); | ||
| 123 | } | ||
| 124 | |||
| 125 | public boolean isMatched(T sourceEntry, T destEntry) { | ||
| 126 | T match = m_matches.get(sourceEntry); | ||
| 127 | return match != null && match.equals(destEntry); | ||
| 128 | } | ||
| 129 | |||
| 130 | public void unmakeMatch(T sourceEntry, T destEntry) { | ||
| 131 | boolean wasRemoved = m_matches.remove(sourceEntry) != null; | ||
| 132 | assert (wasRemoved); | ||
| 133 | wasRemoved = m_matchedSourceEntries.remove(sourceEntry.getClassEntry(), sourceEntry); | ||
| 134 | assert (wasRemoved); | ||
| 135 | addUnmatchedSourceEntry(sourceEntry); | ||
| 136 | addUnmatchedDestEntry(destEntry); | ||
| 137 | } | ||
| 138 | |||
| 139 | public void makeSourceUnmatchable(T sourceEntry) { | ||
| 140 | assert(!isMatchedSourceEntry(sourceEntry)); | ||
| 141 | boolean wasRemoved = m_unmatchedSourceEntries.remove(sourceEntry.getClassEntry(), sourceEntry); | ||
| 142 | assert (wasRemoved); | ||
| 143 | addUnmatchableSourceEntry(sourceEntry); | ||
| 144 | } | ||
| 145 | } | ||