<commit_before>package org.jetbrains.sbt.project.data

import java.io.File

import com.intellij.openapi.projectRoots
import com.intellij.openapi.projectRoots.{JavaSdk, ProjectJdkTable, Sdk => OaSdk}
import com.intellij.openapi.util.Condition
import com.intellij.openapi.util.io.FileUtil
import com.intellij.pom.java.LanguageLevel
import org.jetbrains.android.sdk.{AndroidPlatform, AndroidSdkType}
import org.jetbrains.plugins.scala.extensions.inReadAction

import scala.collection.JavaConverters._

/**
 * @author <NAME>
 * @since 7/14/15.
 */
sealed abstract class Sdk

final case class JdkByName(name: String) extends Sdk
final case class JdkByHome(home: File) extends Sdk
final case class JdkByVersion(version: String) extends Sdk
final case class Android(version: String) extends Sdk

object SdkUtils {
  def findProjectSdk(sdk: Sdk): Option[projectRoots.Sdk] = {

    sdk match {
      case Android(version) =>
        findAndroidJdkByVersion(version)
      case JdkByVersion(version) =>
        findMostRecentJdk { sdk: projectRoots.Sdk =>
          Option(sdk.getVersionString).exists(s => s.contains(version))
        }
      case JdkByName(version) =>
        findMostRecentJdk { sdk: projectRoots.Sdk => sdk.getName.contains(version) }
      case JdkByHome(homeFile) =>
        findMostRecentJdk {sdk: projectRoots.Sdk =>
          FileUtil.comparePaths(homeFile.getCanonicalPath, sdk.getHomePath) == 0
        }
    }
  }

  def findMostRecentJdk(condition: projectRoots.Sdk => Boolean): Option[projectRoots.Sdk] = {
    val jdkCondition = { sdk: projectRoots.Sdk => sdk.getSdkType == JavaSdk.getInstance }
    val combinedCondition = { sdk: projectRoots.Sdk => condition(sdk) && jdkCondition(sdk) }
    Option(inReadAction(ProjectJdkTable.getInstance().findMostRecentSdk(combinedCondition)))
  }

  def mostRecentJdk: Option[projectRoots.Sdk] =
    findMostRecentJdk(_ => true)

  def allAndroidSdks: Seq[projectRoots.Sdk] =
    inReadAction(ProjectJdkTable.getInstance().getSdksOfType(AndroidSdkType.getInstance()).asScala)


  def defaultJavaLanguageLevelIn(jdk: projectRoots.Sdk): Option[LanguageLevel] = {
    val JavaLanguageLevels = Map(
      "1.3" -> LanguageLevel.JDK_1_3,
      "1.4" -> LanguageLevel.JDK_1_4,
      "1.5" -> LanguageLevel.JDK_1_5,
      "1.6" -> LanguageLevel.JDK_1_6,
      "1.7" -> LanguageLevel.JDK_1_7,
      "1.8" -> LanguageLevel.JDK_1_8,
      "1.9" -> LanguageLevel.JDK_1_9)
    val jdkVersion = Option(jdk.getVersionString).getOrElse(jdk.getName)

    JavaLanguageLevels.collectFirst {
      case (name, level) if jdkVersion.contains(name) => level
    }
  }

  def javaLanguageLevelFrom(javacOptions: Seq[String]): Option[LanguageLevel] = {
    for {<commit_msg>findMostRecentSdk may pass a null to conditions in some cases. #EA-88958 fixed<commit_after>package org.jetbrains.sbt.project.data

import java.io.File

import com.intellij.openapi.projectRoots
import com.intellij.openapi.projectRoots.{JavaSdk, ProjectJdkTable, Sdk => OaSdk}
import com.intellij.openapi.util.Condition
import com.intellij.openapi.util.io.FileUtil
import com.intellij.pom.java.LanguageLevel
import org.jetbrains.android.sdk.{AndroidPlatform, AndroidSdkType}
import org.jetbrains.plugins.scala.extensions.inReadAction

import scala.collection.JavaConverters._

/**
 * @author <NAME>
 * @since 7/14/15.
 */
sealed abstract class Sdk

final case class JdkByName(name: String) extends Sdk
final case class JdkByHome(home: File) extends Sdk
final case class JdkByVersion(version: String) extends Sdk
final case class Android(version: String) extends Sdk

object SdkUtils {
  def findProjectSdk(sdk: Sdk): Option[projectRoots.Sdk] = {

    sdk match {
      case Android(version) =>
        findAndroidJdkByVersion(version)
      case JdkByVersion(version) =>
        findMostRecentJdk { sdk: projectRoots.Sdk =>
          Option(sdk.getVersionString).exists(s => s.contains(version))
        }
      case JdkByName(version) =>
        findMostRecentJdk { sdk: projectRoots.Sdk => sdk.getName.contains(version) }
      case JdkByHome(homeFile) =>
        findMostRecentJdk {sdk: projectRoots.Sdk =>
          FileUtil.comparePaths(homeFile.getCanonicalPath, sdk.getHomePath) == 0
        }
    }
  }

  def findMostRecentJdk(condition: projectRoots.Sdk => Boolean): Option[projectRoots.Sdk] = {
    val jdkCondition = { sdk: projectRoots.Sdk => sdk.getSdkType == JavaSdk.getInstance }
    val combinedCondition = { sdk: projectRoots.Sdk => sdk != null && condition(sdk) && jdkCondition(sdk) }
    Option(inReadAction(ProjectJdkTable.getInstance().findMostRecentSdk(combinedCondition)))
  }

  def mostRecentJdk: Option[projectRoots.Sdk] =
    findMostRecentJdk(_ => true)

  def allAndroidSdks: Seq[projectRoots.Sdk] =
    inReadAction(ProjectJdkTable.getInstance().getSdksOfType(AndroidSdkType.getInstance()).asScala)


  def defaultJavaLanguageLevelIn(jdk: projectRoots.Sdk): Option[LanguageLevel] = {
    val JavaLanguageLevels = Map(
      "1.3" -> LanguageLevel.JDK_1_3,
      "1.4" -> LanguageLevel.JDK_1_4,
      "1.5" -> LanguageLevel.JDK_1_5,
      "1.6" -> LanguageLevel.JDK_1_6,
      "1.7" -> LanguageLevel.JDK_1_7,
      "1.8" -> LanguageLevel.JDK_1_8,
      "1.9" -> LanguageLevel.JDK_1_9)
    val jdkVersion = Option(jdk.getVersionString).getOrElse(jdk.getName)

    JavaLanguageLevels.collectFirst {
      case (name, level) if jdkVersion.contains(name) => level
    }
  }

  def javaLanguageLevelFrom(javacOptions: Seq[String]): Option[LanguageLevel] = {
    for {
{'fraction_non_alphanumeric': 0.11468556888404216, 'fraction_numerical': 0.013267902580879681, 'mean_word_length': 4.076568265682657, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 47, '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': '665151', 'n_tokens_mistral': 1938, 'n_tokens_neox': 1867, 'n_words': 323}