1 package com.android.helpers; 2 3 import java.util.Map; 4 5 /** 6 * MetricUtility consist of basic utility methods to construct the metrics 7 * reported at the end of the test. 8 */ 9 public class MetricUtility { 10 11 private static final String KEY_JOIN = "_"; 12 private static final String METRIC_SEPARATOR = ","; 13 14 /** 15 * Append the given array of string to construct the final key used to track the metrics. 16 * 17 * @param keys to append using KEY_JOIN 18 */ constructKey(String... keys)19 public static String constructKey(String... keys) { 20 return String.join(KEY_JOIN, keys); 21 } 22 23 /** 24 * Add metric to the result map. If metric key already exist append the new metric. 25 * 26 * @param metricKey Unique key to track the metric. 27 * @param metric metric to track. 28 * @param resultMap map of all the metrics. 29 */ addMetric(String metricKey, long metric, Map<String, StringBuilder> resultMap)30 public static void addMetric(String metricKey, long metric, Map<String, 31 StringBuilder> resultMap) { 32 resultMap.compute(metricKey, (key, value) -> (value == null) ? 33 new StringBuilder().append(metric) : value.append(METRIC_SEPARATOR).append(metric)); 34 } 35 36 /** 37 * Add metric to the result map. If metric key already exist increment the value by 1. 38 * 39 * @param metricKey Unique key to track the metric. 40 * @param resultMap map of all the metrics. 41 */ addMetric(String metricKey, Map<String, Integer> resultMap)42 public static void addMetric(String metricKey, Map<String, 43 Integer> resultMap) { 44 resultMap.compute(metricKey, (key, value) -> (value == null) ? 1 : value + 1); 45 } 46 47 } 48