LCOV - code coverage report
Current view: top level - entities - GroupReference.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 15 16 93.8 %
Date: 2022-11-19 15:00:39 Functions: 11 12 91.7 %

          Line data    Source code
       1             : // Copyright (C) 2010 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 static java.util.Objects.requireNonNull;
      18             : 
      19             : import com.google.auto.value.AutoValue;
      20             : import com.google.auto.value.extension.memoized.Memoized;
      21             : import com.google.gerrit.common.Nullable;
      22             : 
      23             : /** Describes a group within a projects {@link AccessSection}s. */
      24             : @AutoValue
      25         155 : public abstract class GroupReference implements Comparable<GroupReference> {
      26             : 
      27             :   private static final String PREFIX = "group ";
      28             : 
      29             :   public static GroupReference forGroup(GroupDescription.Basic group) {
      30          16 :     return GroupReference.create(group.getGroupUUID(), group.getName());
      31             :   }
      32             : 
      33             :   public static boolean isGroupReference(String configValue) {
      34         153 :     return configValue != null && configValue.startsWith(PREFIX);
      35             :   }
      36             : 
      37             :   @Nullable
      38             :   public static String extractGroupName(String configValue) {
      39         153 :     if (!isGroupReference(configValue)) {
      40           5 :       return null;
      41             :     }
      42         153 :     return configValue.substring(PREFIX.length()).trim();
      43             :   }
      44             : 
      45             :   @Nullable
      46             :   public abstract AccountGroup.UUID getUUID();
      47             : 
      48             :   public abstract String getName();
      49             : 
      50             :   /**
      51             :    * Create a group reference.
      52             :    *
      53             :    * @param uuid UUID of the group, must not be {@code null}
      54             :    * @param name the group name, must not be {@code null}
      55             :    */
      56             :   public static GroupReference create(AccountGroup.UUID uuid, String name) {
      57         155 :     return new AutoValue_GroupReference(requireNonNull(uuid), requireNonNull(name));
      58             :   }
      59             : 
      60             :   /**
      61             :    * Create a group reference where the group's name couldn't be resolved.
      62             :    *
      63             :    * @param name the group name, must not be {@code null}
      64             :    */
      65             :   public static GroupReference create(String name) {
      66         154 :     return new AutoValue_GroupReference(null, name);
      67             :   }
      68             : 
      69             :   @Override
      70             :   public final int compareTo(GroupReference o) {
      71         155 :     return uuid(this).compareTo(uuid(o));
      72             :   }
      73             : 
      74             :   private static String uuid(GroupReference a) {
      75         155 :     if (a.getUUID() != null && a.getUUID().get() != null) {
      76         155 :       return a.getUUID().get();
      77             :     }
      78             : 
      79           2 :     return "?";
      80             :   }
      81             : 
      82             :   @Memoized
      83             :   @Override
      84             :   public int hashCode() {
      85         153 :     return uuid(this).hashCode();
      86             :   }
      87             : 
      88             :   @Override
      89             :   public final boolean equals(Object o) {
      90          10 :     return o instanceof GroupReference && compareTo((GroupReference) o) == 0;
      91             :   }
      92             : 
      93             :   @Override
      94             :   public final String toString() {
      95           0 :     return "Group[" + getName() + " / " + getUUID() + "]";
      96             :   }
      97             : 
      98             :   public String toConfigValue() {
      99         153 :     return PREFIX + getName();
     100             :   }
     101             : }

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