summaryrefslogtreecommitdiff
path: root/src/cuchaz/enigma/convert/ClassMatching.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/cuchaz/enigma/convert/ClassMatching.java')
-rw-r--r--src/cuchaz/enigma/convert/ClassMatching.java152
1 files changed, 61 insertions, 91 deletions
diff --git a/src/cuchaz/enigma/convert/ClassMatching.java b/src/cuchaz/enigma/convert/ClassMatching.java
index e45c0e1..53b6f7f 100644
--- a/src/cuchaz/enigma/convert/ClassMatching.java
+++ b/src/cuchaz/enigma/convert/ClassMatching.java
@@ -24,180 +24,150 @@ import com.google.common.collect.Lists;
24import com.google.common.collect.Maps; 24import com.google.common.collect.Maps;
25import com.google.common.collect.Multimap; 25import com.google.common.collect.Multimap;
26 26
27public class ClassMatching 27public class ClassMatching {
28{ 28
29 private Multimap<ClassIdentity,ClassIdentity> m_sourceClasses; 29 private Multimap<ClassIdentity,ClassIdentity> m_sourceClasses;
30 private Multimap<ClassIdentity,ClassIdentity> m_matchedDestClasses; 30 private Multimap<ClassIdentity,ClassIdentity> m_matchedDestClasses;
31 private List<ClassIdentity> m_unmatchedDestClasses; 31 private List<ClassIdentity> m_unmatchedDestClasses;
32 32
33 public ClassMatching( ) 33 public ClassMatching() {
34 {
35 m_sourceClasses = ArrayListMultimap.create(); 34 m_sourceClasses = ArrayListMultimap.create();
36 m_matchedDestClasses = ArrayListMultimap.create(); 35 m_matchedDestClasses = ArrayListMultimap.create();
37 m_unmatchedDestClasses = Lists.newArrayList(); 36 m_unmatchedDestClasses = Lists.newArrayList();
38 } 37 }
39 38
40 public void addSource( ClassIdentity c ) 39 public void addSource(ClassIdentity c) {
41 { 40 m_sourceClasses.put(c, c);
42 m_sourceClasses.put( c, c );
43 } 41 }
44 42
45 public void matchDestClass( ClassIdentity destClass ) 43 public void matchDestClass(ClassIdentity destClass) {
46 { 44 Collection<ClassIdentity> matchedSourceClasses = m_sourceClasses.get(destClass);
47 Collection<ClassIdentity> matchedSourceClasses = m_sourceClasses.get( destClass ); 45 if (matchedSourceClasses.isEmpty()) {
48 if( matchedSourceClasses.isEmpty() )
49 {
50 // no match 46 // no match
51 m_unmatchedDestClasses.add( destClass ); 47 m_unmatchedDestClasses.add(destClass);
52 } 48 } else {
53 else
54 {
55 // found a match 49 // found a match
56 m_matchedDestClasses.put( destClass, destClass ); 50 m_matchedDestClasses.put(destClass, destClass);
57 51
58 // DEBUG 52 // DEBUG
59 ClassIdentity sourceClass = matchedSourceClasses.iterator().next(); 53 ClassIdentity sourceClass = matchedSourceClasses.iterator().next();
60 assert( sourceClass.hashCode() == destClass.hashCode() ); 54 assert (sourceClass.hashCode() == destClass.hashCode());
61 assert( sourceClass.equals( destClass ) ); 55 assert (sourceClass.equals(destClass));
62 } 56 }
63 } 57 }
64 58
65 public void removeSource( ClassIdentity sourceClass ) 59 public void removeSource(ClassIdentity sourceClass) {
66 { 60 m_sourceClasses.remove(sourceClass, sourceClass);
67 m_sourceClasses.remove( sourceClass, sourceClass );
68 } 61 }
69 62
70 public void removeDest( ClassIdentity destClass ) 63 public void removeDest(ClassIdentity destClass) {
71 { 64 m_matchedDestClasses.remove(destClass, destClass);
72 m_matchedDestClasses.remove( destClass, destClass ); 65 m_unmatchedDestClasses.remove(destClass);
73 m_unmatchedDestClasses.remove( destClass );
74 } 66 }
75 67
76 public List<ClassIdentity> getSourceClasses( ) 68 public List<ClassIdentity> getSourceClasses() {
77 { 69 return new ArrayList<ClassIdentity>(m_sourceClasses.values());
78 return new ArrayList<ClassIdentity>( m_sourceClasses.values() );
79 } 70 }
80 71
81 public List<ClassIdentity> getDestClasses( ) 72 public List<ClassIdentity> getDestClasses() {
82 {
83 List<ClassIdentity> classes = Lists.newArrayList(); 73 List<ClassIdentity> classes = Lists.newArrayList();
84 classes.addAll( m_matchedDestClasses.values() ); 74 classes.addAll(m_matchedDestClasses.values());
85 classes.addAll( m_unmatchedDestClasses ); 75 classes.addAll(m_unmatchedDestClasses);
86 return classes; 76 return classes;
87 } 77 }
88 78
89 public BiMap<ClassIdentity,ClassIdentity> getUniqueMatches( ) 79 public BiMap<ClassIdentity,ClassIdentity> getUniqueMatches() {
90 {
91 BiMap<ClassIdentity,ClassIdentity> uniqueMatches = HashBiMap.create(); 80 BiMap<ClassIdentity,ClassIdentity> uniqueMatches = HashBiMap.create();
92 for( ClassIdentity sourceClass : m_sourceClasses.keySet() ) 81 for (ClassIdentity sourceClass : m_sourceClasses.keySet()) {
93 { 82 Collection<ClassIdentity> matchedSourceClasses = m_sourceClasses.get(sourceClass);
94 Collection<ClassIdentity> matchedSourceClasses = m_sourceClasses.get( sourceClass ); 83 Collection<ClassIdentity> matchedDestClasses = m_matchedDestClasses.get(sourceClass);
95 Collection<ClassIdentity> matchedDestClasses = m_matchedDestClasses.get( sourceClass ); 84 if (matchedSourceClasses.size() == 1 && matchedDestClasses.size() == 1) {
96 if( matchedSourceClasses.size() == 1 && matchedDestClasses.size() == 1 )
97 {
98 ClassIdentity matchedSourceClass = matchedSourceClasses.iterator().next(); 85 ClassIdentity matchedSourceClass = matchedSourceClasses.iterator().next();
99 ClassIdentity matchedDestClass = matchedDestClasses.iterator().next(); 86 ClassIdentity matchedDestClass = matchedDestClasses.iterator().next();
100 uniqueMatches.put( matchedSourceClass, matchedDestClass ); 87 uniqueMatches.put(matchedSourceClass, matchedDestClass);
101 } 88 }
102 } 89 }
103 return uniqueMatches; 90 return uniqueMatches;
104 } 91 }
105 92
106 public BiMap<List<ClassIdentity>,List<ClassIdentity>> getAmbiguousMatches( ) 93 public BiMap<List<ClassIdentity>,List<ClassIdentity>> getAmbiguousMatches() {
107 {
108 BiMap<List<ClassIdentity>,List<ClassIdentity>> ambiguousMatches = HashBiMap.create(); 94 BiMap<List<ClassIdentity>,List<ClassIdentity>> ambiguousMatches = HashBiMap.create();
109 for( ClassIdentity sourceClass : m_sourceClasses.keySet() ) 95 for (ClassIdentity sourceClass : m_sourceClasses.keySet()) {
110 { 96 Collection<ClassIdentity> matchedSourceClasses = m_sourceClasses.get(sourceClass);
111 Collection<ClassIdentity> matchedSourceClasses = m_sourceClasses.get( sourceClass ); 97 Collection<ClassIdentity> matchedDestClasses = m_matchedDestClasses.get(sourceClass);
112 Collection<ClassIdentity> matchedDestClasses = m_matchedDestClasses.get( sourceClass ); 98 if (matchedSourceClasses.size() > 1 && matchedDestClasses.size() > 1) {
113 if( matchedSourceClasses.size() > 1 && matchedDestClasses.size() > 1 )
114 {
115 ambiguousMatches.put( 99 ambiguousMatches.put(
116 new ArrayList<ClassIdentity>( matchedSourceClasses ), 100 new ArrayList<ClassIdentity>(matchedSourceClasses),
117 new ArrayList<ClassIdentity>( matchedDestClasses ) 101 new ArrayList<ClassIdentity>(matchedDestClasses)
118 ); 102 );
119 } 103 }
120 } 104 }
121 return ambiguousMatches; 105 return ambiguousMatches;
122 } 106 }
123 107
124 public int getNumAmbiguousSourceMatches( ) 108 public int getNumAmbiguousSourceMatches() {
125 {
126 int num = 0; 109 int num = 0;
127 for( Map.Entry<List<ClassIdentity>,List<ClassIdentity>> entry : getAmbiguousMatches().entrySet() ) 110 for (Map.Entry<List<ClassIdentity>,List<ClassIdentity>> entry : getAmbiguousMatches().entrySet()) {
128 {
129 num += entry.getKey().size(); 111 num += entry.getKey().size();
130 } 112 }
131 return num; 113 return num;
132 } 114 }
133 115
134 public int getNumAmbiguousDestMatches( ) 116 public int getNumAmbiguousDestMatches() {
135 {
136 int num = 0; 117 int num = 0;
137 for( Map.Entry<List<ClassIdentity>,List<ClassIdentity>> entry : getAmbiguousMatches().entrySet() ) 118 for (Map.Entry<List<ClassIdentity>,List<ClassIdentity>> entry : getAmbiguousMatches().entrySet()) {
138 {
139 num += entry.getValue().size(); 119 num += entry.getValue().size();
140 } 120 }
141 return num; 121 return num;
142 } 122 }
143 123
144 public List<ClassIdentity> getUnmatchedSourceClasses( ) 124 public List<ClassIdentity> getUnmatchedSourceClasses() {
145 {
146 List<ClassIdentity> classes = Lists.newArrayList(); 125 List<ClassIdentity> classes = Lists.newArrayList();
147 for( ClassIdentity sourceClass : getSourceClasses() ) 126 for (ClassIdentity sourceClass : getSourceClasses()) {
148 { 127 if (m_matchedDestClasses.get(sourceClass).isEmpty()) {
149 if( m_matchedDestClasses.get( sourceClass ).isEmpty() ) 128 classes.add(sourceClass);
150 {
151 classes.add( sourceClass );
152 } 129 }
153 } 130 }
154 return classes; 131 return classes;
155 } 132 }
156 133
157 public List<ClassIdentity> getUnmatchedDestClasses( ) 134 public List<ClassIdentity> getUnmatchedDestClasses() {
158 { 135 return new ArrayList<ClassIdentity>(m_unmatchedDestClasses);
159 return new ArrayList<ClassIdentity>( m_unmatchedDestClasses );
160 } 136 }
161 137
162 public Map<String,Map.Entry<ClassIdentity,List<ClassIdentity>>> getIndex( ) 138 public Map<String,Map.Entry<ClassIdentity,List<ClassIdentity>>> getIndex() {
163 {
164 Map<String,Map.Entry<ClassIdentity,List<ClassIdentity>>> conversion = Maps.newHashMap(); 139 Map<String,Map.Entry<ClassIdentity,List<ClassIdentity>>> conversion = Maps.newHashMap();
165 for( Map.Entry<ClassIdentity,ClassIdentity> entry : getUniqueMatches().entrySet() ) 140 for (Map.Entry<ClassIdentity,ClassIdentity> entry : getUniqueMatches().entrySet()) {
166 {
167 conversion.put( 141 conversion.put(
168 entry.getKey().getClassEntry().getName(), 142 entry.getKey().getClassEntry().getName(),
169 new AbstractMap.SimpleEntry<ClassIdentity,List<ClassIdentity>>( entry.getKey(), Arrays.asList( entry.getValue() ) ) 143 new AbstractMap.SimpleEntry<ClassIdentity,List<ClassIdentity>>(entry.getKey(), Arrays.asList(entry.getValue()))
170 ); 144 );
171 } 145 }
172 for( Map.Entry<List<ClassIdentity>,List<ClassIdentity>> entry : getAmbiguousMatches().entrySet() ) 146 for (Map.Entry<List<ClassIdentity>,List<ClassIdentity>> entry : getAmbiguousMatches().entrySet()) {
173 { 147 for (ClassIdentity sourceClass : entry.getKey()) {
174 for( ClassIdentity sourceClass : entry.getKey() )
175 {
176 conversion.put( 148 conversion.put(
177 sourceClass.getClassEntry().getName(), 149 sourceClass.getClassEntry().getName(),
178 new AbstractMap.SimpleEntry<ClassIdentity,List<ClassIdentity>>( sourceClass, entry.getValue() ) 150 new AbstractMap.SimpleEntry<ClassIdentity,List<ClassIdentity>>(sourceClass, entry.getValue())
179 ); 151 );
180 } 152 }
181 } 153 }
182 for( ClassIdentity sourceClass : getUnmatchedSourceClasses() ) 154 for (ClassIdentity sourceClass : getUnmatchedSourceClasses()) {
183 {
184 conversion.put( 155 conversion.put(
185 sourceClass.getClassEntry().getName(), 156 sourceClass.getClassEntry().getName(),
186 new AbstractMap.SimpleEntry<ClassIdentity,List<ClassIdentity>>( sourceClass, getUnmatchedDestClasses() ) 157 new AbstractMap.SimpleEntry<ClassIdentity,List<ClassIdentity>>(sourceClass, getUnmatchedDestClasses())
187 ); 158 );
188 } 159 }
189 return conversion; 160 return conversion;
190 } 161 }
191 162
192 @Override 163 @Override
193 public String toString( ) 164 public String toString() {
194 {
195 StringBuilder buf = new StringBuilder(); 165 StringBuilder buf = new StringBuilder();
196 buf.append( String.format( "%12s%8s%8s\n", "", "Source", "Dest" ) ); 166 buf.append(String.format("%12s%8s%8s\n", "", "Source", "Dest"));
197 buf.append( String.format( "%12s%8d%8d\n", "Classes", getSourceClasses().size(), getDestClasses().size() ) ); 167 buf.append(String.format("%12s%8d%8d\n", "Classes", getSourceClasses().size(), getDestClasses().size()));
198 buf.append( String.format( "%12s%8d%8d\n", "Unique", getUniqueMatches().size(), getUniqueMatches().size() ) ); 168 buf.append(String.format("%12s%8d%8d\n", "Unique", getUniqueMatches().size(), getUniqueMatches().size()));
199 buf.append( String.format( "%12s%8d%8d\n", "Ambiguous", getNumAmbiguousSourceMatches(), getNumAmbiguousDestMatches() ) ); 169 buf.append(String.format("%12s%8d%8d\n", "Ambiguous", getNumAmbiguousSourceMatches(), getNumAmbiguousDestMatches()));
200 buf.append( String.format( "%12s%8d%8d\n", "Unmatched", getUnmatchedSourceClasses().size(), getUnmatchedDestClasses().size() ) ); 170 buf.append(String.format("%12s%8d%8d\n", "Unmatched", getUnmatchedSourceClasses().size(), getUnmatchedDestClasses().size()));
201 return buf.toString(); 171 return buf.toString();
202 } 172 }
203} 173}