diff options
Diffstat (limited to 'src/main/java/cuchaz/enigma/convert/FieldMatches.java')
| -rw-r--r-- | src/main/java/cuchaz/enigma/convert/FieldMatches.java | 261 |
1 files changed, 130 insertions, 131 deletions
diff --git a/src/main/java/cuchaz/enigma/convert/FieldMatches.java b/src/main/java/cuchaz/enigma/convert/FieldMatches.java index 236cd4d..a528b27 100644 --- a/src/main/java/cuchaz/enigma/convert/FieldMatches.java +++ b/src/main/java/cuchaz/enigma/convert/FieldMatches.java | |||
| @@ -8,144 +8,143 @@ | |||
| 8 | * Contributors: | 8 | * Contributors: |
| 9 | * Jeff Martin - initial API and implementation | 9 | * Jeff Martin - initial API and implementation |
| 10 | ******************************************************************************/ | 10 | ******************************************************************************/ |
| 11 | |||
| 11 | package cuchaz.enigma.convert; | 12 | package cuchaz.enigma.convert; |
| 12 | 13 | ||
| 13 | import com.google.common.collect.*; | 14 | import com.google.common.collect.*; |
| 14 | |||
| 15 | import java.util.Collection; | ||
| 16 | import java.util.Set; | ||
| 17 | |||
| 18 | import cuchaz.enigma.mapping.ClassEntry; | 15 | import cuchaz.enigma.mapping.ClassEntry; |
| 19 | import cuchaz.enigma.mapping.FieldEntry; | 16 | import cuchaz.enigma.mapping.FieldEntry; |
| 20 | 17 | ||
| 18 | import java.util.Collection; | ||
| 19 | import java.util.Set; | ||
| 21 | 20 | ||
| 22 | public class FieldMatches { | 21 | public class FieldMatches { |
| 23 | 22 | ||
| 24 | private BiMap<FieldEntry, FieldEntry> matches; | 23 | private BiMap<FieldEntry, FieldEntry> matches; |
| 25 | private Multimap<ClassEntry, FieldEntry> matchedSourceFields; | 24 | private Multimap<ClassEntry, FieldEntry> matchedSourceFields; |
| 26 | private Multimap<ClassEntry, FieldEntry> unmatchedSourceFields; | 25 | private Multimap<ClassEntry, FieldEntry> unmatchedSourceFields; |
| 27 | private Multimap<ClassEntry, FieldEntry> unmatchedDestFields; | 26 | private Multimap<ClassEntry, FieldEntry> unmatchedDestFields; |
| 28 | private Multimap<ClassEntry, FieldEntry> unmatchableSourceFields; | 27 | private Multimap<ClassEntry, FieldEntry> unmatchableSourceFields; |
| 29 | 28 | ||
| 30 | public FieldMatches() { | 29 | public FieldMatches() { |
| 31 | matches = HashBiMap.create(); | 30 | matches = HashBiMap.create(); |
| 32 | matchedSourceFields = HashMultimap.create(); | 31 | matchedSourceFields = HashMultimap.create(); |
| 33 | unmatchedSourceFields = HashMultimap.create(); | 32 | unmatchedSourceFields = HashMultimap.create(); |
| 34 | unmatchedDestFields = HashMultimap.create(); | 33 | unmatchedDestFields = HashMultimap.create(); |
| 35 | unmatchableSourceFields = HashMultimap.create(); | 34 | unmatchableSourceFields = HashMultimap.create(); |
| 36 | } | 35 | } |
| 37 | 36 | ||
| 38 | public void addMatch(FieldEntry srcField, FieldEntry destField) { | 37 | public void addMatch(FieldEntry srcField, FieldEntry destField) { |
| 39 | boolean wasAdded = matches.put(srcField, destField) == null; | 38 | boolean wasAdded = matches.put(srcField, destField) == null; |
| 40 | assert (wasAdded); | 39 | assert (wasAdded); |
| 41 | wasAdded = matchedSourceFields.put(srcField.getClassEntry(), srcField); | 40 | wasAdded = matchedSourceFields.put(srcField.getClassEntry(), srcField); |
| 42 | assert (wasAdded); | 41 | assert (wasAdded); |
| 43 | } | 42 | } |
| 44 | 43 | ||
| 45 | public void addUnmatchedSourceField(FieldEntry fieldEntry) { | 44 | public void addUnmatchedSourceField(FieldEntry fieldEntry) { |
| 46 | boolean wasAdded = unmatchedSourceFields.put(fieldEntry.getClassEntry(), fieldEntry); | 45 | boolean wasAdded = unmatchedSourceFields.put(fieldEntry.getClassEntry(), fieldEntry); |
| 47 | assert (wasAdded); | 46 | assert (wasAdded); |
| 48 | } | 47 | } |
| 49 | 48 | ||
| 50 | public void addUnmatchedSourceFields(Iterable<FieldEntry> fieldEntries) { | 49 | public void addUnmatchedSourceFields(Iterable<FieldEntry> fieldEntries) { |
| 51 | for (FieldEntry fieldEntry : fieldEntries) { | 50 | for (FieldEntry fieldEntry : fieldEntries) { |
| 52 | addUnmatchedSourceField(fieldEntry); | 51 | addUnmatchedSourceField(fieldEntry); |
| 53 | } | 52 | } |
| 54 | } | 53 | } |
| 55 | 54 | ||
| 56 | public void addUnmatchedDestField(FieldEntry fieldEntry) { | 55 | public void addUnmatchedDestField(FieldEntry fieldEntry) { |
| 57 | boolean wasAdded = unmatchedDestFields.put(fieldEntry.getClassEntry(), fieldEntry); | 56 | boolean wasAdded = unmatchedDestFields.put(fieldEntry.getClassEntry(), fieldEntry); |
| 58 | assert (wasAdded); | 57 | assert (wasAdded); |
| 59 | } | 58 | } |
| 60 | 59 | ||
| 61 | public void addUnmatchedDestFields(Iterable<FieldEntry> fieldEntries) { | 60 | public void addUnmatchedDestFields(Iterable<FieldEntry> fieldEntries) { |
| 62 | for (FieldEntry fieldEntry : fieldEntries) { | 61 | for (FieldEntry fieldEntry : fieldEntries) { |
| 63 | addUnmatchedDestField(fieldEntry); | 62 | addUnmatchedDestField(fieldEntry); |
| 64 | } | 63 | } |
| 65 | } | 64 | } |
| 66 | 65 | ||
| 67 | public void addUnmatchableSourceField(FieldEntry sourceField) { | 66 | public void addUnmatchableSourceField(FieldEntry sourceField) { |
| 68 | boolean wasAdded = unmatchableSourceFields.put(sourceField.getClassEntry(), sourceField); | 67 | boolean wasAdded = unmatchableSourceFields.put(sourceField.getClassEntry(), sourceField); |
| 69 | assert (wasAdded); | 68 | assert (wasAdded); |
| 70 | } | 69 | } |
| 71 | 70 | ||
| 72 | public Set<ClassEntry> getSourceClassesWithUnmatchedFields() { | 71 | public Set<ClassEntry> getSourceClassesWithUnmatchedFields() { |
| 73 | return unmatchedSourceFields.keySet(); | 72 | return unmatchedSourceFields.keySet(); |
| 74 | } | 73 | } |
| 75 | 74 | ||
| 76 | public Collection<ClassEntry> getSourceClassesWithoutUnmatchedFields() { | 75 | public Collection<ClassEntry> getSourceClassesWithoutUnmatchedFields() { |
| 77 | Set<ClassEntry> out = Sets.newHashSet(); | 76 | Set<ClassEntry> out = Sets.newHashSet(); |
| 78 | out.addAll(matchedSourceFields.keySet()); | 77 | out.addAll(matchedSourceFields.keySet()); |
| 79 | out.removeAll(unmatchedSourceFields.keySet()); | 78 | out.removeAll(unmatchedSourceFields.keySet()); |
| 80 | return out; | 79 | return out; |
| 81 | } | 80 | } |
| 82 | 81 | ||
| 83 | public Collection<FieldEntry> getUnmatchedSourceFields() { | 82 | public Collection<FieldEntry> getUnmatchedSourceFields() { |
| 84 | return unmatchedSourceFields.values(); | 83 | return unmatchedSourceFields.values(); |
| 85 | } | 84 | } |
| 86 | 85 | ||
| 87 | public Collection<FieldEntry> getUnmatchedSourceFields(ClassEntry sourceClass) { | 86 | public Collection<FieldEntry> getUnmatchedSourceFields(ClassEntry sourceClass) { |
| 88 | return unmatchedSourceFields.get(sourceClass); | 87 | return unmatchedSourceFields.get(sourceClass); |
| 89 | } | 88 | } |
| 90 | 89 | ||
| 91 | public Collection<FieldEntry> getUnmatchedDestFields() { | 90 | public Collection<FieldEntry> getUnmatchedDestFields() { |
| 92 | return unmatchedDestFields.values(); | 91 | return unmatchedDestFields.values(); |
| 93 | } | 92 | } |
| 94 | 93 | ||
| 95 | public Collection<FieldEntry> getUnmatchedDestFields(ClassEntry destClass) { | 94 | public Collection<FieldEntry> getUnmatchedDestFields(ClassEntry destClass) { |
| 96 | return unmatchedDestFields.get(destClass); | 95 | return unmatchedDestFields.get(destClass); |
| 97 | } | 96 | } |
| 98 | 97 | ||
| 99 | public Collection<FieldEntry> getUnmatchableSourceFields() { | 98 | public Collection<FieldEntry> getUnmatchableSourceFields() { |
| 100 | return unmatchableSourceFields.values(); | 99 | return unmatchableSourceFields.values(); |
| 101 | } | 100 | } |
| 102 | 101 | ||
| 103 | public boolean hasSource(FieldEntry fieldEntry) { | 102 | public boolean hasSource(FieldEntry fieldEntry) { |
| 104 | return matches.containsKey(fieldEntry) || unmatchedSourceFields.containsValue(fieldEntry); | 103 | return matches.containsKey(fieldEntry) || unmatchedSourceFields.containsValue(fieldEntry); |
| 105 | } | 104 | } |
| 106 | 105 | ||
| 107 | public boolean hasDest(FieldEntry fieldEntry) { | 106 | public boolean hasDest(FieldEntry fieldEntry) { |
| 108 | return matches.containsValue(fieldEntry) || unmatchedDestFields.containsValue(fieldEntry); | 107 | return matches.containsValue(fieldEntry) || unmatchedDestFields.containsValue(fieldEntry); |
| 109 | } | 108 | } |
| 110 | 109 | ||
| 111 | public BiMap<FieldEntry, FieldEntry> matches() { | 110 | public BiMap<FieldEntry, FieldEntry> matches() { |
| 112 | return matches; | 111 | return matches; |
| 113 | } | 112 | } |
| 114 | 113 | ||
| 115 | public boolean isMatchedSourceField(FieldEntry sourceField) { | 114 | public boolean isMatchedSourceField(FieldEntry sourceField) { |
| 116 | return matches.containsKey(sourceField); | 115 | return matches.containsKey(sourceField); |
| 117 | } | 116 | } |
| 118 | 117 | ||
| 119 | public boolean isMatchedDestField(FieldEntry destField) { | 118 | public boolean isMatchedDestField(FieldEntry destField) { |
| 120 | return matches.containsValue(destField); | 119 | return matches.containsValue(destField); |
| 121 | } | 120 | } |
| 122 | 121 | ||
| 123 | public void makeMatch(FieldEntry sourceField, FieldEntry destField) { | 122 | public void makeMatch(FieldEntry sourceField, FieldEntry destField) { |
| 124 | boolean wasRemoved = unmatchedSourceFields.remove(sourceField.getClassEntry(), sourceField); | 123 | boolean wasRemoved = unmatchedSourceFields.remove(sourceField.getClassEntry(), sourceField); |
| 125 | assert (wasRemoved); | 124 | assert (wasRemoved); |
| 126 | wasRemoved = unmatchedDestFields.remove(destField.getClassEntry(), destField); | 125 | wasRemoved = unmatchedDestFields.remove(destField.getClassEntry(), destField); |
| 127 | assert (wasRemoved); | 126 | assert (wasRemoved); |
| 128 | addMatch(sourceField, destField); | 127 | addMatch(sourceField, destField); |
| 129 | } | 128 | } |
| 130 | 129 | ||
| 131 | public boolean isMatched(FieldEntry sourceField, FieldEntry destField) { | 130 | public boolean isMatched(FieldEntry sourceField, FieldEntry destField) { |
| 132 | FieldEntry match = matches.get(sourceField); | 131 | FieldEntry match = matches.get(sourceField); |
| 133 | return match != null && match.equals(destField); | 132 | return match != null && match.equals(destField); |
| 134 | } | 133 | } |
| 135 | 134 | ||
| 136 | public void unmakeMatch(FieldEntry sourceField, FieldEntry destField) { | 135 | public void unmakeMatch(FieldEntry sourceField, FieldEntry destField) { |
| 137 | boolean wasRemoved = matches.remove(sourceField) != null; | 136 | boolean wasRemoved = matches.remove(sourceField) != null; |
| 138 | assert (wasRemoved); | 137 | assert (wasRemoved); |
| 139 | wasRemoved = matchedSourceFields.remove(sourceField.getClassEntry(), sourceField); | 138 | wasRemoved = matchedSourceFields.remove(sourceField.getClassEntry(), sourceField); |
| 140 | assert (wasRemoved); | 139 | assert (wasRemoved); |
| 141 | addUnmatchedSourceField(sourceField); | 140 | addUnmatchedSourceField(sourceField); |
| 142 | addUnmatchedDestField(destField); | 141 | addUnmatchedDestField(destField); |
| 143 | } | 142 | } |
| 144 | 143 | ||
| 145 | public void makeSourceUnmatchable(FieldEntry sourceField) { | 144 | public void makeSourceUnmatchable(FieldEntry sourceField) { |
| 146 | assert (!isMatchedSourceField(sourceField)); | 145 | assert (!isMatchedSourceField(sourceField)); |
| 147 | boolean wasRemoved = unmatchedSourceFields.remove(sourceField.getClassEntry(), sourceField); | 146 | boolean wasRemoved = unmatchedSourceFields.remove(sourceField.getClassEntry(), sourceField); |
| 148 | assert (wasRemoved); | 147 | assert (wasRemoved); |
| 149 | addUnmatchableSourceField(sourceField); | 148 | addUnmatchableSourceField(sourceField); |
| 150 | } | 149 | } |
| 151 | } | 150 | } |