// Copyright 2000-2020 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 org.jetbrains.plugins.gradle.tooling.util;

import org.gradle.util.GradleVersion;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.plugins.gradle.tooling.annotation.TargetVersions;

/**
 * @author Vladislav.Soroka
 */
public final class VersionMatcher {
  private static final String RANGE_TOKEN = " <=> ";

  @NotNull
  private final GradleVersion myGradleVersion;

  public VersionMatcher(@NotNull GradleVersion version) {
    myGradleVersion = version;
  }

  public boolean isVersionMatch(@Nullable TargetVersions targetVersions) {
    if (targetVersions == null || targetVersions.value().isEmpty()) return true;

    final GradleVersion current = adjust(myGradleVersion, targetVersions.checkBaseVersions());

    if (targetVersions.value().endsWith("+")) {
      String minVersion = targetVersions.value().substring(0, targetVersions.value().length() - 1);
      return compare(current, minVersion, targetVersions.checkBaseVersions()) >= 0;
    }
    else if (targetVersions.value().startsWith("<")) {
      if (targetVersions.value().startsWith("<=")) {
        String maxVersion = targetVersions.value().substring(2);
        return compare(current, maxVersion, targetVersions.checkBaseVersions()) <= 0;
      }
      else {
        String maxVersion = targetVersions.value().substring(1);
        return compare(current, maxVersion, targetVersions.checkBaseVersions()) < 0;
      }
    }
    else {
      final int rangeIndex = targetVersions.value().indexOf(RANGE_TOKEN);
      if (rangeIndex != -1) {
        String minVersion = targetVersions.value().substring(0, rangeIndex);
        String maxVersion = targetVersions.value().substring(rangeIndex + RANGE_TOKEN.length());
        return compare(current, minVersion, targetVersions.checkBaseVersions()) >= 0 &&
               compare(current, maxVersion, targetVersions.checkBaseVersions()) <= 0;
      }
      else {
        return compare(current, targetVersions.value(), targetVersions.checkBaseVersions()) == 0;
      }
    }
  }

  private static int compare(@NotNull GradleVersion gradleVersion, @NotNull String otherGradleVersion, boolean checkBaseVersions) {
    return gradleVersion.compareTo(adjust(GradleVersion.version(otherGradleVersion), checkBaseVersions));
  }

  private static GradleVersion adjust(@NotNull GradleVersion version, boolean checkBaseVersions) {
    return checkBaseVersions ? version.getBaseVersion() : version;
  }
}

@@ -765,16 +765,247 @@
  == null
+) return true;%0A    return isVersionMatch(targetVersions.value(), targetVersions.checkBaseVersions());%0A  %7D%0A%0A  public boolean isVersionMatch(@Nullable String targetVersions, boolean checkBaseVersions) %7B%0A    if (targetVersions == null
  %7C%7C targ
@@ -1014,24 +1014,16 @@
 Versions
-.value()
 .isEmpty
@@ -1094,39 +1094,24 @@
 dleVersion, 
-targetVersions.
 checkBaseVer
@@ -1115,18 +1115,16 @@
 Versions
-()
 );%0A%0A    
@@ -1142,24 +1142,16 @@
 ersions.
-value().
 endsWith
@@ -1196,32 +1196,24 @@
 getVersions.
-value().
 substring(0,
@@ -1228,24 +1228,16 @@
 ersions.
-value().
 length()
@@ -1281,39 +1281,24 @@
 minVersion, 
-targetVersions.
 checkBaseVer
@@ -1298,26 +1298,24 @@
 BaseVersions
-()
 ) %3E= 0;%0A    
@@ -1340,32 +1340,24 @@
 getVersions.
-value().
 startsWith(%22
@@ -1388,24 +1388,16 @@
 ersions.
-value().
 startsWi
@@ -1447,32 +1447,24 @@
 getVersions.
-value().
 substring(2)
@@ -1505,39 +1505,24 @@
 maxVersion, 
-targetVersions.
 checkBaseVer
@@ -1518,34 +1518,32 @@
 heckBaseVersions
-()
 ) %3C= 0;%0A      %7D%0A
@@ -1594,32 +1594,24 @@
 getVersions.
-value().
 substring(1)
@@ -1652,39 +1652,24 @@
 maxVersion, 
-targetVersions.
 checkBaseVer
@@ -1673,18 +1673,16 @@
 Versions
-()
 ) %3C 0;%0A 
@@ -1749,24 +1749,16 @@
 ersions.
-value().
 indexOf(
@@ -1840,32 +1840,24 @@
 getVersions.
-value().
 substring(0,
@@ -1917,16 +1917,8 @@
 ons.
-value().
 subs
@@ -1999,39 +1999,24 @@
 minVersion, 
-targetVersions.
 checkBaseVer
@@ -2020,18 +2020,16 @@
 Versions
-()
 ) %3E= 0 &
@@ -2040,21 +2040,16 @@
         
-     
 compare(
@@ -2069,31 +2069,16 @@
 ersion, 
-targetVersions.
 checkBas
@@ -2086,18 +2086,16 @@
 Versions
-()
 ) %3C= 0;%0A
@@ -2165,33 +2165,10 @@
 ions
-.value(), targetVersions.
+, 
 chec
@@ -2180,18 +2180,16 @@
 Versions
-()
 ) == 0;%0A
@@ -2203,24 +2203,25 @@
 %0A    %7D%0A  %7D%0A%0A
+%0A
   private st

Fix NewMultiplatformProjectImportingTest for newer plugin versions