summaryrefslogtreecommitdiff
path: root/src/cuchaz/enigma/convert/FieldMatches.java
blob: 2973356be2fc53c8d94a63b14b815ec9a2cb565b (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
package cuchaz.enigma.convert;

import java.util.Collection;
import java.util.Set;

import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
import com.google.common.collect.Sets;

import cuchaz.enigma.mapping.ClassEntry;
import cuchaz.enigma.mapping.FieldEntry;


public class FieldMatches {

	private BiMap<FieldEntry,FieldEntry> m_matches;
	private Multimap<ClassEntry,FieldEntry> m_matchedSourceFields;
	private Multimap<ClassEntry,FieldEntry> m_unmatchedSourceFields;
	private Multimap<ClassEntry,FieldEntry> m_unmatchedDestFields;
	private Multimap<ClassEntry,FieldEntry> m_unmatchableSourceFields;
	
	public FieldMatches() {
		m_matches = HashBiMap.create();
		m_matchedSourceFields = HashMultimap.create();
		m_unmatchedSourceFields = HashMultimap.create();
		m_unmatchedDestFields = HashMultimap.create();
		m_unmatchableSourceFields = HashMultimap.create();
	}
	
	public void addMatch(FieldEntry srcField, FieldEntry destField) {
		boolean wasAdded = m_matches.put(srcField, destField) == null;
		assert (wasAdded);
		wasAdded = m_matchedSourceFields.put(srcField.getClassEntry(), srcField);
		assert (wasAdded);
	}
	
	public void addUnmatchedSourceField(FieldEntry fieldEntry) {
		boolean wasAdded = m_unmatchedSourceFields.put(fieldEntry.getClassEntry(), fieldEntry);
		assert (wasAdded);
	}
	
	public void addUnmatchedSourceFields(Iterable<FieldEntry> fieldEntries) {
		for (FieldEntry fieldEntry : fieldEntries) {
			addUnmatchedSourceField(fieldEntry);
		}
	}
	
	public void addUnmatchedDestField(FieldEntry fieldEntry) {
		boolean wasAdded = m_unmatchedDestFields.put(fieldEntry.getClassEntry(), fieldEntry);
		assert (wasAdded);
	}
	
	public void addUnmatchedDestFields(Iterable<FieldEntry> fieldEntries) {
		for (FieldEntry fieldEntry : fieldEntries) {
			addUnmatchedDestField(fieldEntry);
		}
	}
	
	public void addUnmatchableSourceField(FieldEntry sourceField) {
		boolean wasAdded = m_unmatchableSourceFields.put(sourceField.getClassEntry(), sourceField);
		assert (wasAdded);
	}
	
	public Set<ClassEntry> getSourceClassesWithUnmatchedFields() {
		return m_unmatchedSourceFields.keySet();
	}
	
	public Collection<ClassEntry> getSourceClassesWithoutUnmatchedFields() {
		Set<ClassEntry> out = Sets.newHashSet();
		out.addAll(m_matchedSourceFields.keySet());
		out.removeAll(m_unmatchedSourceFields.keySet());
		return out;
	}

	public Collection<FieldEntry> getUnmatchedSourceFields() {
		return m_unmatchedSourceFields.values();
	}

	public Collection<FieldEntry> getUnmatchedSourceFields(ClassEntry sourceClass) {
		return m_unmatchedSourceFields.get(sourceClass);
	}

	public Collection<FieldEntry> getUnmatchedDestFields() {
		return m_unmatchedDestFields.values();
	}

	public Collection<FieldEntry> getUnmatchedDestFields(ClassEntry destClass) {
		return m_unmatchedDestFields.get(destClass);
	}
	
	public Collection<FieldEntry> getUnmatchableSourceFields() {
		return m_unmatchableSourceFields.values();
	}

	public boolean hasSource(FieldEntry fieldEntry) {
		return m_matches.containsKey(fieldEntry) || m_unmatchedSourceFields.containsValue(fieldEntry);
	}
	
	public boolean hasDest(FieldEntry fieldEntry) {
		return m_matches.containsValue(fieldEntry) || m_unmatchedDestFields.containsValue(fieldEntry);
	}

	public BiMap<FieldEntry,FieldEntry> matches() {
		return m_matches;
	}
	
	public boolean isMatchedSourceField(FieldEntry sourceField) {
		return m_matches.containsKey(sourceField);
	}

	public boolean isMatchedDestField(FieldEntry destField) {
		return m_matches.containsValue(destField);
	}

	public void makeMatch(FieldEntry sourceField, FieldEntry destField) {
		boolean wasRemoved = m_unmatchedSourceFields.remove(sourceField.getClassEntry(), sourceField);
		assert (wasRemoved);
		wasRemoved = m_unmatchedDestFields.remove(destField.getClassEntry(), destField);
		assert (wasRemoved);
		addMatch(sourceField, destField);
	}

	public boolean isMatched(FieldEntry sourceField, FieldEntry destField) {
		FieldEntry match = m_matches.get(sourceField);
		return match != null && match.equals(destField);
	}

	public void unmakeMatch(FieldEntry sourceField, FieldEntry destField) {
		boolean wasRemoved = m_matches.remove(sourceField) != null;
		assert (wasRemoved);
		wasRemoved = m_matchedSourceFields.remove(sourceField.getClassEntry(), sourceField);
		assert (wasRemoved);
		addUnmatchedSourceField(sourceField);
		addUnmatchedDestField(destField);
	}
	
	public void makeSourceUnmatchable(FieldEntry sourceField) {
		assert(!isMatchedSourceField(sourceField));
		boolean wasRemoved = m_unmatchedSourceFields.remove(sourceField.getClassEntry(), sourceField);
		assert (wasRemoved);
		addUnmatchableSourceField(sourceField);
	}
}