Line data Source code
1 : // Copyright (C) 2016 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; 16 : 17 : import static com.google.common.base.Preconditions.checkArgument; 18 : import static com.google.gerrit.server.notedb.ReviewerStateInternal.CC; 19 : import static com.google.gerrit.server.notedb.ReviewerStateInternal.REVIEWER; 20 : 21 : import com.google.common.collect.HashBasedTable; 22 : import com.google.common.collect.ImmutableSet; 23 : import com.google.common.collect.ImmutableTable; 24 : import com.google.common.collect.Table; 25 : import com.google.gerrit.entities.Account; 26 : import com.google.gerrit.entities.PatchSetApproval; 27 : import com.google.gerrit.server.notedb.ReviewerStateInternal; 28 : import java.time.Instant; 29 : 30 : /** 31 : * Set of reviewers on a change. 32 : * 33 : * <p>A given account may appear in multiple states and at different timestamps. No reviewers with 34 : * state {@link ReviewerStateInternal#REMOVED} are ever exposed by this interface. 35 : */ 36 : public class ReviewerSet { 37 103 : private static final ReviewerSet EMPTY = new ReviewerSet(ImmutableTable.of()); 38 : 39 : public static ReviewerSet fromApprovals(Iterable<PatchSetApproval> approvals) { 40 0 : PatchSetApproval first = null; 41 0 : Table<ReviewerStateInternal, Account.Id, Instant> reviewers = HashBasedTable.create(); 42 0 : for (PatchSetApproval psa : approvals) { 43 0 : if (first == null) { 44 0 : first = psa; 45 : } else { 46 0 : checkArgument( 47 0 : first.key().patchSetId().changeId().equals(psa.key().patchSetId().changeId()), 48 : "multiple change IDs: %s, %s", 49 0 : first.key(), 50 0 : psa.key()); 51 : } 52 0 : Account.Id id = psa.accountId(); 53 0 : reviewers.put(REVIEWER, id, psa.granted()); 54 0 : if (psa.value() != 0) { 55 0 : reviewers.remove(CC, id); 56 : } 57 0 : } 58 0 : return new ReviewerSet(reviewers); 59 : } 60 : 61 : public static ReviewerSet fromTable(Table<ReviewerStateInternal, Account.Id, Instant> table) { 62 103 : return new ReviewerSet(table); 63 : } 64 : 65 : public static ReviewerSet empty() { 66 103 : return EMPTY; 67 : } 68 : 69 : private final ImmutableTable<ReviewerStateInternal, Account.Id, Instant> table; 70 : private ImmutableSet<Account.Id> accounts; 71 : 72 103 : private ReviewerSet(Table<ReviewerStateInternal, Account.Id, Instant> table) { 73 103 : this.table = ImmutableTable.copyOf(table); 74 103 : } 75 : 76 : public ImmutableSet<Account.Id> all() { 77 103 : if (accounts == null) { 78 : // Idempotent and immutable, don't bother locking. 79 103 : accounts = ImmutableSet.copyOf(table.columnKeySet()); 80 : } 81 103 : return accounts; 82 : } 83 : 84 : public ImmutableSet<Account.Id> byState(ReviewerStateInternal state) { 85 103 : return table.row(state).keySet(); 86 : } 87 : 88 : public ImmutableTable<ReviewerStateInternal, Account.Id, Instant> asTable() { 89 103 : return table; 90 : } 91 : 92 : @Override 93 : public boolean equals(Object o) { 94 1 : return (o instanceof ReviewerSet) && table.equals(((ReviewerSet) o).table); 95 : } 96 : 97 : @Override 98 : public int hashCode() { 99 0 : return table.hashCode(); 100 : } 101 : 102 : @Override 103 : public String toString() { 104 0 : return getClass().getSimpleName() + table; 105 : } 106 : }