summaryrefslogtreecommitdiff
path: root/src/cuchaz/enigma/convert/Matches.java
blob: 75ecc2a87735dc01c0cf3c8f34c8b21528b3445b (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
package cuchaz.enigma.convert;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

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

import cuchaz.enigma.mapping.ClassEntry;


public class Matches implements Iterable<ClassMatch> {

	Collection<ClassMatch> m_matches;
	BiMap<ClassEntry,ClassEntry> m_uniqueMatches;
	Map<ClassEntry,ClassMatch> m_ambiguousMatchesBySource;
	Map<ClassEntry,ClassMatch> m_ambiguousMatchesByDest;
	Set<ClassEntry> m_unmatchedSourceClasses;
	Set<ClassEntry> m_unmatchedDestClasses;
	
	public Matches() {
		this(new ArrayList<ClassMatch>());
	}
	
	public Matches(Collection<ClassMatch> matches) {
		m_matches = matches;
		m_uniqueMatches = HashBiMap.create();
		m_ambiguousMatchesBySource = Maps.newHashMap();
		m_ambiguousMatchesByDest = Maps.newHashMap();
		m_unmatchedSourceClasses = Sets.newHashSet();
		m_unmatchedDestClasses = Sets.newHashSet();
		
		for (ClassMatch match : matches) {
			indexMatch(match);
		}
	}

	public void add(ClassMatch match) {
		m_matches.add(match);
		indexMatch(match);
	}

	public int size() {
		return m_matches.size();
	}

	@Override
	public Iterator<ClassMatch> iterator() {
		return m_matches.iterator();
	}
	
	private void indexMatch(ClassMatch match) {
		if (!match.isMatched()) {
			// unmatched
			m_unmatchedSourceClasses.addAll(match.sourceClasses);
			m_unmatchedDestClasses.addAll(match.destClasses);
		} else {
			if (match.isAmbiguous()) {
				// ambiguously matched
				for (ClassEntry entry : match.sourceClasses) {
					m_ambiguousMatchesBySource.put(entry, match);
				}
				for (ClassEntry entry : match.destClasses) {
					m_ambiguousMatchesByDest.put(entry, match);
				}
			} else {
				// uniquely matched
				m_uniqueMatches.put(match.getUniqueSource(), match.getUniqueDest());
			}
		}
	}
	
	public BiMap<ClassEntry,ClassEntry> getUniqueMatches() {
		return m_uniqueMatches;
	}
	
	public Set<ClassEntry> getUnmatchedSourceClasses() {
		return m_unmatchedSourceClasses;
	}
	
	public Set<ClassEntry> getUnmatchedDestClasses() {
		return m_unmatchedDestClasses;
	}
}