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