<commit_before>// Copyright 2000-2019 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.facet.impl;

import com.intellij.configurationStore.ComponentSerializationUtil;
import com.intellij.configurationStore.XmlSerializer;
import com.intellij.facet.*;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.WriteAction;
import com.intellij.openapi.components.PersistentStateComponent;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.util.InvalidDataException;
import com.intellij.openapi.util.JDOMExternalizable;
import com.intellij.openapi.util.WriteExternalException;
import org.jdom.Element;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jps.model.serialization.facet.JpsFacetSerializer;

import java.util.Arrays;

public class FacetUtil {

  public static <F extends Facet> F addFacet(Module module, FacetType<F, ?> type) {
    final ModifiableFacetModel model = FacetManager.getInstance(module).createModifiableModel();
    final F facet = createFacet(module, type);
    ApplicationManager.getApplication().runWriteAction(() -> {
      model.addFacet(facet);
      model.commit();
    });
    return facet;
  }

  private static <F extends Facet, C extends FacetConfiguration> F createFacet(final Module module, final FacetType<F, C> type) {
    return FacetManager.getInstance(module).createFacet(type, type.getPresentableName(), type.createDefaultConfiguration(), null);
  }

  public static void deleteFacet(final Facet facet) {
    WriteAction.runAndWait(() -> {
      if (!isRegistered(facet)) {
        return;
      }

      ModifiableFacetModel model = FacetManager.getInstance(facet.getModule()).createModifiableModel();
      model.removeFacet(facet);
      model.commit();
    });
  }

  public static boolean isRegistered(Facet facet) {
    return Arrays.asList(FacetManager.getInstance(facet.getModule()).getAllFacets()).contains(facet);
  }

  public static void loadFacetConfiguration(final @NotNull FacetConfiguration configuration, final @Nullable Element config)
      throws InvalidDataException {
    if (config != null) {
      if (configuration instanceof PersistentStateComponent) {
        ComponentSerializationUtil.loadComponentState((PersistentStateComponent)configuration, config);
      }
      else {
        configuration.readExternal(config);
      }
    }
  }
<commit_msg>[web tests] don't add 2 web facets with the same name<commit_after>// Copyright 2000-2019 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.facet.impl;

import com.intellij.configurationStore.ComponentSerializationUtil;
import com.intellij.configurationStore.XmlSerializer;
import com.intellij.facet.*;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.WriteAction;
import com.intellij.openapi.components.PersistentStateComponent;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.util.InvalidDataException;
import com.intellij.openapi.util.JDOMExternalizable;
import com.intellij.openapi.util.WriteExternalException;
import org.jdom.Element;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jps.model.serialization.facet.JpsFacetSerializer;

import java.util.Arrays;

public class FacetUtil {

  public static <F extends Facet> F addFacet(Module module, FacetType<F, ?> type) {
    return addFacet(module, type, type.getPresentableName());
  }

  public static <F extends Facet> F addFacet(@NotNull Module module, @NotNull FacetType<F, ?> type, @NotNull String facetName) {
    final ModifiableFacetModel model = FacetManager.getInstance(module).createModifiableModel();
    final F facet = createFacet(module, type, facetName);
    ApplicationManager.getApplication().runWriteAction(() -> {
      model.addFacet(facet);
      model.commit();
    });
    return facet;
  }

  private static <F extends Facet, C extends FacetConfiguration> F createFacet(final Module module,
                                                                               final FacetType<F, C> type, @NotNull String facetName) {
    return FacetManager.getInstance(module).createFacet(type, facetName, type.createDefaultConfiguration(), null);
  }

  public static void deleteFacet(final Facet facet) {
    WriteAction.runAndWait(() -> {
      if (!isRegistered(facet)) {
        return;
      }

      ModifiableFacetModel model = FacetManager.getInstance(facet.getModule()).createModifiableModel();
      model.removeFacet(facet);
      model.commit();
    });
  }

  public static boolean isRegistered(Facet facet) {
    return Arrays.asList(FacetManager.getInstance(facet.getModule()).getAllFacets()).contains(facet);
  }

  public static void loadFacetConfiguration(final @NotNull FacetConfiguration configuration, final @Nullable Element config)
      throws InvalidDataException {
    if (config != null) {
      if (configuration instanceof PersistentStateComponent) {
        ComponentSerializationUtil.loadComponentState((PersistentStateComponent)configuration, config);
      }
      else {
        configuration.readExternal(config);
      }
    }
  }

{'fraction_non_alphanumeric': 0.09713346722658389, 'fraction_numerical': 0.00383421581157568, 'mean_word_length': 4.4076999012833165, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3138409', 'n_tokens_mistral': 1566, 'n_tokens_neox': 1518, 'n_words': 339}