Line data Source code
1 : // Copyright (C) 2021 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.base.Strings; 19 : import com.google.gerrit.common.Nullable; 20 : import com.google.gson.Gson; 21 : import com.google.gson.TypeAdapter; 22 : import java.util.Optional; 23 : 24 : /** Describe a applicability, blocking or override expression of a {@link SubmitRequirement}. */ 25 : @AutoValue 26 104 : public abstract class SubmitRequirementExpression { 27 : 28 : public static SubmitRequirementExpression create(String expression) { 29 104 : return new AutoValue_SubmitRequirementExpression(expression); 30 : } 31 : 32 : /** 33 : * Creates a new {@link SubmitRequirementExpression}. 34 : * 35 : * @param expression String representation of the expression 36 : * @return empty {@link Optional} if the input expression is null or empty, or an Optional 37 : * containing the expression otherwise. 38 : */ 39 : public static Optional<SubmitRequirementExpression> of(@Nullable String expression) { 40 7 : return Optional.ofNullable(Strings.emptyToNull(expression)) 41 7 : .map(SubmitRequirementExpression::create); 42 : } 43 : 44 : /** Returns the underlying String representing this {@link SubmitRequirementExpression}. */ 45 : public abstract String expressionString(); 46 : 47 : public static TypeAdapter<SubmitRequirementExpression> typeAdapter(Gson gson) { 48 3 : return new AutoValue_SubmitRequirementExpression.GsonTypeAdapter(gson); 49 : } 50 : 51 : /** 52 : * Returns a "submit requirement" expression that requires the maximum vote on the Code-Review 53 : * label. 54 : */ 55 : public static SubmitRequirementExpression maxCodeReview() { 56 3 : return SubmitRequirementExpression.create(String.format("label:Code-Review=MAX")); 57 : } 58 : }