/*
 * Copyright 2000-2015 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.pom.java;

import com.intellij.core.JavaCoreBundle;
import com.intellij.openapi.roots.LanguageLevelModuleExtension;
import com.intellij.openapi.roots.LanguageLevelProjectExtension;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.util.ArrayUtil;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
 * @author dsl
 * @see LanguageLevelProjectExtension
 * @see LanguageLevelModuleExtension
 */
public enum LanguageLevel {
  JDK_1_3("Java 1.3", JavaCoreBundle.message("jdk.1.3.language.level.description"), "1.3"),
  JDK_1_4("Java 1.4", JavaCoreBundle.message("jdk.1.4.language.level.description"), "1.4"),
  JDK_1_5("Java 5.0", JavaCoreBundle.message("jdk.1.5.language.level.description"), "1.5", "5"),
  JDK_1_6("Java 6", JavaCoreBundle.message("jdk.1.6.language.level.description"), "1.6", "6"),
  JDK_1_7("Java 7", JavaCoreBundle.message("jdk.1.7.language.level.description"), "1.7", "7"),
  JDK_1_8("Java 8", JavaCoreBundle.message("jdk.1.8.language.level.description"), "1.8", "8"),
  JDK_1_9("Java 9", JavaCoreBundle.message("jdk.1.9.language.level.description"), "1.9", "9"),
  JDK_X("Java X", JavaCoreBundle.message("jdk.X.language.level.description"), "");

  public static final LanguageLevel HIGHEST = JDK_1_8; // TODO! when language level 9 is really supported, update this field
  public static final Key<LanguageLevel> KEY = Key.create("LANGUAGE_LEVEL");

  private final String myName;
  private final String myPresentableText;
  private final String myCompilerComplianceDefaultOption;
  private final String[] myCompilerComplianceOptionVariants;

  /**
   * @param compilerComplianceAlternativeOptions synonyms supported by javac -source parameter (see http://docs.oracle.com/javase/8/docs/technotes/tools/windows/javac.html)
   */
  LanguageLevel(@NotNull String name, @NotNull @Nls String presentableText, @NotNull String compilerComplianceDefaultOption,
                @NotNull String... compilerComplianceAlternativeOptions) {
    myName = name;
    myPresentableText = presentableText;
    myCompilerComplianceDefaultOption = compilerComplianceDefaultOption;
    myCompilerComplianceOptionVariants = ArrayUtil.prepend(compilerComplianceDefaultOption, compilerComplianceAlternativeOptions);
  }

  @NotNull
  public String getName() {
    return myName;
  }

  @NotNull
  @Nls
  public String getPresentableText() {
    return myPresentableText;
  }

  public boolean isAtLeast(@NotNull LanguageLevel level) {
    return compareTo(level) >= 0;
  }

  public boolean isLessThan(@NotNull LanguageLevel level) {
    return compareTo(level) < 0;
  }

  /**
   * String representation of the level, suitable to pass as a value of compiler's "-source" and "-target" options
   */
  public String getCompilerComplianceDefaultOption() {
    return myCompilerComplianceDefaultOption;
  }

  /**
   * Parses string accordingly to format of '-source' parameter of javac. Synonyms ("8" for "1.8") are supported.
   */
  @Nullable
  public static LanguageLevel parse(@Nullable String compilerComplianceOption) {
    if (StringUtil.isEmpty(compilerComplianceOption)) return null;

    for (LanguageLevel level : values()) {
      if (ArrayUtil.contains(compilerComplianceOption, level.myCompilerComplianceOptionVariants)) {
        return level;
      }
    }
    return null;
  }
}

@@ -17,17 +17,17 @@
 2000-201
-5
+7
  JetBrai
@@ -916,24 +916,75 @@
 .ArrayUtil;%0A
+import com.intellij.util.containers.ContainerUtil;%0A
 import org.j
@@ -1872,15 +1872,8 @@
 n%22),
- %221.9%22,
  %229%22
@@ -2239,66 +2239,8 @@
 xt;%0A
-  private final String myCompilerComplianceDefaultOption;%0A
   pr
@@ -2286,23 +2286,16 @@
 ceOption
-Variant
 s;%0A%0A  /*
@@ -2330,27 +2330,16 @@
 ance
-Alternative
 Options 
 syno
@@ -2338,15 +2338,15 @@
 ons 
-synonym
+version
 s su
@@ -2360,21 +2360,23 @@
  by 
-j
+J
 avac 
+'
 -source
+'
  par
@@ -2385,14 +2385,28 @@
 eter
- (see 
+%0A   * @see %3Ca href=%22
 http
@@ -2472,17 +2472,37 @@
 vac.html
-)
+%22%3EJavac Reference%3C/a%3E
 %0A   */%0A 
@@ -2516,25 +2516,16 @@
 geLevel(
-@NotNull 
 String n
@@ -2528,25 +2528,16 @@
 ng name,
- @NotNull
  @Nls St
@@ -2561,82 +2561,8 @@
 ext,
- @NotNull String compilerComplianceDefaultOption,%0A                @NotNull
  Str
@@ -2582,35 +2582,24 @@
 erCompliance
-Alternative
 Options) %7B%0A 
@@ -2685,185 +2685,43 @@
 ance
-DefaultOption = compilerComplianceDefaultOption;%0A    myCompilerComplianceOptionVariants = ArrayUtil.prepend(compilerComplianceDefaultOption, compilerComplianceAlternativ
+Options = compilerComplianc
 eOptions
 );%0A 
@@ -2716,17 +2716,16 @@
 eOptions
-)
 ;%0A  %7D%0A%0A 
@@ -3287,29 +3287,26 @@
 mpliance
-Default
 Option
+s%5B0%5D
 ;%0A  %7D%0A%0A 
@@ -3594,62 +3594,57 @@
 ll;%0A
-%0A
     
-for (LanguageLevel level : values()) %7B%0A      if (
+return ContainerUtil.find(values(), level -%3E 
 Arra
@@ -3720,76 +3720,14 @@
 tion
-Variants)) %7B%0A        return level;%0A      %7D%0A    %7D%0A    return null
+s))
 ;%0A  %7D%0A%7D
-%0A

correct compiler compliance option for Java 9; cleanup