LCOV - code coverage report
Current view: top level - metrics/dropwizard - CallbackMetricImpl0.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 25 30 83.3 %
Date: 2022-11-19 15:00:39 Functions: 7 8 87.5 %

          Line data    Source code
       1             : // Copyright (C) 2015 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.metrics.dropwizard;
      16             : 
      17             : import com.codahale.metrics.Gauge;
      18             : import com.codahale.metrics.MetricRegistry;
      19             : import com.google.gerrit.metrics.CallbackMetric0;
      20             : 
      21             : class CallbackMetricImpl0<V> extends CallbackMetric0<V> implements CallbackMetricGlue {
      22             :   @SuppressWarnings("unchecked")
      23             :   static <V> V zeroFor(Class<V> valueClass) {
      24          16 :     if (valueClass == Integer.class) {
      25          16 :       return (V) Integer.valueOf(0);
      26          16 :     } else if (valueClass == Long.class) {
      27          16 :       return (V) Long.valueOf(0);
      28          16 :     } else if (valueClass == Double.class) {
      29          16 :       return (V) Double.valueOf(0);
      30          16 :     } else if (valueClass == Float.class) {
      31           0 :       return (V) Float.valueOf(0);
      32          16 :     } else if (valueClass == String.class) {
      33          16 :       return (V) "";
      34          15 :     } else if (valueClass == Boolean.class) {
      35          15 :       return (V) Boolean.FALSE;
      36             :     } else {
      37           0 :       throw new IllegalArgumentException("unsupported value type " + valueClass.getName());
      38             :     }
      39             :   }
      40             : 
      41             :   private final DropWizardMetricMaker metrics;
      42             :   private final MetricRegistry registry;
      43             :   private final String name;
      44             :   private volatile V value;
      45             : 
      46             :   CallbackMetricImpl0(
      47          16 :       DropWizardMetricMaker metrics, MetricRegistry registry, String name, Class<V> valueType) {
      48          16 :     this.metrics = metrics;
      49          16 :     this.registry = registry;
      50          16 :     this.name = name;
      51          16 :     this.value = zeroFor(valueType);
      52          16 :   }
      53             : 
      54             :   @Override
      55          16 :   public void beginSet() {}
      56             : 
      57             :   @Override
      58          16 :   public void endSet() {}
      59             : 
      60             :   @Override
      61             :   public void set(V value) {
      62          16 :     this.value = value;
      63          16 :   }
      64             : 
      65             :   @Override
      66             :   public void remove() {
      67           0 :     metrics.remove(name);
      68           0 :     registry.remove(name);
      69           0 :   }
      70             : 
      71             :   @Override
      72             :   public void register(Runnable trigger) {
      73          16 :     registry.register(
      74             :         name,
      75             :         (Gauge<V>)
      76             :             () -> {
      77           1 :               trigger.run();
      78           1 :               return value;
      79             :             });
      80          16 :   }
      81             : }

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