summaryrefslogtreecommitdiff
path: root/src/main/java/cuchaz/enigma/convert/ClassMatches.java
blob: db2c550fe350a2c14a4c92441daf91ac980787e2 (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
/*******************************************************************************
 * Copyright (c) 2015 Jeff Martin.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser General Public
 * License v3.0 which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl.html
 * <p>
 * Contributors:
 * Jeff Martin - initial API and implementation
 ******************************************************************************/

package cuchaz.enigma.convert;

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;

import java.util.*;

public class ClassMatches implements Iterable<ClassMatch> {

	private Collection<ClassMatch> matches;
	private Map<ClassEntry, ClassMatch> matchesBySource;
	private Map<ClassEntry, ClassMatch> matchesByDest;
	private BiMap<ClassEntry, ClassEntry> uniqueMatches;
	private Map<ClassEntry, ClassMatch> ambiguousMatchesBySource;
	private Map<ClassEntry, ClassMatch> ambiguousMatchesByDest;
	private Set<ClassEntry> unmatchedSourceClasses;
	private Set<ClassEntry> unmatchedDestClasses;

	public ClassMatches() {
		this(new ArrayList<>());
	}

	public ClassMatches(Collection<ClassMatch> matches) {
		this.matches = matches;
		matchesBySource = Maps.newHashMap();
		matchesByDest = Maps.newHashMap();
		uniqueMatches = HashBiMap.create();
		ambiguousMatchesBySource = Maps.newHashMap();
		ambiguousMatchesByDest = Maps.newHashMap();
		unmatchedSourceClasses = Sets.newHashSet();
		unmatchedDestClasses = Sets.newHashSet();

		for (ClassMatch match : matches) {
			indexMatch(match);
		}
	}

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

	public void remove(ClassMatch match) {
		for (ClassEntry sourceClass : match.sourceClasses) {
			matchesBySource.remove(sourceClass);
			uniqueMatches.remove(sourceClass);
			ambiguousMatchesBySource.remove(sourceClass);
			unmatchedSourceClasses.remove(sourceClass);
		}
		for (ClassEntry destClass : match.destClasses) {
			matchesByDest.remove(destClass);
			uniqueMatches.inverse().remove(destClass);
			ambiguousMatchesByDest.remove(destClass);
			unmatchedDestClasses.remove(destClass);
		}
		matches.remove(match);
	}

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

	@Override
	public Iterator<ClassMatch> iterator() {
		return matches.iterator();
	}

	private void indexMatch(ClassMatch match) {
		if (!match.isMatched()) {
			// unmatched
			unmatchedSourceClasses.addAll(match.sourceClasses);
			unmatchedDestClasses.addAll(match.destClasses);
		} else {
			if (match.isAmbiguous()) {
				// ambiguously matched
				for (ClassEntry entry : match.sourceClasses) {
					ambiguousMatchesBySource.put(entry, match);
				}
				for (ClassEntry entry : match.destClasses) {
					ambiguousMatchesByDest.put(entry, match);
				}
			} else {
				// uniquely matched
				uniqueMatches.put(match.getUniqueSource(), match.getUniqueDest());
			}
		}
		for (ClassEntry entry : match.sourceClasses) {
			matchesBySource.put(entry, match);
		}
		for (ClassEntry entry : match.destClasses) {
			matchesByDest.put(entry, match);
		}
	}

	public BiMap<ClassEntry, ClassEntry> getUniqueMatches() {
		return uniqueMatches;
	}

	public Set<ClassEntry> getUnmatchedSourceClasses() {
		return unmatchedSourceClasses;
	}

	public Set<ClassEntry> getUnmatchedDestClasses() {
		return unmatchedDestClasses;
	}

	public Set<ClassEntry> getAmbiguouslyMatchedSourceClasses() {
		return ambiguousMatchesBySource.keySet();
	}

	public ClassMatch getAmbiguousMatchBySource(ClassEntry sourceClass) {
		return ambiguousMatchesBySource.get(sourceClass);
	}

	public ClassMatch getMatchBySource(ClassEntry sourceClass) {
		return matchesBySource.get(sourceClass);
	}

	public ClassMatch getMatchByDest(ClassEntry destClass) {
		return matchesByDest.get(destClass);
	}

	public void removeSource(ClassEntry sourceClass) {
		ClassMatch match = matchesBySource.get(sourceClass);
		if (match != null) {
			remove(match);
			match.sourceClasses.remove(sourceClass);
			if (!match.sourceClasses.isEmpty() || !match.destClasses.isEmpty()) {
				add(match);
			}
		}
	}

	public void removeDest(ClassEntry destClass) {
		ClassMatch match = matchesByDest.get(destClass);
		if (match != null) {
			remove(match);
			match.destClasses.remove(destClass);
			if (!match.sourceClasses.isEmpty() || !match.destClasses.isEmpty()) {
				add(match);
			}
		}
	}
}