summaryrefslogtreecommitdiff
path: root/src/cuchaz/enigma/convert/ClassMatching.java
blob: 53b6f7f4a80389902544072c92dd4fd36d11c9d1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*******************************************************************************
 * Copyright (c) 2014 Jeff Martin.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl.html
 * 
 * Contributors:
 *     Jeff Martin - initial API and implementation
 ******************************************************************************/
package cuchaz.enigma.convert;

import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;

public class ClassMatching {
	
	private Multimap<ClassIdentity,ClassIdentity> m_sourceClasses;
	private Multimap<ClassIdentity,ClassIdentity> m_matchedDestClasses;
	private List<ClassIdentity> m_unmatchedDestClasses;
	
	public ClassMatching() {
		m_sourceClasses = ArrayListMultimap.create();
		m_matchedDestClasses = ArrayListMultimap.create();
		m_unmatchedDestClasses = Lists.newArrayList();
	}
	
	public void addSource(ClassIdentity c) {
		m_sourceClasses.put(c, c);
	}
	
	public void matchDestClass(ClassIdentity destClass) {
		Collection<ClassIdentity> matchedSourceClasses = m_sourceClasses.get(destClass);
		if (matchedSourceClasses.isEmpty()) {
			// no match
			m_unmatchedDestClasses.add(destClass);
		} else {
			// found a match
			m_matchedDestClasses.put(destClass, destClass);
			
			// DEBUG
			ClassIdentity sourceClass = matchedSourceClasses.iterator().next();
			assert (sourceClass.hashCode() == destClass.hashCode());
			assert (sourceClass.equals(destClass));
		}
	}
	
	public void removeSource(ClassIdentity sourceClass) {
		m_sourceClasses.remove(sourceClass, sourceClass);
	}
	
	public void removeDest(ClassIdentity destClass) {
		m_matchedDestClasses.remove(destClass, destClass);
		m_unmatchedDestClasses.remove(destClass);
	}
	
	public List<ClassIdentity> getSourceClasses() {
		return new ArrayList<ClassIdentity>(m_sourceClasses.values());
	}
	
	public List<ClassIdentity> getDestClasses() {
		List<ClassIdentity> classes = Lists.newArrayList();
		classes.addAll(m_matchedDestClasses.values());
		classes.addAll(m_unmatchedDestClasses);
		return classes;
	}
	
	public BiMap<ClassIdentity,ClassIdentity> getUniqueMatches() {
		BiMap<ClassIdentity,ClassIdentity> uniqueMatches = HashBiMap.create();
		for (ClassIdentity sourceClass : m_sourceClasses.keySet()) {
			Collection<ClassIdentity> matchedSourceClasses = m_sourceClasses.get(sourceClass);
			Collection<ClassIdentity> matchedDestClasses = m_matchedDestClasses.get(sourceClass);
			if (matchedSourceClasses.size() == 1 && matchedDestClasses.size() == 1) {
				ClassIdentity matchedSourceClass = matchedSourceClasses.iterator().next();
				ClassIdentity matchedDestClass = matchedDestClasses.iterator().next();
				uniqueMatches.put(matchedSourceClass, matchedDestClass);
			}
		}
		return uniqueMatches;
	}
	
	public BiMap<List<ClassIdentity>,List<ClassIdentity>> getAmbiguousMatches() {
		BiMap<List<ClassIdentity>,List<ClassIdentity>> ambiguousMatches = HashBiMap.create();
		for (ClassIdentity sourceClass : m_sourceClasses.keySet()) {
			Collection<ClassIdentity> matchedSourceClasses = m_sourceClasses.get(sourceClass);
			Collection<ClassIdentity> matchedDestClasses = m_matchedDestClasses.get(sourceClass);
			if (matchedSourceClasses.size() > 1 && matchedDestClasses.size() > 1) {
				ambiguousMatches.put(
					new ArrayList<ClassIdentity>(matchedSourceClasses),
					new ArrayList<ClassIdentity>(matchedDestClasses)
				);
			}
		}
		return ambiguousMatches;
	}
	
	public int getNumAmbiguousSourceMatches() {
		int num = 0;
		for (Map.Entry<List<ClassIdentity>,List<ClassIdentity>> entry : getAmbiguousMatches().entrySet()) {
			num += entry.getKey().size();
		}
		return num;
	}
	
	public int getNumAmbiguousDestMatches() {
		int num = 0;
		for (Map.Entry<List<ClassIdentity>,List<ClassIdentity>> entry : getAmbiguousMatches().entrySet()) {
			num += entry.getValue().size();
		}
		return num;
	}
	
	public List<ClassIdentity> getUnmatchedSourceClasses() {
		List<ClassIdentity> classes = Lists.newArrayList();
		for (ClassIdentity sourceClass : getSourceClasses()) {
			if (m_matchedDestClasses.get(sourceClass).isEmpty()) {
				classes.add(sourceClass);
			}
		}
		return classes;
	}
	
	public List<ClassIdentity> getUnmatchedDestClasses() {
		return new ArrayList<ClassIdentity>(m_unmatchedDestClasses);
	}
	
	public Map<String,Map.Entry<ClassIdentity,List<ClassIdentity>>> getIndex() {
		Map<String,Map.Entry<ClassIdentity,List<ClassIdentity>>> conversion = Maps.newHashMap();
		for (Map.Entry<ClassIdentity,ClassIdentity> entry : getUniqueMatches().entrySet()) {
			conversion.put(
				entry.getKey().getClassEntry().getName(),
				new AbstractMap.SimpleEntry<ClassIdentity,List<ClassIdentity>>(entry.getKey(), Arrays.asList(entry.getValue()))
			);
		}
		for (Map.Entry<List<ClassIdentity>,List<ClassIdentity>> entry : getAmbiguousMatches().entrySet()) {
			for (ClassIdentity sourceClass : entry.getKey()) {
				conversion.put(
					sourceClass.getClassEntry().getName(),
					new AbstractMap.SimpleEntry<ClassIdentity,List<ClassIdentity>>(sourceClass, entry.getValue())
				);
			}
		}
		for (ClassIdentity sourceClass : getUnmatchedSourceClasses()) {
			conversion.put(
				sourceClass.getClassEntry().getName(),
				new AbstractMap.SimpleEntry<ClassIdentity,List<ClassIdentity>>(sourceClass, getUnmatchedDestClasses())
			);
		}
		return conversion;
	}
	
	@Override
	public String toString() {
		StringBuilder buf = new StringBuilder();
		buf.append(String.format("%12s%8s%8s\n", "", "Source", "Dest"));
		buf.append(String.format("%12s%8d%8d\n", "Classes", getSourceClasses().size(), getDestClasses().size()));
		buf.append(String.format("%12s%8d%8d\n", "Unique", getUniqueMatches().size(), getUniqueMatches().size()));
		buf.append(String.format("%12s%8d%8d\n", "Ambiguous", getNumAmbiguousSourceMatches(), getNumAmbiguousDestMatches()));
		buf.append(String.format("%12s%8d%8d\n", "Unmatched", getUnmatchedSourceClasses().size(), getUnmatchedDestClasses().size()));
		return buf.toString();
	}
}