From b0b57534dd4e69913753b9acf1c3a495b7738f6a Mon Sep 17 00:00:00 2001 From: Joss Armstrong Date: Mon, 25 Feb 2019 10:32:13 +0000 Subject: Fix for casting/subclassing in MetricRegistry Put in code to avoid cast exceptions from this class and updated tests Issue-ID: APPC-1480 Change-Id: I8b54d93e52eb523f08d13e874b4d18d05feede6b Signed-off-by: Joss Armstrong --- .../appc/metricservice/impl/MetricRegistryImpl.java | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) (limited to 'appc-metric/appc-metric-bundle/src/main') diff --git a/appc-metric/appc-metric-bundle/src/main/java/org/onap/appc/metricservice/impl/MetricRegistryImpl.java b/appc-metric/appc-metric-bundle/src/main/java/org/onap/appc/metricservice/impl/MetricRegistryImpl.java index 48d1c6fac..d371fed43 100644 --- a/appc-metric/appc-metric-bundle/src/main/java/org/onap/appc/metricservice/impl/MetricRegistryImpl.java +++ b/appc-metric/appc-metric-bundle/src/main/java/org/onap/appc/metricservice/impl/MetricRegistryImpl.java @@ -25,9 +25,10 @@ package org.onap.appc.metricservice.impl; +import java.util.ArrayList; +import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; - import org.onap.appc.metricservice.MetricRegistry; import org.onap.appc.metricservice.metric.Counter; import org.onap.appc.metricservice.metric.Metric; @@ -40,6 +41,8 @@ import org.onap.appc.metricservice.policy.impl.PolicyBuilderFactoryImpl; public class MetricRegistryImpl implements MetricRegistry { private String name; + // Map can contain Counters, DispatchingFunctionMetrics and DMaapRequestCounterMetrics + // and there are methods to retrieve only the 'Counter' types private Map concurrentMetricMap = new ConcurrentHashMap<>(); public MetricRegistryImpl(String name) { @@ -68,12 +71,22 @@ public class MetricRegistryImpl implements MetricRegistry { @Override public Counter counter(String value) { - return (Counter)concurrentMetricMap.get(value); + Metric metric = concurrentMetricMap.get(value); + if (metric instanceof Counter) { + return (Counter)metric; + } + else return null; } @Override public Counter[] counters() { - return (Counter[])concurrentMetricMap.values().toArray(); + List counterList = new ArrayList<>(); + for (Metric m: concurrentMetricMap.values()) { + if (m instanceof Counter) { + counterList.add((Counter) m); + } + } + return counterList.toArray(new Counter[counterList.size()]); } @Override -- cgit 1.2.3-korg