Search in sources :

Example 1 with ExcludeNothing

use of org.gradle.api.internal.artifacts.ivyservice.resolveengine.excludes.specs.ExcludeNothing in project gradle by gradle.

the class NormalizingExcludeFactory method doIntersect.

private ExcludeSpec doIntersect(Set<ExcludeSpec> specs) {
    specs = simplifySet(ExcludeAnyOf.class, specs);
    FlattenOperationResult flattened = flatten(ExcludeAllOf.class, specs, ExcludeNothing.class::isInstance, ExcludeEverything.class::isInstance);
    if (flattened.fastExit) {
        return nothing();
    }
    Set<ExcludeSpec> result = flattened.result;
    if (result.isEmpty()) {
        return everything();
    }
    if (result.size() > 1) {
        // try simplify
        ExcludeSpec[] asArray = result.toArray(new ExcludeSpec[0]);
        boolean simplified = false;
        for (int i = 0; i < asArray.length; i++) {
            ExcludeSpec left = asArray[i];
            if (left != null) {
                for (int j = 0; j < asArray.length; j++) {
                    ExcludeSpec right = asArray[j];
                    if (right != null && i != j) {
                        ExcludeSpec merged = intersections.tryIntersect(left, right);
                        if (merged != null) {
                            if (merged instanceof ExcludeNothing) {
                                return merged;
                            }
                            left = merged;
                            asArray[i] = merged;
                            asArray[j] = null;
                            simplified = true;
                        }
                    }
                }
            }
        }
        if (simplified) {
            result = Arrays.stream(asArray).filter(Objects::nonNull).collect(toSet());
        }
    }
    return Optimizations.optimizeCollection(this, result, delegate::allOf);
}
Also used : ExcludeNothing(org.gradle.api.internal.artifacts.ivyservice.resolveengine.excludes.specs.ExcludeNothing) Objects(java.util.Objects) ExcludeEverything(org.gradle.api.internal.artifacts.ivyservice.resolveengine.excludes.specs.ExcludeEverything) ExcludeAnyOf(org.gradle.api.internal.artifacts.ivyservice.resolveengine.excludes.specs.ExcludeAnyOf) ExcludeSpec(org.gradle.api.internal.artifacts.ivyservice.resolveengine.excludes.specs.ExcludeSpec)

Example 2 with ExcludeNothing

use of org.gradle.api.internal.artifacts.ivyservice.resolveengine.excludes.specs.ExcludeNothing in project gradle by gradle.

the class Intersections method intersectAnyOf.

private ExcludeSpec intersectAnyOf(ExcludeAnyOf left, ExcludeSpec right) {
    Set<ExcludeSpec> leftComponents = left.getComponents();
    if (right instanceof ExcludeAnyOf) {
        Set<ExcludeSpec> rightComponents = ((ExcludeAnyOf) right).getComponents();
        Set<ExcludeSpec> common = Sets.newHashSet(leftComponents);
        common.retainAll(rightComponents);
        if (common.size() >= 1) {
            ExcludeSpec alpha = asUnion(common);
            if (leftComponents.equals(common) || rightComponents.equals(common)) {
                return alpha;
            }
            Set<ExcludeSpec> remainderLeft = Sets.newHashSet(leftComponents);
            remainderLeft.removeAll(common);
            Set<ExcludeSpec> remainderRight = Sets.newHashSet(rightComponents);
            remainderRight.removeAll(common);
            ExcludeSpec unionLeft = asUnion(remainderLeft);
            ExcludeSpec unionRight = asUnion(remainderRight);
            ExcludeSpec beta = factory.allOf(unionLeft, unionRight);
            return factory.anyOf(alpha, beta);
        } else {
            // slowest path, full distribution
            // (A ∪ B) ∩ (C ∪ D) = (A ∩ C) ∪ (A ∩ D) ∪ (B ∩ C) ∪ (B ∩ D)
            Set<ExcludeSpec> intersections = Sets.newHashSetWithExpectedSize(leftComponents.size() * rightComponents.size());
            for (ExcludeSpec leftSpec : leftComponents) {
                for (ExcludeSpec rightSpec : rightComponents) {
                    ExcludeSpec merged = tryIntersect(leftSpec, rightSpec);
                    if (merged == null) {
                        merged = factory.allOf(leftSpec, rightSpec);
                    }
                    if (!(merged instanceof ExcludeNothing)) {
                        intersections.add(merged);
                    }
                }
            }
            return asUnion(intersections);
        }
    } else {
        // Here, we will distribute A ∩ (B ∪ C) if, and only if, at
        // least one of the distribution operations (A ∩ B) can be simplified
        ExcludeSpec[] excludeSpecs = leftComponents.toArray(new ExcludeSpec[0]);
        ExcludeSpec[] intersections = null;
        for (int i = 0; i < excludeSpecs.length; i++) {
            ExcludeSpec excludeSpec = tryIntersect(excludeSpecs[i], right);
            if (excludeSpec != null) {
                if (intersections == null) {
                    intersections = new ExcludeSpec[excludeSpecs.length];
                }
                intersections[i] = excludeSpec;
            }
        }
        if (intersections != null) {
            Set<ExcludeSpec> simplified = Sets.newHashSetWithExpectedSize(excludeSpecs.length);
            for (int i = 0; i < intersections.length; i++) {
                ExcludeSpec intersection = intersections[i];
                if (intersection instanceof ExcludeNothing) {
                    continue;
                }
                if (intersection != null) {
                    simplified.add(intersection);
                } else {
                    simplified.add(factory.allOf(excludeSpecs[i], right));
                }
            }
            return asUnion(simplified);
        }
    }
    return null;
}
Also used : ExcludeNothing(org.gradle.api.internal.artifacts.ivyservice.resolveengine.excludes.specs.ExcludeNothing) ExcludeAnyOf(org.gradle.api.internal.artifacts.ivyservice.resolveengine.excludes.specs.ExcludeAnyOf) ExcludeSpec(org.gradle.api.internal.artifacts.ivyservice.resolveengine.excludes.specs.ExcludeSpec)

Aggregations

ExcludeAnyOf (org.gradle.api.internal.artifacts.ivyservice.resolveengine.excludes.specs.ExcludeAnyOf)2 ExcludeNothing (org.gradle.api.internal.artifacts.ivyservice.resolveengine.excludes.specs.ExcludeNothing)2 ExcludeSpec (org.gradle.api.internal.artifacts.ivyservice.resolveengine.excludes.specs.ExcludeSpec)2 Objects (java.util.Objects)1 ExcludeEverything (org.gradle.api.internal.artifacts.ivyservice.resolveengine.excludes.specs.ExcludeEverything)1