package com.intellij.vcs.log.data; import com.intellij.openapi.util.Condition; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.util.NotNullFunction; import com.intellij.util.SmartList; import com.intellij.util.containers.ContainerUtil; import com.intellij.util.containers.MultiMap; import com.intellij.vcs.log.Hash; import com.intellij.vcs.log.VcsLogRefs; import com.intellij.vcs.log.VcsRef; import gnu.trove.TIntObjectHashMap; import org.jetbrains.annotations.NotNull; import java.util.*; public class RefsModel implements VcsLogRefs { @NotNull private final Map> myRefs; @NotNull private final NotNullFunction myIndexGetter; @NotNull private final Collection myBranches; @NotNull private final MultiMap myRefsToHashes; @NotNull private final TIntObjectHashMap> myRefsToIndices; public RefsModel(@NotNull Map> refsByRoot, @NotNull NotNullFunction indexGetter) { myRefs = refsByRoot; myIndexGetter = indexGetter; List allRefs = ContainerUtil.concat(refsByRoot.values()); myBranches = ContainerUtil.filter(allRefs, new Condition() { @Override public boolean value(VcsRef ref) { return ref.getType().isBranch(); } }); myRefsToHashes = prepareRefsMap(allRefs); myRefsToIndices = prepareRefsToIndicesMap(allRefs); } @NotNull private TIntObjectHashMap> prepareRefsToIndicesMap(@NotNull Collection refs) { TIntObjectHashMap> map = new TIntObjectHashMap>(); for (VcsRef ref : refs) { int index = myIndexGetter.fun(ref.getCommitHash()); SmartList list = map.get(index); if (list == null) map.put(index, list = new SmartList()); list.add(ref); } return map; } @NotNull private static MultiMap prepareRefsMap(@NotNull Collection refs) { MultiMap map = MultiMap.createSmartList(); for (VcsRef ref : refs) { map.putValue(ref.getCommitHash(), ref); } return map; } @NotNull public Collection refsToCommit(@NotNull Hash hash) { if (myRefsToHashes.containsKey(hash)) { return myRefsToHashes.get(hash); } return Collections.emptyList(); } @NotNull public Collection refsToCommit(int index) { return myRefsToIndices.containsKey(index) ? myRefsToIndices.get(index) : Collections.emptyList(); } @Override @NotNull public Collection getBranches() { return myBranches; } @NotNull public Collection getAllRefs() { return new ArrayList(myRefsToHashes.values()); } @NotNull public Map> getAllRefsByRoot() { return myRefs; } }