Line data Source code
1 : // Copyright (C) 2015 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.server.submit; 16 : 17 : import com.google.common.collect.ImmutableCollection; 18 : import com.google.common.collect.ImmutableList; 19 : import com.google.common.collect.ImmutableMap; 20 : import com.google.common.collect.ImmutableSet; 21 : import com.google.common.collect.ListMultimap; 22 : import com.google.common.collect.MultimapBuilder; 23 : import com.google.gerrit.entities.BranchNameKey; 24 : import com.google.gerrit.entities.Change; 25 : import com.google.gerrit.entities.Project; 26 : import com.google.gerrit.server.query.change.ChangeData; 27 : import java.util.Collection; 28 : import java.util.LinkedHashMap; 29 : import java.util.Map; 30 : 31 : /** 32 : * A set of changes grouped together to be submitted atomically. 33 : * 34 : * <p>MergeSuperSet constructs ChangeSets to accumulate intermediate results toward the ChangeSet it 35 : * returns when done. 36 : * 37 : * <p>This class is not thread safe. 38 : */ 39 : public class ChangeSet { 40 : private final ImmutableMap<Change.Id, ChangeData> changeData; 41 : 42 : /** 43 : * Additional changes not included in changeData because their connection to the original change 44 : * is not visible to the current user. That is, this map includes both - changes that are not 45 : * visible to the current user, and - changes whose only relationship to the set is via a change 46 : * that is not visible to the current user 47 : */ 48 : private final ImmutableMap<Change.Id, ChangeData> nonVisibleChanges; 49 : 50 : private static ImmutableMap<Change.Id, ChangeData> index( 51 : Iterable<ChangeData> changes, Collection<Change.Id> exclude) { 52 53 : Map<Change.Id, ChangeData> ret = new LinkedHashMap<>(); 53 53 : for (ChangeData cd : changes) { 54 53 : Change.Id id = cd.getId(); 55 53 : if (!ret.containsKey(id) && !exclude.contains(id)) { 56 53 : ret.put(id, cd); 57 : } 58 53 : } 59 53 : return ImmutableMap.copyOf(ret); 60 : } 61 : 62 53 : public ChangeSet(Iterable<ChangeData> changes, Iterable<ChangeData> hiddenChanges) { 63 53 : changeData = index(changes, ImmutableList.of()); 64 53 : nonVisibleChanges = index(hiddenChanges, changeData.keySet()); 65 53 : } 66 : 67 : public ChangeSet(ChangeData change, boolean visible) { 68 53 : this(visible ? ImmutableList.of(change) : ImmutableList.of(), ImmutableList.of(change)); 69 53 : } 70 : 71 : public ImmutableSet<Change.Id> ids() { 72 53 : return changeData.keySet(); 73 : } 74 : 75 : public ImmutableMap<Change.Id, ChangeData> changesById() { 76 53 : return changeData; 77 : } 78 : 79 : public ListMultimap<BranchNameKey, ChangeData> changesByBranch() { 80 : ListMultimap<BranchNameKey, ChangeData> ret = 81 53 : MultimapBuilder.hashKeys().arrayListValues().build(); 82 53 : for (ChangeData cd : changeData.values()) { 83 53 : ret.put(cd.change().getDest(), cd); 84 53 : } 85 53 : return ret; 86 : } 87 : 88 : public ImmutableCollection<ChangeData> changes() { 89 53 : return changeData.values(); 90 : } 91 : 92 : public ImmutableSet<Project.NameKey> projects() { 93 53 : ImmutableSet.Builder<Project.NameKey> ret = ImmutableSet.builder(); 94 53 : for (ChangeData cd : changeData.values()) { 95 53 : ret.add(cd.project()); 96 53 : } 97 53 : return ret.build(); 98 : } 99 : 100 : public ImmutableSet<Change.Id> nonVisibleIds() { 101 5 : return nonVisibleChanges.keySet(); 102 : } 103 : 104 : public ImmutableList<ChangeData> nonVisibleChanges() { 105 53 : return nonVisibleChanges.values().asList(); 106 : } 107 : 108 : public boolean furtherHiddenChanges() { 109 53 : return !nonVisibleChanges.isEmpty(); 110 : } 111 : 112 : public int size() { 113 19 : return changeData.size() + nonVisibleChanges.size(); 114 : } 115 : 116 : @Override 117 : public String toString() { 118 5 : return getClass().getSimpleName() + ids() + nonVisibleIds(); 119 : } 120 : }