LCOV - code coverage report
Current view: top level - server/account - GroupBackends.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 16 19 84.2 %
Date: 2022-11-19 15:00:39 Functions: 6 6 100.0 %

          Line data    Source code
       1             : // Copyright (C) 2012 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.account;
      16             : 
      17             : import static java.util.Comparator.comparing;
      18             : 
      19             : import com.google.common.collect.Iterables;
      20             : import com.google.gerrit.common.Nullable;
      21             : import com.google.gerrit.entities.GroupReference;
      22             : import com.google.gerrit.server.project.ProjectState;
      23             : import java.util.Collection;
      24             : import java.util.Comparator;
      25             : 
      26             : /** Utility class for dealing with a GroupBackend. */
      27             : public class GroupBackends {
      28             : 
      29          43 :   public static final Comparator<GroupReference> GROUP_REF_NAME_COMPARATOR =
      30          43 :       comparing(GroupReference::getName);
      31             : 
      32             :   /**
      33             :    * Runs {@link GroupBackend#suggest(String, ProjectState)} and filters the result to return the
      34             :    * best suggestion, or null if one does not exist.
      35             :    *
      36             :    * @param groupBackend the group backend
      37             :    * @param name the name for which to suggest groups
      38             :    * @return the best single GroupReference suggestion
      39             :    */
      40             :   @Nullable
      41             :   public static GroupReference findBestSuggestion(GroupBackend groupBackend, String name) {
      42           8 :     return findBestSuggestion(groupBackend, name, null);
      43             :   }
      44             : 
      45             :   /**
      46             :    * Runs {@link GroupBackend#suggest(String, ProjectState)} and filters the result to return the
      47             :    * best suggestion, or null if one does not exist.
      48             :    *
      49             :    * @param groupBackend the group backend
      50             :    * @param name the name for which to suggest groups
      51             :    * @param project the project for which to suggest groups
      52             :    * @return the best single GroupReference suggestion
      53             :    */
      54             :   @Nullable
      55             :   public static GroupReference findBestSuggestion(
      56             :       GroupBackend groupBackend, String name, @Nullable ProjectState project) {
      57           8 :     Collection<GroupReference> refs = groupBackend.suggest(name, project);
      58           8 :     if (refs.size() == 1) {
      59           8 :       return Iterables.getOnlyElement(refs);
      60             :     }
      61             : 
      62           6 :     for (GroupReference ref : refs) {
      63           0 :       if (isExactSuggestion(ref, name)) {
      64           0 :         return ref;
      65             :       }
      66           0 :     }
      67           6 :     return null;
      68             :   }
      69             : 
      70             :   /**
      71             :    * Runs {@link GroupBackend#suggest(String, ProjectState)} and filters the result to return the
      72             :    * exact suggestion, or null if one does not exist.
      73             :    *
      74             :    * @param groupBackend the group backend
      75             :    * @param name the name for which to suggest groups
      76             :    * @return the exact single GroupReference suggestion
      77             :    */
      78             :   @Nullable
      79             :   public static GroupReference findExactSuggestion(GroupBackend groupBackend, String name) {
      80          41 :     return findExactSuggestion(groupBackend, name, null);
      81             :   }
      82             : 
      83             :   /**
      84             :    * Runs {@link GroupBackend#suggest(String, ProjectState)} and filters the result to return the
      85             :    * exact suggestion, or null if one does not exist.
      86             :    *
      87             :    * @param groupBackend the group backend
      88             :    * @param name the name for which to suggest groups
      89             :    * @param project the project for which to suggest groups
      90             :    * @return the exact single GroupReference suggestion
      91             :    */
      92             :   @Nullable
      93             :   public static GroupReference findExactSuggestion(
      94             :       GroupBackend groupBackend, String name, ProjectState project) {
      95          41 :     Collection<GroupReference> refs = groupBackend.suggest(name, project);
      96          41 :     for (GroupReference ref : refs) {
      97          18 :       if (isExactSuggestion(ref, name)) {
      98          18 :         return ref;
      99             :       }
     100           2 :     }
     101          38 :     return null;
     102             :   }
     103             : 
     104             :   /** Returns whether the GroupReference is an exact suggestion for the name. */
     105             :   public static boolean isExactSuggestion(GroupReference ref, String name) {
     106          18 :     return ref.getName().equalsIgnoreCase(name) || ref.getUUID().get().equals(name);
     107             :   }
     108             : 
     109             :   private GroupBackends() {}
     110             : }

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