/*-
 * #%L
 * Vert.x utilities from Braintags
 * %%
 * Copyright (C) 2017 Braintags GmbH
 * %%
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * #L%
 */
package de.braintags.vertx.util.json;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;

import com.fasterxml.jackson.annotation.JsonCreator.Mode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.guava.GuavaModule;
import com.fasterxml.jackson.datatype.joda.JodaModule;
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;

import io.vertx.core.json.Json;

public class JsonConfig {

  private static final List<WeakReference<ObjectMapper>> objectMappers = new ArrayList<>();
  private static final List<Config> configs = new ArrayList<>();

  static {
    // add default configuration for all mappers
    addConfig(mapper -> {
      mapper.registerModule(new ParameterNamesModule(Mode.DELEGATING));
      mapper.registerModule(new JodaModule());
      mapper.registerModule(new GuavaModule());
      mapper.registerModule(new ArrayMapModule());
    });
    configureObjectMapper(Json.mapper);
    configureObjectMapper(Json.prettyMapper);
  }

  public static void staticInit() {

  }

  public static void configureObjectMapper(final ObjectMapper mapper) {
    synchronized (configs) {
      for (WeakReference<ObjectMapper> objectMapper : objectMappers) {
        if (objectMapper.get() == mapper) {
          return;
        }
      }

      objectMappers.add(new WeakReference<>(mapper));
      for (Config config : configs) {
        config.configure(mapper);
      }
    }
  }

  public static void addConfig(final Config config) {
    synchronized (configs) {
      if (configs.contains(config)) {
        return;
      }

      ListIterator<WeakReference<ObjectMapper>> iter = objectMappers.listIterator();
      while (iter.hasNext()) {
        WeakReference<ObjectMapper> objectMapperRef = iter.next();
        ObjectMapper objectMapper = objectMapperRef.get();
        if (objectMapper == null) {
          iter.remove();
        } else {
          config.configure(objectMapper);
        }
      }
      configs.add(config);
    }
  }

  @FunctionalInterface
  public interface Config {
    void configure(ObjectMapper mapper);
  }

}

@@ -925,36 +925,16 @@
 tMappers
- = new ArrayList%3C%3E()
 ;%0A  priv
@@ -970,16 +970,80 @@
  configs
+;%0A%0A  static %7B%0A    objectMappers = new ArrayList%3C%3E();%0A    configs
  = new A
@@ -1055,28 +1055,16 @@
 ist%3C%3E();
-%0A%0A  static %7B
 %0A    // 
@@ -1824,24 +1824,40 @@
 onfig : 
+new ArrayList%3C%3E(
 configs)
  %7B%0A     
@@ -1840,32 +1840,33 @@
 yList%3C%3E(configs)
+)
  %7B%0A        confi
@@ -2102,24 +2102,40 @@
 er%3E%3E iter = 
+new ArrayList%3C%3E(
 objectMapper
@@ -2135,16 +2135,17 @@
 tMappers
+)
 .listIte

fix concurrent modification