Line data Source code
1 : // Copyright (C) 2008 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.entities; 16 : 17 : import com.google.auto.value.AutoValue; 18 : import com.google.common.primitives.Shorts; 19 : import java.time.Instant; 20 : import java.util.Optional; 21 : 22 : /** An approval (or negative approval) on a patch set. */ 23 : @AutoValue 24 77 : public abstract class PatchSetApproval { 25 : public static Key key(PatchSet.Id patchSetId, Account.Id accountId, LabelId labelId) { 26 77 : return new AutoValue_PatchSetApproval_Key(patchSetId, accountId, labelId); 27 : } 28 : 29 : @AutoValue 30 77 : public abstract static class Key { 31 : public abstract PatchSet.Id patchSetId(); 32 : 33 : public abstract Account.Id accountId(); 34 : 35 : public abstract LabelId labelId(); 36 : 37 : public boolean isLegacySubmit() { 38 68 : return LabelId.LEGACY_SUBMIT_NAME.equals(labelId().get()); 39 : } 40 : } 41 : 42 : /** 43 : * Globally unique identifier. 44 : * 45 : * <p>The identifier is unique to each granted approval, i.e. approvals, re-added within same 46 : * {@link Change} or even {@link PatchSet} have different {@link UUID}. 47 : */ 48 : @AutoValue 49 69 : public abstract static class UUID implements Comparable<UUID> { 50 : 51 : abstract String uuid(); 52 : 53 : public String get() { 54 69 : return uuid(); 55 : } 56 : 57 : @Override 58 : public final int compareTo(UUID o) { 59 0 : return uuid().compareTo(o.uuid()); 60 : } 61 : 62 : @Override 63 : public final String toString() { 64 68 : return get(); 65 : } 66 : } 67 : 68 : public static UUID uuid(String n) { 69 69 : return new AutoValue_PatchSetApproval_UUID(n); 70 : } 71 : 72 : public static Builder builder() { 73 77 : return new AutoValue_PatchSetApproval.Builder().postSubmit(false).copied(false); 74 : } 75 : 76 : @AutoValue.Builder 77 77 : public abstract static class Builder { 78 : public abstract Builder key(Key key); 79 : 80 : public abstract Key key(); 81 : 82 : /** 83 : * {@link UUID} of {@link PatchSetApproval}. 84 : * 85 : * <p>Optional, since it might be missing for approvals, granted (persisted in NoteDB), before 86 : * {@link UUID} was introduced and does not apply to removals ( represented as approval with 87 : * {@link #value}, set to '0'). 88 : */ 89 : public abstract Builder uuid(Optional<UUID> uuid); 90 : 91 : public abstract Builder uuid(UUID uuid); 92 : 93 : public abstract Builder value(short value); 94 : 95 : public Builder value(int value) { 96 77 : return value(Shorts.checkedCast(value)); 97 : } 98 : 99 : public abstract Builder granted(Instant granted); 100 : 101 : public abstract Builder tag(String tag); 102 : 103 : public abstract Builder tag(Optional<String> tag); 104 : 105 : public abstract Builder realAccountId(Account.Id realAccountId); 106 : 107 : abstract Optional<Account.Id> realAccountId(); 108 : 109 : public abstract Builder postSubmit(boolean isPostSubmit); 110 : 111 : public abstract Builder copied(boolean isCopied); 112 : 113 : abstract PatchSetApproval autoBuild(); 114 : 115 : public PatchSetApproval build() { 116 77 : if (!realAccountId().isPresent()) { 117 77 : realAccountId(key().accountId()); 118 : } 119 77 : return autoBuild(); 120 : } 121 : } 122 : 123 : public abstract Key key(); 124 : 125 : public abstract Optional<UUID> uuid(); 126 : 127 : /** 128 : * Value assigned by the user. 129 : * 130 : * <p>The precise meaning of "value" is up to each category. 131 : * 132 : * <p>In general: 133 : * 134 : * <ul> 135 : * <li><b>< 0:</b> The approval is rejected/revoked. 136 : * <li><b>= 0:</b> No indication either way is provided. 137 : * <li><b>> 0:</b> The approval is approved/positive. 138 : * </ul> 139 : * 140 : * and in the negative and positive direction a magnitude can be assumed.The further from 0 the 141 : * more assertive the approval. 142 : */ 143 : public abstract short value(); 144 : 145 : public abstract Instant granted(); 146 : 147 : public abstract Optional<String> tag(); 148 : 149 : /** Real user that made this approval on behalf of the user recorded in {@link Key#accountId}. */ 150 : public abstract Account.Id realAccountId(); 151 : 152 : public abstract boolean postSubmit(); 153 : 154 : public abstract boolean copied(); 155 : 156 : public abstract Builder toBuilder(); 157 : 158 : /** 159 : * Makes a copy of {@link PatchSetApproval} that applies to {@code psId}. 160 : * 161 : * <p>The returned {@link PatchSetApproval} has the same {@link UUID} as the original {@link 162 : * PatchSetApproval}, which is generated when it is originally granted. 163 : * 164 : * <p>This is needed since we want to keep the link between the original {@link PatchSetApproval} 165 : * and the {@link #copied} one. 166 : * 167 : * @param psId {@link PatchSet.Id} of {@link PatchSet} that the copy should be applied to. 168 : * @return {@link #copied} {@link PatchSetApproval} that applies to {@code psId}. 169 : */ 170 : public PatchSetApproval copyWithPatchSet(PatchSet.Id psId) { 171 19 : return toBuilder() 172 19 : .key(key(psId, key().accountId(), key().labelId())) 173 19 : .uuid(uuid()) 174 19 : .copied(true) 175 19 : .build(); 176 : } 177 : 178 : public PatchSet.Id patchSetId() { 179 52 : return key().patchSetId(); 180 : } 181 : 182 : public Account.Id accountId() { 183 77 : return key().accountId(); 184 : } 185 : 186 : public LabelId labelId() { 187 75 : return key().labelId(); 188 : } 189 : 190 : public String label() { 191 68 : return labelId().get(); 192 : } 193 : 194 : public boolean isLegacySubmit() { 195 68 : return key().isLegacySubmit(); 196 : } 197 : }