intersect method

Returns a PackageVersionNumberGeneralLibraryConstraint that only allows PackageVersionNumberGeneralLibrarys allowed by both this and other.

Implementation

@override
PackageVersionNumberGeneralLibraryConstraint intersect(
    PackageVersionNumberGeneralLibraryConstraint other) {
  if (other.isEmpty) return other;
  if (other is PackageVersionNumberGeneralLibraryUnion) {
    return other.intersect(this);
  }

  // A range and a PackageVersionNumberGeneralLibrary just yields the version if it's in the range.
  if (other is PackageVersionNumberGeneralLibrary) {
    return allows(other)
        ? other
        : PackageVersionNumberGeneralLibraryConstraint.empty;
  }

  if (other is PackageVersionNumberGeneralLibraryRange) {
    // Intersect the two ranges.
    PackageVersionNumberGeneralLibrary? intersectMin;
    bool intersectIncludeMin;
    if (allowsLower(this, other)) {
      if (strictlyLower(this, other)) {
        return PackageVersionNumberGeneralLibraryConstraint.empty;
      }
      intersectMin = other.min;
      intersectIncludeMin = other.includeMin;
    } else {
      if (strictlyLower(other, this)) {
        return PackageVersionNumberGeneralLibraryConstraint.empty;
      }
      intersectMin = min;
      intersectIncludeMin = includeMin;
    }

    PackageVersionNumberGeneralLibrary? intersectMax;
    bool intersectIncludeMax;
    if (allowsHigher(this, other)) {
      intersectMax = other.max;
      intersectIncludeMax = other.includeMax;
    } else {
      intersectMax = max;
      intersectIncludeMax = includeMax;
    }

    if (intersectMin == null && intersectMax == null) {
      // Open range.
      return PackageVersionNumberGeneralLibraryRange();
    }

    // If the range is just a single version.
    if (intersectMin == intersectMax) {
      // Because we already verified that the lower range isn't strictly
      // lower, there must be some overlap.
      assert(intersectIncludeMin && intersectIncludeMax);
      return intersectMin!;
    }

    // If we got here, there is an actual range.
    return PackageVersionNumberGeneralLibraryRange(
        min: intersectMin,
        max: intersectMax,
        includeMin: intersectIncludeMin,
        includeMax: intersectIncludeMax,
        alwaysIncludeMaxPreRelease: true);
  }

  throw ArgumentError(
      'Unknown PackageVersionNumberGeneralLibraryConstraint type $other.');
}