// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.openapi.util;

import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.xmlb.Constants;
import org.jdom.Element;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Collections;
import java.util.List;

public class JDOMExternalizerUtil {
  /**
   * Adds {@code <option name="{fieldName}" value="{value}"/>} element to the parent.
   */
  public static void writeField(@NotNull Element parent, @NotNull String fieldName, @Nullable String value) {
    Element element = new Element(Constants.OPTION);
    element.setAttribute(Constants.NAME, fieldName);
    element.setAttribute(Constants.VALUE, value == null ? "" : value);
    parent.addContent(element);
  }

  /**
   * Adds {@code <option name="{fieldName}" value="{value}"/>} element to the parent when the value differs from the default.
   */
  public static void writeField(@NotNull Element parent, @NotNull String fieldName, @Nullable String value, @NotNull String defaultValue) {
    if (!defaultValue.equals(value)) {
      writeField(parent, fieldName, value);
    }
  }

  @NotNull
  public static String readField(@NotNull Element parent, @NotNull String fieldName, @NotNull String defaultValue) {
    String val = readField(parent, fieldName);
    return val == null ? defaultValue : val;
  }

  @Nullable
  public static String readField(@NotNull Element parent, @NotNull String fieldName) {
    for (Element element : parent.getChildren(Constants.OPTION)) {
      if (fieldName.equals(element.getAttributeValue(Constants.NAME))) {
        return element.getAttributeValue(Constants.VALUE);
      }
    }
    return null;
  }

  /**
   * Adds {@code <option name="{fieldName}"/>} element to the parent and returns the created element.
   */
  @NotNull
  public static Element writeOption(@NotNull Element parent, @NotNull String fieldName) {
    Element element = new Element(Constants.OPTION);
    element.setAttribute(Constants.NAME, fieldName);
    parent.addContent(element);
    return element;
  }

  @Nullable
  public static Element readOption(@NotNull Element parent, @NotNull String fieldName) {
    for (Element element : parent.getChildren(Constants.OPTION)) {
      if (fieldName.equals(element.getAttributeValue(Constants.NAME))) {
        return element;
      }
    }
    return null;
  }

  /**
   * Adds {@code <{tagName} value="{value}"/>} element to the parent (or just {@code <{tagName}"/>} if the value is {@code null}).
   */
  public static void writeCustomField(@NotNull Element parent, @NotNull String tagName, @Nullable String value) {
    Element element = new Element(tagName);
    if (value != null) {
      element.setAttribute(Constants.VALUE, value);
    }
    parent.addContent(element);
  }

  @Nullable
  public static String readCustomField(@NotNull Element parent, @NotNull String tagName) {
    Element element = parent.getChild(tagName);
    return element != null ? element.getAttributeValue(Constants.VALUE) : null;
  }

  @NotNull
  public static List<String> getChildrenValueAttributes(@NotNull Element parent, @NotNull String childTagName) {
    List<Element> children = parent.getChildren(childTagName);
    if (children.isEmpty()) {
      return Collections.emptyList();
    }
    if (children.size() == 1) {
      String value = children.iterator().next().getAttributeValue(Constants.VALUE);
      return value == null ? Collections.<String>emptyList() : Collections.singletonList(value);
    }
    List<String> values = ContainerUtil.newArrayListWithCapacity(children.size());
    for (Element child : children) {
      String value = child.getAttributeValue(Constants.VALUE);
      if (value != null) {
        values.add(value);
      }
    }
    return values;
  }

  public static void addChildrenWithValueAttribute(@NotNull Element parent,
                                                   @NotNull String childTagName,
                                                   @NotNull List<String> attrValues) {
    for (String value : attrValues) {
      if (value != null) {
        Element child = new Element(childTagName);
        child.setAttribute(Constants.VALUE, value);
        parent.addContent(child);
      }
    }
  }

  //<editor-fold desc="Deprecated stuff.">
  /** @deprecated use {@link #writeCustomField(Element, String, String)} (to be removed in IDEA 2019) */
  @Deprecated
  public static Element addElementWithValueAttribute(@NotNull Element parent, @NotNull String childTagName, @Nullable String attrValue) {
    writeCustomField(parent, childTagName, attrValue);
    return parent.getChild(childTagName);
  }

  /** @deprecated use {@link #readCustomField(Element, String)} (to be removed in IDEA 2019) */
  @Deprecated
  public static String getFirstChildValueAttribute(@NotNull Element parent, @NotNull String childTagName) {
    return readCustomField(parent, childTagName);
  }
  //</editor-fold>
}
@@ -373,16 +373,45 @@
 lable;%0A%0A
+import java.util.Collection;%0A
 import j
@@ -4407,48 +4407,798 @@
 %0A%0A  
-//%3Ceditor-fold desc=%22Deprecated stuff.%22%3E
+@SuppressWarnings(%22deprecation%22)%0A  public static void addChildren(@NotNull Element parent,%0A                                 @NotNull String childElementName,%0A                                 @NotNull Collection%3C? extends JDOMExternalizable%3E children) throws WriteExternalException %7B%0A    for (JDOMExternalizable child : children) %7B%0A      if (child != null) %7B%0A        Element element = new Element(childElementName);%0A        child.writeExternal(element);%0A        parent.addContent(element);%0A      %7D%0A    %7D%0A  %7D%0A%0A  //%3Ceditor-fold desc=%22Deprecated stuff.%22%3E%0A  /** @deprecated use %7B@link #readOption(Element, String)%7D (to be removed in IDEA 2019) */%0A  @Deprecated%0A  public static Element getOption(@NotNull Element parent, @NotNull String fieldName) %7B%0A    return readOption(parent, fieldName);%0A  %7D%0A
 %0A  /

Revert "cleanup (remove deprecated methods)"