<commit_before>/*
 * @author max
 */
package com.intellij.openapi.util;

import com.intellij.openapi.extensions.*;
import com.intellij.util.KeyedLazyInstanceEP;
import gnu.trove.THashMap;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;

@SuppressWarnings({"NonPrivateFieldAccessedInSynchronizedContext"})
public abstract class KeyedExtensionCollector<T, KeyT> {
  private final Map<String, List<KeyedLazyInstanceEP<T>>> myRegistry = new THashMap<String, List<KeyedLazyInstanceEP<T>>>();
  private final Map<String, List<T>> myExplicitExtensions = new THashMap<String, List<T>>();
  private final Map<String, List<T>> myCache = new HashMap<String, List<T>>();
  private final Object lock = new Object();

  public KeyedExtensionCollector(@NonNls String epName) {
    ExtensionPointName<KeyedLazyInstanceEP<T>> typesafe = ExtensionPointName.create(epName);
    final ExtensionPoint<KeyedLazyInstanceEP<T>> point = Extensions.getRootArea().getExtensionPoint(typesafe);
    point.addExtensionPointListener(new ExtensionPointListener<KeyedLazyInstanceEP<T>>() {
      public void extensionAdded(final KeyedLazyInstanceEP<T> bean, @Nullable final PluginDescriptor pluginDescriptor) {
        synchronized (lock) {
          List<KeyedLazyInstanceEP<T>> beans = myRegistry.get(bean.key);
          if (beans == null) {
            beans = new CopyOnWriteArrayList<KeyedLazyInstanceEP<T>>();
            myRegistry.put(bean.key, beans);
          }
          beans.add(bean);
          myCache.remove(bean.key);
        }
      }

      public void extensionRemoved(final KeyedLazyInstanceEP<T> bean, @Nullable final PluginDescriptor pluginDescriptor) {
        synchronized (lock) {
          List<KeyedLazyInstanceEP<T>> beans = myRegistry.get(bean.key);
          if (beans != null) {
            beans.remove(bean);
          }
          myCache.remove(bean.key);
        }
      }
    });
  }

  public void addExpicitExtension(KeyT key, T t) {
    synchronized (lock) {
      final String skey = keyToString(key);
      List<T> list = myExplicitExtensions.get(skey);
      if (list == null) {
        list = new ArrayList<T>();
        myExplicitExtensions.put(skey, list);
      }
      list.add(t);
      myCache.remove(skey);
    }
  }

  public void removeExpicitExtension(KeyT key, T t) {
    synchronized (lock) {
      final String skey = keyToString(key);
      List<T> list = myExplicitExtensions.get(skey);
      if (list != null) {
        list.remove(t);
        myCache.remove(skey);
      }
    }
  }

  protected abstract String keyToString(KeyT key);

  public List<T> forKey(KeyT key) {
    synchronized (lock) {
      final String stringKey = keyToString(key);
      List<T> cache = myCache.get(stringKey);
      if (cache == null) {
        cache = buildExtensions(stringKey);
        myCache.put(stringKey, cache);
      }
      return cache;
    }
  }

  private List<T> buildExtensions(final String key) {
    final List<T> explicit = myExplicitExtensions.get(key);
    List<T> result = explicit != null ? new ArrayList<T>(explicit) : new ArrayList<T>();
    final List<KeyedLazyInstanceEP<T>> beans = myRegistry.get(key);
    if (beans != null) {
      for (KeyedLazyInstanceEP<T> bean : beans) {
        result.add(bean.getInstance());
      }
    }
    return result;
  }
}<commit_msg>Don't rely on extension point exist.<commit_after>/*
 * @author max
 */
package com.intellij.openapi.util;

import com.intellij.openapi.extensions.*;
import com.intellij.util.KeyedLazyInstanceEP;
import gnu.trove.THashMap;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;

@SuppressWarnings({"NonPrivateFieldAccessedInSynchronizedContext"})
public abstract class KeyedExtensionCollector<T, KeyT> {
  private final Map<String, List<KeyedLazyInstanceEP<T>>> myRegistry = new THashMap<String, List<KeyedLazyInstanceEP<T>>>();
  private final Map<String, List<T>> myExplicitExtensions = new THashMap<String, List<T>>();
  private final Map<String, List<T>> myCache = new HashMap<String, List<T>>();
  private final Object lock = new Object();

  public KeyedExtensionCollector(@NonNls String epName) {
    ExtensionPointName<KeyedLazyInstanceEP<T>> typesafe = ExtensionPointName.create(epName);
    if (Extensions.getRootArea().hasExtensionPoint(epName)) {
      final ExtensionPoint<KeyedLazyInstanceEP<T>> point = Extensions.getRootArea().getExtensionPoint(typesafe);
      point.addExtensionPointListener(new ExtensionPointListener<KeyedLazyInstanceEP<T>>() {
        public void extensionAdded(final KeyedLazyInstanceEP<T> bean, @Nullable final PluginDescriptor pluginDescriptor) {
          synchronized (lock) {
            List<KeyedLazyInstanceEP<T>> beans = myRegistry.get(bean.key);
            if (beans == null) {
              beans = new CopyOnWriteArrayList<KeyedLazyInstanceEP<T>>();
              myRegistry.put(bean.key, beans);
            }
            beans.add(bean);
            myCache.remove(bean.key);
          }
        }

        public void extensionRemoved(final KeyedLazyInstanceEP<T> bean, @Nullable final PluginDescriptor pluginDescriptor) {
          synchronized (lock) {
            List<KeyedLazyInstanceEP<T>> beans = myRegistry.get(bean.key);
            if (beans != null) {
              beans.remove(bean);
            }
            myCache.remove(bean.key);
          }
        }
      });
    }
  }

  public void addExpicitExtension(KeyT key, T t) {
    synchronized (lock) {
      final String skey = keyToString(key);
      List<T> list = myExplicitExtensions.get(skey);
      if (list == null) {
        list = new ArrayList<T>();
        myExplicitExtensions.put(skey, list);
      }
      list.add(t);
      myCache.remove(skey);
    }
  }

  public void removeExpicitExtension(KeyT key, T t) {
    synchronized (lock) {
      final String skey = keyToString(key);
      List<T> list = myExplicitExtensions.get(skey);
      if (list != null) {
        list.remove(t);
        myCache.remove(skey);
      }
    }
  }

  protected abstract String keyToString(KeyT key);

  public List<T> forKey(KeyT key) {
    synchronized (lock) {
      final String stringKey = keyToString(key);
      List<T> cache = myCache.get(stringKey);
      if (cache == null) {
        cache = buildExtensions(stringKey);
        myCache.put(stringKey, cache);
      }
      return cache;
    }
  }

  private List<T> buildExtensions(final String key) {
    final List<T> explicit = myExplicitExtensions.get(key);
    List<T> result = explicit != null ? new ArrayList<T>(explicit) : new ArrayList<T>();
    final List<KeyedLazyInstanceEP<T>> beans = myRegistry.get(key);
    if (beans != null) {
      for (KeyedLazyInstanceEP<T> bean : beans) {
        result.add(bean.getInstance());
      }
    }
    return result;
  }
}
{'fraction_non_alphanumeric': 0.11845614035087719, 'fraction_numerical': 0.0, 'mean_word_length': 3.27987987987988, 'pattern_counts': {'":': 0, '<': 87, '<?xml version=': 0, '>': 87, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7251236', 'n_tokens_mistral': 2179, 'n_tokens_neox': 2168, 'n_words': 447}