package io.vertx.ext.prometheus.metrics;

import io.prometheus.client.Collector;
import io.prometheus.client.CollectorRegistry;
import io.vertx.core.spi.metrics.Metrics;
import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.Map;

public abstract class PrometheusMetrics implements Metrics {
  private final @NotNull CollectorRegistry registry;
  private final @NotNull Map<Collector, Collector> collectors = new HashMap<>();

  protected PrometheusMetrics(@NotNull CollectorRegistry registry) {
    this.registry = registry;
  }

  @Override
  public final boolean isEnabled() {
    return !collectors.isEmpty();
  }

  @Override
  public final void close() {
    collectors.keySet().forEach(registry::unregister);
    collectors.clear();
  }

  public final void register(@NotNull Collector collector) {
    if (collectors.put(collector, collector) == null) {
      registry.register(collector);
    }
  }
}

@@ -223,23 +223,25 @@
 va.util.
-HashMap
+ArrayList
 ;%0Aimport
@@ -251,19 +251,26 @@
 va.util.
-Map
+Collection
 ;%0A%0Apubli
@@ -407,23 +407,19 @@
 ull 
-Map%3C
 Collect
-or, 
+ion%3C
 Coll
@@ -446,15 +446,17 @@
 new 
-HashMap
+ArrayList
 %3C%3E()
@@ -711,17 +711,8 @@
 ors.
-keySet().
 forE
@@ -836,11 +836,37 @@
     
-if 
+try %7B%0A      registry.register
 (col
@@ -875,14 +875,17 @@
 ctor
-s.put(
+);%0A      
 coll
@@ -893,10 +893,14 @@
 ctor
-, 
+s.add(
 coll
@@ -909,55 +909,57 @@
 tor)
- == null) %7B%0A      registry.register(collector);
+;%0A    %7D catch (IllegalArgumentException ignore) %7B
 %0A   

revert ugly IAE