/*
 * 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.changedetection.state;

import com.google.common.collect.ImmutableList;
import org.gradle.api.file.FileVisitDetails;
import org.gradle.api.file.FileVisitor;
import org.gradle.api.internal.file.FileTreeInternal;
import org.gradle.api.internal.file.collections.DirectoryFileTree;
import org.gradle.api.internal.file.collections.FileTreeAdapter;
import org.gradle.api.tasks.util.PatternSet;

import java.util.Collection;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

// Visits a FileTreeInternal for snapshotting, caches some directory scans
public class TreeSnapshotter {
    private ConcurrentMap<String, Collection<? extends FileVisitDetails>> cachedTrees = new ConcurrentHashMap<String, Collection<? extends FileVisitDetails>>();

    public Collection<? extends FileVisitDetails> visitTreeForSnapshotting(FileTreeInternal fileTree, boolean allowReuse) {
        if (isDirectoryFileTree(fileTree)) {
            DirectoryFileTree directoryFileTree = DirectoryFileTree.class.cast(((FileTreeAdapter) fileTree).getTree());
            if (isEligibleForCaching(directoryFileTree)) {
                final String absolutePath = directoryFileTree.getDir().getAbsolutePath();
                Collection<? extends FileVisitDetails> cachedTree = allowReuse ? cachedTrees.get(absolutePath) : null;
                if (cachedTree != null) {
                    return cachedTree;
                } else {
                    cachedTree = doVisitTree(fileTree);
                    cachedTrees.put(absolutePath, cachedTree);
                    return cachedTree;
                }
            }
        }
        return doVisitTree(fileTree);
    }

    private boolean isEligibleForCaching(DirectoryFileTree directoryFileTree) {
        return isEmptyPattern(directoryFileTree.getPatterns());
    }

    private boolean isEmptyPattern(PatternSet patterns) {
        return patterns.getExcludes().isEmpty() && patterns.getIncludes().isEmpty() && patterns.getExcludeSpecs().isEmpty() && patterns.getIncludeSpecs().isEmpty();
    }

    private boolean isDirectoryFileTree(FileTreeInternal fileTree) {
        return fileTree instanceof FileTreeAdapter && ((FileTreeAdapter) fileTree).getTree() instanceof DirectoryFileTree;
    }

    private Collection<? extends FileVisitDetails> doVisitTree(FileTreeInternal fileTree) {
        final ImmutableList.Builder<FileVisitDetails> fileVisitDetails = ImmutableList.builder();
        fileTree.visitTreeOrBackingFile(new FileVisitor() {
            @Override
            public void visitDir(FileVisitDetails dirDetails) {
                fileVisitDetails.add(dirDetails);
            }

            @Override
            public void visitFile(FileVisitDetails fileDetails) {
                fileVisitDetails.add(fileDetails);
            }
        });
        return fileVisitDetails.build();
    }

    public void clearCache() {
        cachedTrees.clear();
    }
}

@@ -714,16 +714,59 @@
 leList;%0A
+import com.google.common.collect.MapMaker;%0A
 import o
@@ -1107,55 +1107,8 @@
 on;%0A
-import java.util.concurrent.ConcurrentHashMap;%0A
 impo
@@ -1349,73 +1349,39 @@
 new 
-ConcurrentHashMap%3CString, Collection%3C? extends FileVisitDetails%3E%3E
+MapMaker().weakValues().makeMap
 ();%0A

Use weak references to values in cache