/*
 * Copyright 2016 the original author or authors.
 *
 * 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 org.gradle.api.internal.artifacts.ivyservice.resolveengine.excludes;

import java.util.Collection;

abstract class AbstractCompositeExclusion extends AbstractModuleExclusion {
    private int hashCode = -1;

    abstract Collection<AbstractModuleExclusion> getFilters();

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("{");
        builder.append(getClass().getSimpleName());
        for (AbstractModuleExclusion spec : getFilters()) {
            builder.append(' ');
            builder.append(spec);
        }
        builder.append("}");
        return builder.toString();
    }

    @Override
    protected boolean doExcludesSameModulesAs(AbstractModuleExclusion other) {
        AbstractCompositeExclusion spec = (AbstractCompositeExclusion) other;
        return implies(spec) && spec.implies(this);
    }

    @Override
    protected boolean doEquals(Object obj) {
        AbstractCompositeExclusion other = (AbstractCompositeExclusion) obj;
        return hashCode() == other.hashCode() && getFilters().equals(other.getFilters());
    }

    @Override
    protected int doHashCode() {
        if (hashCode != -1) {
            return hashCode;
        }
        hashCode = getFilters().hashCode();
        return hashCode;
    }

    /**
     * Returns true if for every spec in this spec, there is a corresponding spec in the given spec that excludesSameModulesAs().
     */
    protected boolean implies(AbstractCompositeExclusion spec) {
        for (AbstractModuleExclusion thisSpec : getFilters()) {
            boolean found = false;
            for (AbstractModuleExclusion otherSpec : spec.getFilters()) {
                if (thisSpec.excludesSameModulesAs(otherSpec)) {
                    found = true;
                    break;
                }
            }
            if (!found) {
                return false;
            }
        }
        return true;
    }
}

@@ -699,28 +699,90 @@
 ort 
-java.util.Collection
+com.google.common.collect.Sets;%0A%0Aimport java.util.Collection;%0Aimport java.util.Set
 ;%0A%0Aa
@@ -1522,49 +1522,1178 @@
     
-return implies(spec) && spec.implies(this
+Collection%3CAbstractModuleExclusion%3E thisFilters = getFilters();%0A        Collection%3CAbstractModuleExclusion%3E otherFilters = spec.getFilters();%0A%0A        // To make the comparison faster, we compute the sets of specs that exist in this exclusion, but not in the other%0A        // and the set of specs that exist in the other and not in this one. Then we only need to check if the missing%0A        // from one set have an equivalent in the missing of the other set, which is much faster than checking all of them%0A        Set%3CAbstractModuleExclusion%3E miss1 = Sets.newHashSetWithExpectedSize(thisFilters.size());%0A        Set%3CAbstractModuleExclusion%3E miss2 = Sets.newHashSetWithExpectedSize(otherFilters.size());%0A        for (AbstractModuleExclusion exclusion : thisFilters) %7B%0A            if (!otherFilters.contains(exclusion)) %7B%0A                miss1.add(exclusion);%0A            %7D%0A        %7D%0A        for (AbstractModuleExclusion otherFilter : otherFilters) %7B%0A            if (!thisFilters.contains(otherFilter)) %7B%0A                miss2.add(otherFilter);%0A            %7D%0A        %7D%0A        return (miss1.isEmpty() && miss2.isEmpty()) %7C%7C (implies(miss1, miss2) && implies(miss2, miss1)
 );%0A 
@@ -3299,24 +3299,28 @@
 implies(
+Set%3C
 Abstract
 Composit
@@ -3315,34 +3315,155 @@
 ract
-CompositeExclusion spec) %7B
+ModuleExclusion%3E miss1, Set%3CAbstractModuleExclusion%3E miss2) %7B%0A        if (miss1.isEmpty() %7C%7C miss2.isEmpty()) %7B%0A            return false;%0A        %7D
 %0A   
@@ -3503,36 +3503,29 @@
  thisSpec : 
-getFilters()
+miss1
 ) %7B%0A        
@@ -3604,33 +3604,21 @@
 rSpec : 
-spec.getFilters()
+miss2
 ) %7B%0A    

Optimize composite exclusion `implies` check by only checking the difference between the 2 specs