1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-05 16:15:25 +02:00

Bug 45203. Export header substitution maps in canonical order.

This commit is contained in:
Sergey Prigogin 2013-08-19 13:35:24 -07:00
parent b3b9eb576e
commit d1e900bd10
2 changed files with 20 additions and 12 deletions

View file

@ -13,9 +13,9 @@ package org.eclipse.cdt.internal.ui.refactoring.includes;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
@ -34,11 +34,12 @@ public class IncludeMap {
private static final String TAG_VALUE = "value"; //$NON-NLS-1$
private final boolean unconditionalSubstitution; // Not serialized when saving to a memento.
private final Map<IncludeInfo, List<IncludeInfo>> map;
// The order is not crucial but can make a difference when calculating transitive closure.
private final LinkedHashMap<IncludeInfo, List<IncludeInfo>> map;
public IncludeMap(boolean unconditionalSubstitution) {
this.unconditionalSubstitution = unconditionalSubstitution;
this.map = new HashMap<IncludeInfo, List<IncludeInfo>>();
this.map = new LinkedHashMap<IncludeInfo, List<IncludeInfo>>();
}
/**
@ -50,7 +51,7 @@ public class IncludeMap {
if (keysAndValues.length % 2 != 0)
throw new IllegalArgumentException("More keys than values"); //$NON-NLS-1$
this.unconditionalSubstitution = unconditionalSubstitution;
this.map = new HashMap<IncludeInfo, List<IncludeInfo>>(keysAndValues.length / 2);
this.map = new LinkedHashMap<IncludeInfo, List<IncludeInfo>>(keysAndValues.length / 2);
for (int i = 0; i < keysAndValues.length;) {
String key = keysAndValues[i++];
addMapping(key, keysAndValues[i++]);
@ -59,7 +60,7 @@ public class IncludeMap {
public IncludeMap(IncludeMap other) {
this.unconditionalSubstitution = other.unconditionalSubstitution;
this.map = new HashMap<IncludeInfo, List<IncludeInfo>>(other.map.size());
this.map = new LinkedHashMap<IncludeInfo, List<IncludeInfo>>(other.map.size());
addAllMappings(other);
}
@ -157,11 +158,12 @@ public class IncludeMap {
* Writes the map to a memento. The {@link #isUnconditionalSubstitution()} flag is not written.
*/
public void saveToMemento(IMemento memento) {
for (Entry<IncludeInfo, List<IncludeInfo>> entry : map.entrySet()) {
String key = entry.getKey().toString();
for (IncludeInfo value : entry.getValue()) {
List<IncludeInfo> keys = new ArrayList<IncludeInfo>(map.keySet());
Collections.sort(keys);
for (IncludeInfo key : keys) {
for (IncludeInfo value : map.get(key)) {
IMemento mapping = memento.createChild(TAG_MAPPING);
mapping.putString(TAG_KEY, key);
mapping.putString(TAG_KEY, key.toString());
mapping.putString(TAG_VALUE, value.toString());
}
}

View file

@ -26,6 +26,8 @@ import org.eclipse.ui.IMemento;
import org.eclipse.ui.WorkbenchException;
import org.eclipse.ui.XMLMemento;
import com.ibm.icu.text.Collator;
import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.cdt.internal.corext.codemanipulation.IncludeInfo;
@ -39,6 +41,7 @@ public class SymbolExportMap {
private static final String TAG_MAPPING = "mapping"; //$NON-NLS-1$
private static final String TAG_KEY = "key"; //$NON-NLS-1$
private static final String TAG_VALUE = "value"; //$NON-NLS-1$
private static final Collator COLLATOR = Collator.getInstance();
private final Map<String, Set<IncludeInfo>> map;
@ -124,9 +127,12 @@ public class SymbolExportMap {
* Writes the map to a memento.
*/
public void saveToMemento(IMemento memento) {
for (Entry<String, Set<IncludeInfo>> entry : map.entrySet()) {
String key = entry.getKey().toString();
for (IncludeInfo value : entry.getValue()) {
List<String> keys = new ArrayList<String>(map.keySet());
Collections.sort(keys, COLLATOR);
for (String key : keys) {
List<IncludeInfo> values = new ArrayList<IncludeInfo>(map.get(key));
Collections.sort(values);
for (IncludeInfo value : values) {
IMemento mapping = memento.createChild(TAG_MAPPING);
mapping.putString(TAG_KEY, key);
mapping.putString(TAG_VALUE, value.toString());