summaryrefslogtreecommitdiff
path: root/src/cuchaz/enigma/convert/Matches.java
blob: 5faa923ccba850e7287622db4128de5b22a0a397 (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
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;
	Map<ClassEntry,ClassMatch> m_matchesBySource;
	Map<ClassEntry,ClassMatch> m_matchesByDest;
	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_matchesBySource = Maps.newHashMap();
		m_matchesByDest = Maps.newHashMap();
		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());
			}
		}
		for (ClassEntry entry : match.sourceClasses) {
			m_matchesBySource.put(entry, match);
		}
		for (ClassEntry entry : match.destClasses) {
			m_matchesByDest.put(entry, match);
		}
	}
	
	public BiMap<ClassEntry,ClassEntry> getUniqueMatches() {
		return m_uniqueMatches;
	}
	
	public Set<ClassEntry> getUnmatchedSourceClasses() {
		return m_unmatchedSourceClasses;
	}
	
	public Set<ClassEntry> getUnmatchedDestClasses() {
		return m_unmatchedDestClasses;
	}

	public Set<ClassEntry> getAmbiguouslyMatchedSourceClasses() {
		return m_ambiguousMatchesBySource.keySet();
	}
	
	public ClassMatch getAmbiguousMatchBySource(ClassEntry sourceClass) {
		return m_ambiguousMatchesBySource.get(sourceClass);
	}

	public ClassMatch getMatchBySource(ClassEntry sourceClass) {
		return m_matchesBySource.get(sourceClass);
	}
	
	public ClassMatch getMatchByDest(ClassEntry destClass) {
		return m_matchesByDest.get(destClass);
	}
}