LCOV - code coverage report
Current view: top level - server/util - LabelVote.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 32 33 97.0 %
Date: 2022-11-19 15:00:39 Functions: 7 8 87.5 %

          Line data    Source code
       1             : // Copyright (C) 2013 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.util;
      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             : 
      23             : /** A single vote on a label, consisting of a label name and a value. */
      24             : @AutoValue
      25         103 : public abstract class LabelVote {
      26             :   public static LabelVote parse(String text) {
      27          14 :     checkArgument(!Strings.isNullOrEmpty(text), "Empty label vote");
      28          14 :     if (text.charAt(0) == '-') {
      29           2 :       return create(text.substring(1), (short) 0);
      30             :     }
      31          14 :     short sign = 0;
      32             :     int i;
      33          14 :     for (i = text.length() - 1; i >= 0; i--) {
      34          14 :       int c = text.charAt(i);
      35          14 :       if (c == '-') {
      36          11 :         sign = (short) -1;
      37          11 :         break;
      38          14 :       } else if (c == '+') {
      39          13 :         sign = (short) 1;
      40          13 :         break;
      41          14 :       } else if (!('0' <= c && c <= '9')) {
      42          11 :         break;
      43             :       }
      44             :     }
      45          12 :     if (sign == 0) {
      46          10 :       return create(text, (short) 1);
      47             :     }
      48          10 :     return create(text.substring(0, i), (short) (sign * Short.parseShort(text.substring(i + 1))));
      49             :   }
      50             : 
      51             :   public static LabelVote parseWithEquals(String text) {
      52          69 :     checkArgument(!Strings.isNullOrEmpty(text), "Empty label vote");
      53          69 :     int e = text.lastIndexOf('=');
      54          69 :     checkArgument(e >= 0, "Label vote missing '=': %s", text);
      55          69 :     return create(text.substring(0, e), Short.parseShort(text.substring(e + 1)));
      56             :   }
      57             : 
      58             :   public static StringBuilder appendTo(StringBuilder sb, String label, short value) {
      59          59 :     if (value == (short) 0) {
      60           9 :       return sb.append('-').append(label);
      61          59 :     } else if (value < 0) {
      62          21 :       return sb.append(label).append(value);
      63             :     }
      64          59 :     return sb.append(label).append('+').append(value);
      65             :   }
      66             : 
      67             :   public static LabelVote create(String label, short value) {
      68         103 :     return new AutoValue_LabelVote(LabelType.checkNameInternal(label), value);
      69             :   }
      70             : 
      71             :   public abstract String label();
      72             : 
      73             :   public abstract short value();
      74             : 
      75             :   public String format() {
      76             :     // Max short string length is "-32768".length() == 6.
      77          59 :     return appendTo(new StringBuilder(label().length() + 6), label(), value()).toString();
      78             :   }
      79             : 
      80             :   public String formatWithEquals() {
      81          68 :     if (value() <= (short) 0) {
      82          22 :       return label() + '=' + value();
      83             :     }
      84          68 :     return label() + "=+" + value();
      85             :   }
      86             : 
      87             :   @Override
      88             :   public final String toString() {
      89           0 :     return format();
      90             :   }
      91             : }

Generated by: LCOV version 1.16+git.20220603.dfeb750