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.server.query.change; 16 : 17 : import static com.google.common.base.Preconditions.checkArgument; 18 : 19 : import com.google.auto.value.AutoValue; 20 : import com.google.common.base.Strings; 21 : import com.google.gerrit.entities.LabelType; 22 : import java.util.Locale; 23 : 24 : /** An entity representing a special label vote that's not numeric, e.g. MAX, MIN, etc... */ 25 : @AutoValue 26 7 : public abstract class MagicLabelVote { 27 : public static MagicLabelVote parseWithEquals(String text) { 28 14 : checkArgument(!Strings.isNullOrEmpty(text), "Empty label vote"); 29 14 : int e = text.lastIndexOf('='); 30 14 : checkArgument(e >= 0, "Label vote missing '=': %s", text); 31 9 : String label = text.substring(0, e); 32 9 : String voteValue = text.substring(e + 1); 33 7 : return create(label, MagicLabelValue.valueOf(voteValue)); 34 : } 35 : 36 : public static MagicLabelVote create(String label, MagicLabelValue value) { 37 7 : return new AutoValue_MagicLabelVote(LabelType.checkNameInternal(label), value); 38 : } 39 : 40 : public abstract String label(); 41 : 42 : public abstract MagicLabelValue value(); 43 : 44 : public String formatLabel() { 45 0 : return label().toLowerCase(Locale.US) + "=" + value().name(); 46 : } 47 : }