From aaf09df8cb0be3adb30c388c8383fe8afa542590 Mon Sep 17 00:00:00 2001 From: Aleksey Pivovarov Date: Tue, 31 Jan 2017 17:09:21 +0300 Subject: [PATCH] vcs: simplify - merge two maps together --- .../vcs/changes/ChangeListsIndexes.java | 69 +++++++++++-------- 1 file changed, 40 insertions(+), 29 deletions(-) diff --git a/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/ChangeListsIndexes.java b/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/ChangeListsIndexes.java index e9da48c4fe83..3fbcacb42280 100644 --- a/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/ChangeListsIndexes.java +++ b/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/ChangeListsIndexes.java @@ -18,7 +18,6 @@ package com.intellij.openapi.vcs.changes; import com.google.common.collect.Sets; import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.util.Comparing; -import com.intellij.openapi.util.Pair; import com.intellij.openapi.vcs.FilePath; import com.intellij.openapi.vcs.FileStatus; import com.intellij.openapi.vcs.VcsKey; @@ -35,38 +34,34 @@ import static com.intellij.util.containers.ContainerUtil.newHashSet; public class ChangeListsIndexes { private static final Logger LOG = Logger.getInstance("#com.intellij.openapi.vcs.changes.ChangeListsIndexes"); - private final TreeMap myFileToStatus; - private final Map> myFileToVcs; + private final TreeMap myMap; ChangeListsIndexes() { - myFileToStatus = new TreeMap<>(HierarchicalFilePathComparator.SYSTEM_CASE_SENSITIVE); - myFileToVcs = new HashMap<>(); + myMap = new TreeMap<>(HierarchicalFilePathComparator.SYSTEM_CASE_SENSITIVE); } ChangeListsIndexes(final ChangeListsIndexes idx) { - myFileToStatus = new TreeMap<>(idx.myFileToStatus); - myFileToVcs = new HashMap<>(idx.myFileToVcs); + myMap = new TreeMap<>(idx.myMap); } void add(final FilePath file, final FileStatus status, final VcsKey key, VcsRevisionNumber number) { - myFileToStatus.put(file, status); - myFileToVcs.put(file, Pair.create(key, number)); + myMap.put(file, new Data(status, key, number)); if (LOG.isDebugEnabled()) { LOG.debug("Set status " + status + " for " + file); } } void remove(final FilePath file) { - myFileToStatus.remove(file); - myFileToVcs.remove(file); + myMap.remove(file); } public FileStatus getStatus(final VirtualFile file) { - return myFileToStatus.get(VcsUtil.getFilePath(file)); + return getStatus(VcsUtil.getFilePath(file)); } public FileStatus getStatus(@NotNull FilePath file) { - return myFileToStatus.get(file); + Data data = myMap.get(file); + return data != null ? data.status : null; } public void changeAdded(final Change change, final VcsKey key) { @@ -95,8 +90,8 @@ public class ChangeListsIndexes { @Nullable private VcsKey getVcsForRevision(@Nullable ContentRevision revision) { if (revision != null) { - Pair pair = myFileToVcs.get(revision.getFile()); - return pair == null ? null : pair.getFirst(); + Data data = myMap.get(revision.getFile()); + return data != null ? data.vcsKey : null; } return null; } @@ -130,9 +125,9 @@ public class ChangeListsIndexes { Set toAdd, Set> toModify) { // this is old - final Set oldKeySet = newHashSet(myFileToVcs.keySet()); + final Set oldKeySet = newHashSet(myMap.keySet()); final Set toRemoveSet = newHashSet(oldKeySet); - final Set newKeySet = newIndexes.myFileToVcs.keySet(); + final Set newKeySet = newIndexes.myMap.keySet(); final Set toAddSet = newHashSet(newKeySet); toRemoveSet.removeAll(newKeySet); toAddSet.removeAll(oldKeySet); @@ -140,31 +135,31 @@ public class ChangeListsIndexes { oldKeySet.removeAll(toRemoveSet); for (FilePath s : toRemoveSet) { - final Pair pair = myFileToVcs.get(s); - toRemove.add(fromPairAndPath(s, pair)); + final Data data = myMap.get(s); + toRemove.add(fromPairAndPath(s, data)); } for (FilePath s : toAddSet) { - final Pair pair = newIndexes.myFileToVcs.get(s); - toAdd.add(fromPairAndPath(s, pair)); + final Data data = newIndexes.myMap.get(s); + toAdd.add(fromPairAndPath(s, data)); } for (FilePath s : oldKeySet) { - final Pair old = myFileToVcs.get(s); - final Pair newOne = newIndexes.myFileToVcs.get(s); + final Data old = myMap.get(s); + final Data newOne = newIndexes.myMap.get(s); assert old != null && newOne != null; - if (!old.equals(newOne)) { + if (!old.sameRevisions(newOne)) { toModify.add(new BeforeAfter<>(fromPairAndPath(s, old), fromPairAndPath(s, newOne))); } } } - private static BaseRevision fromPairAndPath(FilePath s, Pair pair) { - return new BaseRevision(pair.getFirst(), pair.getSecond(), s); + private static BaseRevision fromPairAndPath(FilePath s, Data data) { + return new BaseRevision(data.vcsKey, data.revision, s); } public List getAffectedFilesUnderVcs() { final List result = new ArrayList<>(); - for (Map.Entry> entry : myFileToVcs.entrySet()) { - final Pair value = entry.getValue(); + for (Map.Entry entry : myMap.entrySet()) { + final Data value = entry.getValue(); result.add(fromPairAndPath(entry.getKey(), value)); } return result; @@ -172,6 +167,22 @@ public class ChangeListsIndexes { @NotNull public NavigableSet getAffectedPaths() { - return Sets.unmodifiableNavigableSet(myFileToStatus.navigableKeySet()); + return Sets.unmodifiableNavigableSet(myMap.navigableKeySet()); + } + + private static class Data { + public final FileStatus status; + public final VcsKey vcsKey; + public final VcsRevisionNumber revision; + + public Data(FileStatus status, VcsKey vcsKey, VcsRevisionNumber revision) { + this.status = status; + this.vcsKey = vcsKey; + this.revision = revision; + } + + public boolean sameRevisions(@NotNull Data data) { + return Comparing.equal(vcsKey, data.vcsKey) && Comparing.equal(revision, data.revision); + } } }