package com.intellij.ide.impl;

import com.intellij.openapi.actionSystem.DataConstants;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.extensions.ExtensionPointName;
import com.intellij.openapi.extensions.Extensions;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.util.KeyedLazyInstanceEP;
import com.intellij.util.containers.HashMap;
import org.jetbrains.annotations.Nullable;

import java.lang.reflect.Array;
import java.util.Map;

public abstract class DataValidator <T> {
  private static boolean ourExtensionsLoaded;

  public static final ExtensionPointName<KeyedLazyInstanceEP<DataValidator>> EP_NAME = ExtensionPointName.create("com.intellij.dataValidator");

  Logger LOG = Logger.getInstance("#com.intellij.ide.impl.DataValidator");

  private static final Map<String, DataValidator> ourValidators = new HashMap<String, DataValidator>();
  private static final DataValidator<VirtualFile> VIRTUAL_FILE_VALIDATOR = new DataValidator<VirtualFile>() {
    public VirtualFile findInvalid(final String dataId, VirtualFile file, final Object dataSource) {
      return file.isValid() ? null : file;
    }
  };
  private static final DataValidator<Project> PROJECT_VALIDATOR = new DataValidator<Project>() {
    @Override
    public Project findInvalid(final String dataId, final Project project, final Object dataSource) {
      return project.isDisposed() ? project : null;
    }
  };

  @Nullable
  public abstract T findInvalid(final String dataId, T data, final Object dataSource);

  private static <T> DataValidator<T> getValidator(String dataId) {
    if (!ourExtensionsLoaded) {
      ourExtensionsLoaded = true;
      for(KeyedLazyInstanceEP<DataValidator> ep: Extensions.getExtensions(EP_NAME)) {
        ourValidators.put(ep.key, ep.getInstance());
      }
    }
    return ourValidators.get(dataId);
  }

  public static <T> T findInvalidData(String dataId, Object data, final Object dataSource) {
    if (data == null) return null;
    DataValidator<T> validator = getValidator(dataId);
    if (validator != null) return validator.findInvalid(dataId, (T) data, dataSource);
    return null;
  }

  static {
    ourValidators.put(DataConstants.VIRTUAL_FILE, VIRTUAL_FILE_VALIDATOR);
    ourValidators.put(DataConstants.VIRTUAL_FILE_ARRAY, new ArrayValidator<VirtualFile>(VIRTUAL_FILE_VALIDATOR));
    ourValidators.put(DataConstants.PROJECT, PROJECT_VALIDATOR);
  }

  public static class ArrayValidator<T> extends DataValidator<T[]> {
    private final DataValidator<T> myElementValidator;

    public ArrayValidator(DataValidator<T> elementValidator) {
      myElementValidator = elementValidator;
    }

    public T[] findInvalid(final String dataId, T[] array, final Object dataSource) {
      for (T element : array) {
        LOG.assertTrue(element != null, "Data isn't valid. " + dataId + "=null Provided by: " + dataSource.getClass().getName() + " (" +
                            dataSource.toString() + ")");
        T invalid = myElementValidator.findInvalid(dataId, element, dataSource);
        if (invalid != null) {
          T[] result = (T[])Array.newInstance(array[0].getClass(), 1);
          result[0] = invalid;
          return result;
        }
      }
      return null;
    }
  }

}

@@ -557,15 +557,14 @@
 ator
- 
 %3CT%3E %7B%0A
+
   pr
@@ -692,16 +692,20 @@
 P_NAME =
+%0A   
  Extensi
@@ -1720,16 +1720,17 @@
      for
+ 
 (KeyedLa
@@ -1759,16 +1759,17 @@
 ator%3E ep
+ 
 : Extens
@@ -2159,17 +2159,16 @@
 aId, (T)
- 
 data, da
@@ -2824,24 +2824,57 @@
  : array) %7B%0A
+        if (element == null) %7B%0A  
         LOG.
@@ -2888,23 +2888,13 @@
 rue(
-element != null
+false
 , %22D
@@ -2984,15 +2984,8 @@
 () +
- %22 (%22 +
 %0A   
@@ -3008,16 +3008,27 @@
         
+     %22 (%22 +
  dataSou
@@ -3050,16 +3050,26 @@
 + %22)%22);%0A
+        %7D%0A
         

fix excessive garbage production: don't create assertion message when not needed