/*
 * 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.internal.component.external.descriptor;

import com.google.common.base.Objects;
import com.google.common.collect.ImmutableSet;
import org.gradle.api.artifacts.ModuleIdentifier;
import org.gradle.internal.component.model.Exclude;
import org.gradle.internal.component.model.IvyArtifactName;

import javax.annotation.Nullable;
import java.util.Set;

public class DefaultExclude implements Exclude {
    private final ModuleIdentifier moduleId;
    private final IvyArtifactName artifact;
    private final Set<String> configurations;
    private final String patternMatcher;

    public DefaultExclude(ModuleIdentifier id, IvyArtifactName artifact, String[] configurations, @Nullable String patternMatcher) {
        this.moduleId = id;
        this.artifact = artifact;
        this.patternMatcher = patternMatcher;
        this.configurations = ImmutableSet.copyOf(configurations);
    }

    public DefaultExclude(ModuleIdentifier id, String[] configurations, @Nullable String patternMatcher) {
        this.moduleId = id;
        this.artifact = null;
        this.patternMatcher = patternMatcher;
        this.configurations = ImmutableSet.copyOf(configurations);
    }

    public DefaultExclude(ModuleIdentifier id) {
        this.moduleId = id;
        this.artifact = null;
        this.patternMatcher = null;
        this.configurations = ImmutableSet.of();
    }

    @Override
    public String toString() {
        return "{exclude moduleId: " + moduleId + ", artifact: " + artifact + "}";
    }

    @Override
    public ModuleIdentifier getModuleId() {
        return moduleId;
    }

    @Override
    public IvyArtifactName getArtifact() {
        return artifact;
    }

    @Override
    public Set<String> getConfigurations() {
        return configurations;
    }

    @Override
    public String getMatcher() {
        return patternMatcher;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        DefaultExclude that = (DefaultExclude) o;
        return Objects.equal(moduleId, that.moduleId)
            && Objects.equal(artifact, that.artifact)
            && Objects.equal(configurations, that.configurations)
            && Objects.equal(patternMatcher, that.patternMatcher);
    }

    @Override
    public int hashCode() {
        return Objects.hashCode(moduleId, artifact, configurations, patternMatcher);
    }
}

@@ -674,47 +674,8 @@
 r;%0A%0A
-import com.google.common.base.Objects;%0A
 impo
@@ -2653,32 +2653,33 @@
 alse;%0A        %7D%0A
+%0A
         DefaultE
@@ -2712,24 +2712,25 @@
 ude) o;%0A
+%0A
         
 return O
@@ -2725,39 +2725,29 @@
     
-return Objects.equal(moduleId, 
+if (!moduleId.equals(
 that
@@ -2756,16 +2756,19 @@
 oduleId)
+) %7B
 %0A       
@@ -2776,33 +2776,93 @@
     
-&& Objects.equal(
+return false;%0A        %7D%0A        if (artifact != null ? !artifact.equals(that.
 artifact
 , th
@@ -2857,17 +2857,19 @@
 artifact
-,
+) :
  that.ar
@@ -2874,17 +2874,27 @@
 artifact
-)
+ != null) %7B
 %0A       
@@ -2898,33 +2898,53 @@
         
-&& Objects.equal(
+return false;%0A        %7D%0A        if (!
 configur
@@ -2949,18 +2949,24 @@
 urations
-, 
+.equals(
 that.con
@@ -2977,16 +2977,19 @@
 rations)
+) %7B
 %0A       
@@ -2997,25 +2997,99 @@
     
-&& Objects.equal(
+return false;%0A        %7D%0A        return patternMatcher != null ? patternMatcher.equals(that.
 patt
@@ -3098,17 +3098,19 @@
 nMatcher
-,
+) :
  that.pa
@@ -3113,33 +3113,40 @@
 t.patternMatcher
-)
+ == null
 ;%0A    %7D%0A%0A    @Ov
@@ -3192,83 +3192,287 @@
     
-return Objects.hashCode(moduleId, artifact, configurations, patternMatcher)
+int result = moduleId.hashCode();%0A        result = 31 * result + (artifact != null ? artifact.hashCode() : 0);%0A        result = 31 * result + configurations.hashCode();%0A        result = 31 * result + (patternMatcher != null ? patternMatcher.hashCode() : 0);%0A        return result
 ;%0A  

Make Exclude.hashCode efficient