LCOV - code coverage report
Current view: top level - server/logging - MutableTags.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 33 36 91.7 %
Date: 2022-11-19 15:00:39 Functions: 9 10 90.0 %

          Line data    Source code
       1             : // Copyright (C) 2018 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.logging;
      16             : 
      17             : import static java.util.Objects.requireNonNull;
      18             : 
      19             : import com.google.common.base.MoreObjects;
      20             : import com.google.common.collect.ImmutableSetMultimap;
      21             : import com.google.common.collect.MultimapBuilder;
      22             : import com.google.common.collect.SetMultimap;
      23             : import com.google.common.flogger.context.Tags;
      24             : 
      25         151 : public class MutableTags {
      26         151 :   private final SetMultimap<String, String> tagMap =
      27         151 :       MultimapBuilder.hashKeys().hashSetValues().build();
      28         151 :   private Tags tags = Tags.empty();
      29             : 
      30             :   public Tags getTags() {
      31          32 :     return tags;
      32             :   }
      33             : 
      34             :   /**
      35             :    * Adds a tag if a tag with the same name and value doesn't exist yet.
      36             :    *
      37             :    * @param name the name of the tag
      38             :    * @param value the value of the tag
      39             :    * @return {@code true} if the tag was added, {@code false} if the tag was not added because it
      40             :    *     already exists
      41             :    */
      42             :   public boolean add(String name, String value) {
      43         151 :     requireNonNull(name, "tag name is required");
      44         151 :     requireNonNull(value, "tag value is required");
      45         151 :     boolean ret = tagMap.put(name, value);
      46         151 :     if (ret) {
      47         151 :       buildTags();
      48             :     }
      49         151 :     return ret;
      50             :   }
      51             : 
      52             :   /**
      53             :    * Removes the tag with the given name and value.
      54             :    *
      55             :    * @param name the name of the tag
      56             :    * @param value the value of the tag
      57             :    */
      58             :   public void remove(String name, String value) {
      59         151 :     requireNonNull(name, "tag name is required");
      60         151 :     requireNonNull(value, "tag value is required");
      61         151 :     if (tagMap.remove(name, value)) {
      62         151 :       buildTags();
      63             :     }
      64         151 :   }
      65             : 
      66             :   /**
      67             :    * Checks if the contained tag map is empty.
      68             :    *
      69             :    * @return {@code true} if there are no tags, otherwise {@code false}
      70             :    */
      71             :   public boolean isEmpty() {
      72         151 :     return tagMap.isEmpty();
      73             :   }
      74             : 
      75             :   /** Clears all tags. */
      76             :   public void clear() {
      77           1 :     tagMap.clear();
      78           1 :     tags = Tags.empty();
      79           1 :   }
      80             : 
      81             :   /**
      82             :    * Returns the tags as Multimap.
      83             :    *
      84             :    * @return the tags as Multimap
      85             :    */
      86             :   public ImmutableSetMultimap<String, String> asMap() {
      87         115 :     return ImmutableSetMultimap.copyOf(tagMap);
      88             :   }
      89             : 
      90             :   /**
      91             :    * Replaces the existing tags with the provided tags.
      92             :    *
      93             :    * @param tags the tags that should be set.
      94             :    */
      95             :   void set(ImmutableSetMultimap<String, String> tags) {
      96         103 :     tagMap.clear();
      97         103 :     tags.forEach(tagMap::put);
      98         103 :     buildTags();
      99         103 :   }
     100             : 
     101             :   private void buildTags() {
     102         151 :     if (tagMap.isEmpty()) {
     103         151 :       if (tags.isEmpty()) {
     104           0 :         return;
     105             :       }
     106         151 :       tags = Tags.empty();
     107         151 :       return;
     108             :     }
     109             : 
     110         151 :     Tags.Builder tagsBuilder = Tags.builder();
     111         151 :     tagMap.forEach(tagsBuilder::addTag);
     112         151 :     tags = tagsBuilder.build();
     113         151 :   }
     114             : 
     115             :   @Override
     116             :   public String toString() {
     117           0 :     buildTags();
     118           0 :     return MoreObjects.toStringHelper(this).add("tags", tags).toString();
     119             :   }
     120             : }

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