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.ArrayList;
import java.util.Collection;

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

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

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

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

  public final void register(@NotNull Collector collector) {
    try {
      registry.register(collector);
      collectors.add(collector);
    } catch (IllegalArgumentException ignore) {
    }
  }
}

@@ -223,25 +223,23 @@
 va.util.
-ArrayList
+HashMap
 ;%0Aimport
@@ -249,26 +249,19 @@
 va.util.
-Collection
+Map
 ;%0A%0Apubli
@@ -398,19 +398,23 @@
 ull 
+Map%3C
 Collect
-ion%3C
+or, 
 Coll
@@ -441,17 +441,15 @@
 new 
-ArrayList
+HashMap
 %3C%3E()
@@ -700,16 +700,25 @@
 lectors.
+keySet().
 forEach(
@@ -838,37 +838,11 @@
     
-try %7B%0A      registry.register
+if 
 (col
@@ -851,17 +851,14 @@
 ctor
-);%0A      
+s.put(
 coll
@@ -866,14 +866,10 @@
 ctor
-s.add(
+, 
 coll
@@ -878,57 +878,55 @@
 tor)
-;%0A    %7D catch (IllegalArgumentException ignore) %7B
+ == null) %7B%0A      registry.register(collector);
 %0A   

fix IAE while registering already registered collector #4