/*
 * Copyright 2015 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.artifact;

import com.google.common.collect.Maps;
import org.gradle.api.artifacts.ResolvedArtifact;

import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;

public class DefaultResolvedArtifactResults implements ResolvedArtifactResults {
    // Transient state: held between resolving graph and resolving actual artifacts
    private Map<Long, ArtifactSet> artifactSets = Maps.newLinkedHashMap();

    // Artifact State : held for the life of a build
    private Set<ResolvedArtifact> artifacts;
    private Map<Long, Set<ResolvedArtifact>> resolvedArtifactsById;

    @Override
    public Set<ResolvedArtifact> getArtifacts() {
        assertArtifactsResolved();
        return new LinkedHashSet<ResolvedArtifact>(artifacts);
    }

    @Override
    public Set<ResolvedArtifact> getArtifacts(long id) {
        assertArtifactsResolved();
        Set<ResolvedArtifact> a = resolvedArtifactsById.get(id);
        assert a != null : "Unable to find artifacts for id: " + id;
        return a;
    }

    public void addArtifactSet(ArtifactSet artifactSet) {
        artifactSets.put(artifactSet.getId(), artifactSet);
    }

    public void resolveNow() {
        if (artifacts == null) {
            artifacts = new LinkedHashSet<ResolvedArtifact>();
            resolvedArtifactsById = new LinkedHashMap<Long, Set<ResolvedArtifact>>();
            for (Map.Entry<Long, ArtifactSet> entry : artifactSets.entrySet()) {
                Set<ResolvedArtifact> resolvedArtifacts = entry.getValue().getArtifacts();
                artifacts.addAll(resolvedArtifacts);
                resolvedArtifactsById.put(entry.getKey(), resolvedArtifacts);
            }

            // Release ResolvedArtifactSet instances so we're not holding onto state
            artifactSets = null;
        }
    }

    private void assertArtifactsResolved() {
        if (artifacts == null) {
            throw new IllegalStateException("Cannot access artifacts before they are explicitly resolved.");
        }
    }
}

@@ -692,47 +692,8 @@
 t;%0A%0A
-import com.google.common.collect.Maps;%0A
 impo
@@ -756,26 +756,16 @@
 va.util.
-LinkedHash
 Map;%0Aimp
@@ -782,62 +782,135 @@
 til.
-LinkedHash
 Set;%0A
+%0A
 import 
-java.util.Map;%0Aimport java.util.
+static com.google.common.collect.Maps.newLinkedHashMap;%0Aimport static com.google.common.collect.Sets.newLinkedHash
 Set;
@@ -1126,21 +1126,16 @@
 tSets = 
-Maps.
 newLinke
@@ -1427,25 +1427,24 @@
   return new
- 
 LinkedHashSe
@@ -1444,34 +1444,16 @@
 dHashSet
-%3CResolvedArtifact%3E
 (artifac
@@ -1941,25 +1941,24 @@
 ifacts = new
- 
 LinkedHashSe
@@ -1958,34 +1958,16 @@
 dHashSet
-%3CResolvedArtifact%3E
 ();%0A    
@@ -2001,17 +2001,16 @@
 Id = new
- 
 LinkedHa
@@ -2018,37 +2018,8 @@
 hMap
-%3CLong, Set%3CResolvedArtifact%3E%3E
 ();%0A

Use collection factories to take advantage of type inference