/*
 * 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.cache.internal;

import com.google.common.collect.Sets;
import org.apache.commons.io.filefilter.FileFilterUtils;
import org.apache.commons.io.filefilter.RegexFileFilter;
import org.gradle.internal.Try;
import org.gradle.util.GradleVersion;

import java.io.File;
import java.io.FileFilter;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.SortedSet;
import java.util.concurrent.Callable;
import java.util.function.Consumer;

import static org.apache.commons.io.filefilter.FileFilterUtils.directoryFileFilter;

public class VersionSpecificCacheDirectoryScanner {

    private final File baseDir;

    public VersionSpecificCacheDirectoryScanner(File baseDir) {
        this.baseDir = baseDir;
    }

    public File getBaseDir() {
        return baseDir;
    }

    public File getDirectory(GradleVersion gradleVersion) {
        return new File(baseDir, gradleVersion.getVersion());
    }

    public SortedSet<VersionSpecificCacheDirectory> getExistingDirectories() {
        final SortedSet<VersionSpecificCacheDirectory> result = Sets.newTreeSet();
        for (final File subDir : listVersionSpecificCacheDirs()) {
            Try<GradleVersion> version = tryParseGradleVersion(subDir);
            version.ifSuccessful(new Consumer<GradleVersion>() {
                @Override
                public void accept(GradleVersion gradleVersion) {
                    result.add(new VersionSpecificCacheDirectory(subDir, gradleVersion));
                }
            });
        }
        return result;
    }

    private Collection<File> listVersionSpecificCacheDirs() {
        FileFilter combinedFilter = FileFilterUtils.and(directoryFileFilter(), new RegexFileFilter("^\\d.*"));
        File[] result = baseDir.listFiles(combinedFilter);
        return result == null ? Collections.<File>emptySet() : Arrays.asList(result);
    }

    private Try<GradleVersion> tryParseGradleVersion(final File dir) {
        return Try.ofFailable(new Callable<GradleVersion>() {
            @Override
            public GradleVersion call() {
                return GradleVersion.version(dir.getName());
            }
        });
    }
}

@@ -821,60 +821,62 @@
 dle.
-internal.Try;%0Aimport org.gradle.util.GradleVersion;%0A
+util.GradleVersion;%0A%0Aimport javax.annotation.Nullable;
 %0Aimp
@@ -1035,82 +1035,8 @@
 Set;
-%0Aimport java.util.concurrent.Callable;%0Aimport java.util.function.Consumer;
 %0A%0Aim
@@ -1585,22 +1585,16 @@
         
-final 
 SortedSe
@@ -1667,22 +1667,16 @@
    for (
-final 
 File sub
@@ -1727,20 +1727,16 @@
         
-Try%3C
 GradleVe
@@ -1740,17 +1740,16 @@
 eVersion
-%3E
  version
@@ -1798,157 +1798,31 @@
     
-version.ifSuccessful(new Consumer%3CGradleVersion%3E() %7B%0A                @Override%0A                public void accept(GradleVersion gradleVersion) %7B%0A    
+if (version != null) %7B%0A
     
@@ -1882,31 +1882,25 @@
 ory(subDir, 
-gradleV
+v
 ersion));%0A  
@@ -1913,29 +1913,9 @@
     
-    %7D%0A            %7D);
+%7D
 %0A   
@@ -2268,32 +2268,46 @@
 result);%0A    %7D%0A%0A
+    @Nullable%0A
     private Try%3C
@@ -2306,12 +2306,8 @@
 ate 
-Try%3C
 Grad
@@ -2315,17 +2315,16 @@
 eVersion
-%3E
  tryPars
@@ -2338,22 +2338,16 @@
 Version(
-final 
 File dir
@@ -2362,130 +2362,14 @@
     
-return Try.ofFailable(new Callable%3CGradleVersion%3E() %7B%0A            @Override%0A            public GradleVersion call() %7B%0A    
+try %7B%0A
     
@@ -2421,37 +2421,80 @@
 ame());%0A        
-    %7D
+%7D catch (Exception e) %7B%0A            return null;
 %0A        %7D);%0A   
@@ -2487,18 +2487,16 @@
        %7D
-);
 %0A    %7D%0A%7D

Revert "Use Try in VersionSpecificCacheDirectoryScanner"