/*
 * Copyright 2010 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;

import org.gradle.api.Buildable;
import org.gradle.api.artifacts.ModuleVersionIdentifier;
import org.gradle.api.artifacts.ResolvedArtifact;
import org.gradle.api.artifacts.ResolvedModuleVersion;
import org.gradle.api.artifacts.component.ComponentArtifactIdentifier;
import org.gradle.api.internal.artifacts.ivyservice.modulecache.dynamicversions.DefaultResolvedModuleVersion;
import org.gradle.api.internal.artifacts.ivyservice.resolveengine.artifact.ResolvableArtifact;
import org.gradle.api.tasks.TaskDependency;
import org.gradle.internal.Factory;
import org.gradle.internal.UncheckedException;
import org.gradle.internal.component.local.model.LocalComponentArtifactMetadata;
import org.gradle.internal.component.model.IvyArtifactName;

import java.io.File;

public class DefaultResolvedArtifact implements ResolvedArtifact, Buildable, ResolvableArtifact {
    private final ModuleVersionIdentifier owner;
    private final IvyArtifactName artifact;
    private final ComponentArtifactIdentifier artifactId;
    private final TaskDependency buildDependencies;
    private volatile Factory<File> artifactSource;
    private File file;
    private Throwable failure;

    public DefaultResolvedArtifact(ModuleVersionIdentifier owner, IvyArtifactName artifact, ComponentArtifactIdentifier artifactId, TaskDependency buildDependencies, Factory<File> artifactSource) {
        this.owner = owner;
        this.artifact = artifact;
        this.artifactId = artifactId;
        this.buildDependencies = buildDependencies;
        this.artifactSource = artifactSource;
    }

    public DefaultResolvedArtifact(ModuleVersionIdentifier owner, IvyArtifactName artifact, ComponentArtifactIdentifier artifactId, TaskDependency buildDependencies, File artifactFile) {
        this.owner = owner;
        this.artifact = artifact;
        this.artifactId = artifactId;
        this.buildDependencies = buildDependencies;
        this.file = artifactFile;
    }

    @Override
    public TaskDependency getBuildDependencies() {
        return buildDependencies;
    }

    public ResolvedModuleVersion getModuleVersion() {
        return new DefaultResolvedModuleVersion(owner);
    }

    @Override
    public ComponentArtifactIdentifier getId() {
        return artifactId;
    }

    @Override
    public String toString() {
        return artifactId.getDisplayName();
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == this) {
            return true;
        }
        if (obj == null || obj.getClass() != getClass()) {
            return false;
        }
        DefaultResolvedArtifact other = (DefaultResolvedArtifact) obj;
        return other.owner.equals(owner) && other.artifactId.equals(artifactId);
    }

    @Override
    public int hashCode() {
        return owner.hashCode() ^ artifactId.hashCode();
    }

    @Override
    public String getName() {
        return artifact.getName();
    }

    @Override
    public String getType() {
        return artifact.getType();
    }

    @Override
    public String getExtension() {
        return artifact.getExtension();
    }

    @Override
    public String getClassifier() {
        return artifact.getClassifier();
    }

    @Override
    public ResolvedArtifact toPublicView() {
        return this;
    }

    @Override
    public boolean isResolveSynchronously() {
        if (artifactId instanceof LocalComponentArtifactMetadata) {
            // Don't bother resolving local components asynchronously
            return true;
        }
        synchronized (this) {
            return file != null || failure != null;
        }
    }

    @Override
    public File getFile() {
        synchronized (this) {
            if (file != null) {
                return file;
            }
            if (failure != null) {
                throw UncheckedException.throwAsUncheckedException(failure);
            }
            try {
                file = artifactSource.create();
                return file;
            } catch (Throwable e) {
                failure = e;
                throw UncheckedException.throwAsUncheckedException(failure);
            } finally {
                artifactSource = null;
            }
        }
    }
}

@@ -1779,16 +1779,25 @@
 private 
+volatile 
 File fil
@@ -1811,16 +1811,25 @@
 private 
+volatile 
 Throwabl
@@ -4205,42 +4205,8 @@
   %7D%0A
-        synchronized (this) %7B%0A    
     
@@ -4249,26 +4249,16 @@
 = null;%0A
-        %7D%0A
     %7D%0A%0A 
@@ -4310,52 +4310,261 @@
     
-synchronized (this) %7B%0A            if (file !
+// This method tries to minimize the number of volatile read/writes.%0A        // Do NOT try to inline the variables there.%0A        File f = file;%0A        if (f == null) %7B%0A            synchronized (this) %7B%0A                f = file;%0A                if (f =
 = nu
@@ -4581,34 +4581,50 @@
             
-return
+    Throwable err =
  f
+a
 il
+ur
 e;%0A         
@@ -4616,36 +4616,32 @@
 lure;%0A          
-  %7D%0A
             if (
@@ -4638,21 +4638,15 @@
     
-  
 if (
-failure
+err
  != 
@@ -4645,32 +4645,40 @@
 (err != null) %7B%0A
+        
                 
@@ -4724,34 +4724,37 @@
 edException(
-failure);%0A
+err);%0A       
             
@@ -4745,32 +4745,33 @@
                 
+ 
 %7D%0A            tr
@@ -4768,16 +4768,24 @@
         
+       
+ 
 try %7B%0A  
@@ -4798,20 +4798,25 @@
         
-file
+        f
  = artif
@@ -4855,21 +4855,34 @@
     
-return file;%0A
+        file = f;%0A        
     
@@ -4933,21 +4933,72 @@
     
-failure = e;%0A
+        err = e;%0A                        failure = err;%0A        
     
@@ -5064,18 +5064,22 @@
 ion(
-failure);%0A
+err);%0A        
     
@@ -5114,16 +5114,24 @@
         
+    
+    
 artifact
@@ -5149,34 +5149,32 @@
 ll;%0A            
-%7D%0A
         %7D%0A    %7D%0A
@@ -5162,21 +5162,81 @@
            %7D
+%0A                %7D%0A            %7D%0A        %7D%0A        return f;
 %0A    %7D%0A%7D%0A

Implement cheaper locking of resolved artifacts