/*
 * Copyright 2017 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.vcs.internal;

import org.gradle.cache.CacheRepository;
import org.gradle.cache.FileLockManager;
import org.gradle.cache.PersistentCache;
import org.gradle.vcs.VersionControlSpec;
import org.gradle.vcs.VersionControlSystem;
import org.gradle.vcs.VersionRef;
import org.gradle.vcs.git.internal.GitVersionControlSystem;

import java.io.File;
import java.util.Set;

import static org.gradle.cache.internal.filelock.LockOptionsBuilder.mode;

public class DefaultVersionControlSystemFactory implements VersionControlSystemFactory {
    private final CacheRepository cacheRepository;

    DefaultVersionControlSystemFactory(CacheRepository cacheRepository) {
        this.cacheRepository = cacheRepository;
    }

    @Override
    public VersionControlSystem create(VersionControlSpec spec) {
        // TODO: Register these mappings somewhere
        VersionControlSystem vcs;
        if (spec instanceof DirectoryRepositorySpec) {
            vcs = new SimpleVersionControlSystem();
        } else {
            vcs = new GitVersionControlSystem();
        }
        return new LockingVersionControlSystem(vcs, cacheRepository);
    }

    private static final class LockingVersionControlSystem implements VersionControlSystem {
        private final VersionControlSystem delegate;
        private final CacheRepository cacheRepository;

        private LockingVersionControlSystem(VersionControlSystem delegate, CacheRepository cacheRepository) {
            this.delegate = delegate;
            this.cacheRepository = cacheRepository;
        }

        @Override
        public Set<VersionRef> getAvailableVersions(VersionControlSpec spec) {
            return delegate.getAvailableVersions(spec);
        }

        @Override
        public File populate(final File versionDir, final VersionRef ref, final VersionControlSpec spec) {
            PersistentCache cache = cacheRepository
                .cache(versionDir)
                .withLockOptions(mode(FileLockManager.LockMode.Exclusive))
                .open();
            DelegatePopulator populator = new DelegatePopulator(delegate, versionDir, ref, spec);
            try {
                cache.useCache(populator);
            } finally {
                cache.close();
            }
            return populator.workingDir;
        }

        private static final class DelegatePopulator implements Runnable {
            private final VersionControlSystem delegate;
            private final File versionDir;
            private final VersionRef ref;
            private final VersionControlSpec spec;
            File workingDir;

            DelegatePopulator(VersionControlSystem delegate, File versionDir, VersionRef ref, VersionControlSpec spec) {
                this.delegate = delegate;
                this.ref = ref;
                this.versionDir = versionDir;
                this.spec = spec;
            }

            @Override
            public void run() {
                workingDir = delegate.populate(versionDir, ref, spec);
            }
        }
    }
}

@@ -767,16 +767,52 @@
 tCache;%0A
+import org.gradle.internal.Factory;%0A
 import o
@@ -984,16 +984,50 @@
 ystem;%0A%0A
+import javax.annotation.Nullable;%0A
 import j
@@ -2725,106 +2725,8 @@
 ();%0A
-            DelegatePopulator populator = new DelegatePopulator(delegate, versionDir, ref, spec);%0A
     
@@ -2751,24 +2751,31 @@
             
+return 
 cache.useCac
@@ -2781,129 +2781,29 @@
 che(
-populator);%0A            %7D finally %7B%0A                cache.close();%0A            %7D%0A            return populator.workingDir;
+new Factory%3CFile%3E() %7B
 %0A   
@@ -2807,27 +2807,24 @@
         
-%7D%0A%0A
         
 private 
@@ -2819,74 +2819,21 @@
     
-private static final class DelegatePopulator implements Runn
+    @Null
 able
- %7B
 %0A   
@@ -2845,631 +2845,236 @@
     
-private final VersionControlSystem delegate;%0A            private final File versionDir;%0A            private final VersionRef ref;%0A            private final VersionControlSpec spec;%0A            File workingDir;%0A%0A            DelegatePopulator(VersionControlSystem delegate, File versionDir, VersionRef ref, VersionControlSpec spec) %7B%0A                this.delegate = delegate;%0A                this.ref = ref;%0A                this.versionDir = versionDir;%0A                this.spec = spec;%0A            %7D%0A%0A            @Override%0A            public void run() %7B%0A                workingDir = delegate.populate(versionDir, ref, spec
+        @Override%0A                    public File create() %7B%0A                        return delegate.populate(versionDir, ref, spec);%0A                    %7D%0A                %7D);%0A            %7D finally %7B%0A                cache.close(
 );%0A 

Make the populator a Factory<File>