Line data Source code
1 : // Copyright (C) 2017 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.permissions;
16 :
17 : import static com.google.gerrit.server.permissions.LabelPermission.ForUser.ON_BEHALF_OF;
18 : import static com.google.gerrit.server.permissions.LabelPermission.ForUser.SELF;
19 : import static java.util.Objects.requireNonNull;
20 :
21 : import com.google.gerrit.entities.LabelType;
22 : import com.google.gerrit.entities.LabelValue;
23 : import com.google.gerrit.server.util.LabelVote;
24 :
25 : /** Permission representing a label. */
26 : public class LabelPermission implements ChangePermissionOrLabel {
27 103 : public enum ForUser {
28 103 : SELF,
29 103 : ON_BEHALF_OF;
30 : }
31 :
32 : private final ForUser forUser;
33 : private final String name;
34 :
35 : /**
36 : * Construct a reference to a label permission.
37 : *
38 : * @param type type description of the label.
39 : */
40 : public LabelPermission(LabelType type) {
41 39 : this(SELF, type);
42 39 : }
43 :
44 : /**
45 : * Construct a reference to a label permission.
46 : *
47 : * @param forUser {@code SELF} (default) or {@code ON_BEHALF_OF} for labelAs behavior.
48 : * @param type type description of the label.
49 : */
50 : public LabelPermission(ForUser forUser, LabelType type) {
51 39 : this(forUser, type.getName());
52 39 : }
53 :
54 : /**
55 : * Construct a reference to a label permission.
56 : *
57 : * @param name name of the label, e.g. {@code "Code-Review"} or {@code "Verified"}.
58 : */
59 : public LabelPermission(String name) {
60 0 : this(SELF, name);
61 0 : }
62 :
63 : /**
64 : * Construct a reference to a label permission.
65 : *
66 : * @param forUser {@code SELF} (default) or {@code ON_BEHALF_OF} for labelAs behavior.
67 : * @param name name of the label, e.g. {@code "Code-Review"} or {@code "Verified"}.
68 : */
69 39 : public LabelPermission(ForUser forUser, String name) {
70 39 : this.forUser = requireNonNull(forUser, "ForUser");
71 39 : this.name = LabelType.checkName(name);
72 39 : }
73 :
74 : /** Returns {@code SELF} or {@code ON_BEHALF_OF} (or labelAs). */
75 : public ForUser forUser() {
76 39 : return forUser;
77 : }
78 :
79 : /** Returns name of the label, e.g. {@code "Code-Review"}. */
80 : public String label() {
81 39 : return name;
82 : }
83 :
84 : @Override
85 : public String describeForException() {
86 0 : if (forUser == ON_BEHALF_OF) {
87 0 : return "label on behalf of " + name;
88 : }
89 0 : return "label " + name;
90 : }
91 :
92 : @Override
93 : public int hashCode() {
94 39 : return name.hashCode();
95 : }
96 :
97 : @Override
98 : public boolean equals(Object other) {
99 0 : if (other instanceof LabelPermission) {
100 0 : LabelPermission b = (LabelPermission) other;
101 0 : return forUser == b.forUser && name.equals(b.name);
102 : }
103 0 : return false;
104 : }
105 :
106 : @Override
107 : public String toString() {
108 0 : if (forUser == ON_BEHALF_OF) {
109 0 : return "LabelAs[" + name + ']';
110 : }
111 0 : return "Label[" + name + ']';
112 : }
113 :
114 : /** A {@link LabelPermission} at a specific value. */
115 : public static class WithValue implements ChangePermissionOrLabel {
116 : private final ForUser forUser;
117 : private final LabelVote label;
118 :
119 : /**
120 : * Construct a reference to a label at a specific value.
121 : *
122 : * @param type description of the label.
123 : * @param value numeric score assigned to the label.
124 : */
125 : public WithValue(LabelType type, LabelValue value) {
126 103 : this(SELF, type, value);
127 103 : }
128 :
129 : /**
130 : * Construct a reference to a label at a specific value.
131 : *
132 : * @param type description of the label.
133 : * @param value numeric score assigned to the label.
134 : */
135 : public WithValue(LabelType type, short value) {
136 58 : this(SELF, type.getName(), value);
137 58 : }
138 :
139 : /**
140 : * Construct a reference to a label at a specific value.
141 : *
142 : * @param forUser {@code SELF} (default) or {@code ON_BEHALF_OF} for labelAs behavior.
143 : * @param type description of the label.
144 : * @param value numeric score assigned to the label.
145 : */
146 : public WithValue(ForUser forUser, LabelType type, LabelValue value) {
147 103 : this(forUser, type.getName(), value.getValue());
148 103 : }
149 :
150 : /**
151 : * Construct a reference to a label at a specific value.
152 : *
153 : * @param forUser {@code SELF} (default) or {@code ON_BEHALF_OF} for labelAs behavior.
154 : * @param type description of the label.
155 : * @param value numeric score assigned to the label.
156 : */
157 : public WithValue(ForUser forUser, LabelType type, short value) {
158 2 : this(forUser, type.getName(), value);
159 2 : }
160 :
161 : /**
162 : * Construct a reference to a label at a specific value.
163 : *
164 : * @param name name of the label, e.g. {@code "Code-Review"} or {@code "Verified"}.
165 : * @param value numeric score assigned to the label.
166 : */
167 : public WithValue(String name, short value) {
168 5 : this(SELF, name, value);
169 5 : }
170 :
171 : /**
172 : * Construct a reference to a label at a specific value.
173 : *
174 : * @param forUser {@code SELF} (default) or {@code ON_BEHALF_OF} for labelAs behavior.
175 : * @param name name of the label, e.g. {@code "Code-Review"} or {@code "Verified"}.
176 : * @param value numeric score assigned to the label.
177 : */
178 : public WithValue(ForUser forUser, String name, short value) {
179 103 : this(forUser, LabelVote.create(name, value));
180 103 : }
181 :
182 : /**
183 : * Construct a reference to a label at a specific value.
184 : *
185 : * @param label label name and vote.
186 : */
187 : public WithValue(LabelVote label) {
188 0 : this(SELF, label);
189 0 : }
190 :
191 : /**
192 : * Construct a reference to a label at a specific value.
193 : *
194 : * @param forUser {@code SELF} (default) or {@code ON_BEHALF_OF} for labelAs behavior.
195 : * @param label label name and vote.
196 : */
197 103 : public WithValue(ForUser forUser, LabelVote label) {
198 103 : this.forUser = requireNonNull(forUser, "ForUser");
199 103 : this.label = requireNonNull(label, "LabelVote");
200 103 : }
201 :
202 : /** Returns {@code SELF} or {@code ON_BEHALF_OF} (or labelAs). */
203 : public ForUser forUser() {
204 103 : return forUser;
205 : }
206 :
207 : /** Returns name of the label, e.g. {@code "Code-Review"}. */
208 : public String label() {
209 103 : return label.label();
210 : }
211 :
212 : /** Returns specific value of the label, e.g. 1 or 2. */
213 : public short value() {
214 103 : return label.value();
215 : }
216 :
217 : @Override
218 : public String describeForException() {
219 4 : if (forUser == ON_BEHALF_OF) {
220 1 : return "label on behalf of " + label.formatWithEquals();
221 : }
222 3 : return "label " + label.formatWithEquals();
223 : }
224 :
225 : @Override
226 : public int hashCode() {
227 103 : return label.hashCode();
228 : }
229 :
230 : @Override
231 : public boolean equals(Object other) {
232 103 : if (other instanceof WithValue) {
233 103 : WithValue b = (WithValue) other;
234 103 : return forUser == b.forUser && label.equals(b.label);
235 : }
236 0 : return false;
237 : }
238 :
239 : @Override
240 : public String toString() {
241 0 : if (forUser == ON_BEHALF_OF) {
242 0 : return "LabelAs[" + label.format() + ']';
243 : }
244 0 : return "Label[" + label.format() + ']';
245 : }
246 : }
247 : }
|