/*
 * Copyright 2018 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.fingerprint.impl;

import com.google.common.collect.ImmutableMap;
import org.gradle.internal.file.FileType;
import org.gradle.internal.fingerprint.FileSystemLocationFingerprint;
import org.gradle.internal.fingerprint.FingerprintHashingStrategy;
import org.gradle.internal.snapshot.CompleteFileSystemLocationSnapshot;
import org.gradle.internal.snapshot.FileSystemSnapshot;
import org.gradle.internal.snapshot.FileSystemSnapshotHierarchyVisitor;
import org.gradle.internal.snapshot.SnapshotVisitResult;

import java.util.HashSet;
import java.util.Map;

/**
 * Fingerprint files normalizing the path to the file name.
 *
 * File names for root directories are ignored.
 */
public class NameOnlyFingerprintingStrategy extends AbstractFingerprintingStrategy {

    public static final NameOnlyFingerprintingStrategy INSTANCE = new NameOnlyFingerprintingStrategy();
    public static final String IDENTIFIER = "NAME_ONLY";

    private NameOnlyFingerprintingStrategy() {
        super(IDENTIFIER);
    }

    @Override
    public String normalizePath(CompleteFileSystemLocationSnapshot snapshot) {
        return snapshot.getName();
    }

    @Override
    public Map<String, FileSystemLocationFingerprint> collectFingerprints(Iterable<? extends FileSystemSnapshot> roots) {
        ImmutableMap.Builder<String, FileSystemLocationFingerprint> builder = ImmutableMap.builder();
        HashSet<String> processedEntries = new HashSet<>();
        for (FileSystemSnapshot root : roots) {
            // TODO Use a SnapshotVisitor
            root.accept(new FileSystemSnapshotHierarchyVisitor() {
                private boolean seenRoot;

                @Override
                public SnapshotVisitResult visitEntry(CompleteFileSystemLocationSnapshot snapshot) {
                    String absolutePath = snapshot.getAbsolutePath();
                    if (processedEntries.add(absolutePath)) {
                        FileSystemLocationFingerprint fingerprint;
                        if (!seenRoot && snapshot.getType() == FileType.Directory) {
                            fingerprint = IgnoredPathFileSystemLocationFingerprint.DIRECTORY;
                            seenRoot = true;
                        } else {
                            fingerprint = new DefaultFileSystemLocationFingerprint(snapshot.getName(), snapshot);
                        }
                        builder.put(absolutePath, fingerprint);
                    }
                    return SnapshotVisitResult.CONTINUE;
                }
            });
        }
        return builder.build();
    }

    @Override
    public FingerprintHashingStrategy getHashingStrategy() {
        return FingerprintHashingStrategy.SORT;
    }
}

@@ -1039,32 +1039,44 @@
 ternal.snapshot.
+RootTracking
 FileSystemSnapsh
@@ -2147,272 +2147,113 @@
     
-// TODO Use a SnapshotVisitor%0A            root.accept(new FileSystemSnapshotHierarchyVisitor() %7B%0A                private boolean seenRoot;%0A%0A                @Override%0A                public SnapshotVisitResult visitEntry(CompleteFileSystemLocationSnapshot snapshot)
+root.accept(RootTrackingFileSystemSnapshotHierarchyVisitor.asSimpleHierarchyVisitor((snapshot, isRoot) -%3E
  %7B%0A 
@@ -2267,20 +2267,16 @@
         
-    
 String a
@@ -2329,28 +2329,24 @@
             
-   
- 
 if (processe
@@ -2395,20 +2395,16 @@
         
-    
 FileSyst
@@ -2440,43 +2440,13 @@
 rint
-;%0A                        if (!seen
+ = is
 Root
@@ -2489,19 +2489,16 @@
 irectory
-) %7B
 %0A       
@@ -2514,33 +2514,17 @@
         
-    fingerprint =
+?
  Ignored
@@ -2566,21 +2566,17 @@
 IRECTORY
-;%0A   
+%0A
         
@@ -2595,100 +2595,9 @@
     
- seenRoot = true;%0A                        %7D else %7B%0A                            fingerprint =
+:
  new
@@ -2669,38 +2669,8 @@
 t);%0A
-                        %7D%0A    
     
@@ -2737,34 +2737,26 @@
             
-    %7D%0A    
+%7D%0A
             
@@ -2812,27 +2812,10 @@
     
-    %7D%0A            
 %7D
+)
 );%0A 

Use root tracking visitor in NameOnlyFingerprintingStrategy