summaryrefslogtreecommitdiff
path: root/src/cuchaz/enigma/convert/ClassMatching.java
blob: d8973ac52422a3ebd9b0bdc176ad2753874c788b (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
/*******************************************************************************
 * 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.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;

import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;

import cuchaz.enigma.mapping.ClassEntry;

public class ClassMatching {
	
	private ClassForest m_sourceClasses;
	private ClassForest m_destClasses;
	private BiMap<ClassEntry,ClassEntry> m_knownMatches;
	
	public ClassMatching(ClassIdentifier sourceIdentifier, ClassIdentifier destIdentifier) {
		m_sourceClasses = new ClassForest(sourceIdentifier);
		m_destClasses = new ClassForest(destIdentifier);
		m_knownMatches = HashBiMap.create();
	}
	
	public void addKnownMatches(BiMap<ClassEntry,ClassEntry> knownMatches) {
		m_knownMatches.putAll(knownMatches);
	}
	
	public void match(Iterable<ClassEntry> sourceClasses, Iterable<ClassEntry> destClasses) {
		for (ClassEntry sourceClass : sourceClasses) {
			if (!m_knownMatches.containsKey(sourceClass)) {
				m_sourceClasses.add(sourceClass);
			}
		}
		for (ClassEntry destClass : destClasses) {
			if (!m_knownMatches.containsValue(destClass)) {
				m_destClasses.add(destClass);
			}
		}
	}
	
	public Collection<ClassMatch> matches() {
		List<ClassMatch> matches = Lists.newArrayList();
		for (Entry<ClassEntry,ClassEntry> entry : m_knownMatches.entrySet()) {
			matches.add(new ClassMatch(
				entry.getKey(),
				entry.getValue()
			));
		}
		for (ClassIdentity identity : m_sourceClasses.identities()) {
			matches.add(new ClassMatch(
				m_sourceClasses.getClasses(identity),
				m_destClasses.getClasses(identity)
			));
		}
		for (ClassIdentity identity : m_destClasses.identities()) {
			if (!m_sourceClasses.containsIdentity(identity)) {
				matches.add(new ClassMatch(
					new ArrayList<ClassEntry>(),
					m_destClasses.getClasses(identity)
				));
			}
		}
		return matches;
	}
	
	public Collection<ClassEntry> sourceClasses() {
		Set<ClassEntry> classes = Sets.newHashSet();
		for (ClassMatch match : matches()) {
			classes.addAll(match.sourceClasses);
		}
		return classes;
	}
	
	public Collection<ClassEntry> destClasses() {
		Set<ClassEntry> classes = Sets.newHashSet();
		for (ClassMatch match : matches()) {
			classes.addAll(match.destClasses);
		}
		return classes;
	}
	
	public BiMap<ClassEntry,ClassEntry> uniqueMatches() {
		BiMap<ClassEntry,ClassEntry> uniqueMatches = HashBiMap.create();
		for (ClassMatch match : matches()) {
			if (match.isMatched() && !match.isAmbiguous()) {
				uniqueMatches.put(match.getUniqueSource(), match.getUniqueDest());
			}
		}
		return uniqueMatches;
	}
	
	public Collection<ClassMatch> ambiguousMatches() {
		List<ClassMatch> ambiguousMatches = Lists.newArrayList();
		for (ClassMatch match : matches()) {
			if (match.isMatched() && match.isAmbiguous()) {
				ambiguousMatches.add(match);
			}
		}
		return ambiguousMatches;
	}
	
	public Collection<ClassEntry> unmatchedSourceClasses() {
		List<ClassEntry> classes = Lists.newArrayList();
		for (ClassMatch match : matches()) {
			if (!match.isMatched() && !match.sourceClasses.isEmpty()) {
				classes.addAll(match.sourceClasses);
			}
		}
		return classes;
	}
	
	public Collection<ClassEntry> unmatchedDestClasses() {
		List<ClassEntry> classes = Lists.newArrayList();
		for (ClassMatch match : matches()) {
			if (!match.isMatched() && !match.destClasses.isEmpty()) {
				classes.addAll(match.destClasses);
			}
		}
		return classes;
	}
	
	@Override
	public String toString() {
		
		// count the ambiguous classes
		int numAmbiguousSource = 0;
		int numAmbiguousDest = 0;
		for (ClassMatch match : ambiguousMatches()) {
			numAmbiguousSource += match.sourceClasses.size();
			numAmbiguousDest += match.destClasses.size();
		}
		
		StringBuilder buf = new StringBuilder();
		buf.append(String.format("%20s%8s%8s\n", "", "Source", "Dest"));
		buf.append(String.format("%20s%8d%8d\n", "Classes", sourceClasses().size(), destClasses().size()));
		buf.append(String.format("%20s%8d%8d\n", "Uniquely matched", uniqueMatches().size(), uniqueMatches().size()));
		buf.append(String.format("%20s%8d%8d\n", "Ambiguously matched", numAmbiguousSource, numAmbiguousDest));
		buf.append(String.format("%20s%8d%8d\n", "Unmatched", unmatchedSourceClasses().size(), unmatchedDestClasses().size()));
		return buf.toString();
	}
}