<commit_before>package com.loopperfect.buckaroo.io;

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.jimfs.Configuration;
import com.google.common.jimfs.Jimfs;
import com.loopperfect.buckaroo.Either;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.nio.file.*;
import java.util.Arrays;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * Created by gaetano on 21/02/17.
 */

@FunctionalInterface
public interface FSContext {

    FileSystem getFS();

    default Path getUserHomeDirectory() {
        return getFS().getPath(System.getProperty("user.home"));
    }

    default Path getWorkingDirectory() {
        return getFS().getPath(System.getProperty("user.dir"));
    }

    default Path getPath(String... path) {
        String[] paths = Arrays
            .stream(path)
            .toArray(size -> new String[size]);
        return getFS().getPath("", paths);
    }

    default boolean isFile(final Path path) {
        return Files.isRegularFile(path);
    }

    default boolean exists(final Path path) {
        Preconditions.checkNotNull(path);
        return Files.exists(path);
    }

    default Optional<IOException> createDirectory(final Path p) {
        Preconditions.checkNotNull(p);
        final Path path = getFS().getPath(p.toString());
        try {
            Files.createDirectories(path);
        } catch (IOException e) {
            return Optional.of(e);
        }
        if (!Files.isDirectory(path) || !Files.exists(path)) {
            return Optional.of(new IOException("Could not create a directory at " + path));
        }
        return Optional.empty();
    }

    default Either<IOException, String> readFile(final Path p) {
        Preconditions.checkNotNull(p);
        final Path path = getFS().getPath(p.toString());
        try {
            final String content = Files.readAllLines(getFS().getPath(path.toString()), Charset.defaultCharset())
                .stream()
                .collect(Collectors.joining("\n"));
            return Either.right(content);
        } catch (final IOException e) {
            return Either.left(e);
        }
    }

    default Optional<IOException> writeFile(final Path p, final String content, final boolean overwrite) {
        Preconditions.checkNotNull(p);
        Preconditions.checkNotNull(content);
        final Path path = getFS().getPath(p.toString());
        try {
            if (!overwrite && Files.exists(path)) {
                throw new IOException("There is already a file at " + path);
            }
            Files.write(path, ImmutableList.of(content), Charset.defaultCharset());
            return Optional.empty();
        } catch (final IOException e) {
            return Optional.of(e);
        }
    }

    default Optional<IOException> writeFile(final Path path, final String content) {
        return writeFile(path, content, false);
    }

    default Either<IOException, ImmutableList<Path>> listFiles(final Path p) {
        Preconditions.checkNotNull(p);
        final Path path = getFS().getPath(p.toString());
        try (Stream<Path> paths = java.nio.file.Files.walk(path, 1, FileVisitOption.FOLLOW_LINKS)) {
            return Either.right(ImmutableList.copyOf(paths.collect(Collectors.toList())));
        } catch (final IOException e) {
            return Either.left(e);
        }
    }


    static FSContext actual() {
        return create(
            FileSystems.getDefault(),
            System.getProperty("user.home"),
            System.getProperty("user.dir"));
    }

    static FSContext fake() {
        return create(
            Jimfs.newFileSystem(Configuration.unix()),
            System.getProperty("user.home"),
            System.getProperty("user.dir"));
    }

    static FSContext create(final FileSystem fs, final String homeDir, final String workingDir) {
        return new FSContext() {
            @Override
            public FileSystem getFS() {
                return fs;
            }

            @Override
            public Path getUserHomeDirectory() {
                return getFS()
                    .getPath(homeDir);
            }

            @Override
            public Path getWorkingDirectory() {
                return getFS()
                    .getPath(workingDir);
            }
        };
    }
}<commit_msg>removes unneeded imports<commit_after>package com.loopperfect.buckaroo.io;

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.jimfs.Configuration;
import com.google.common.jimfs.Jimfs;
import com.loopperfect.buckaroo.Either;

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.*;
import java.util.Arrays;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * Created by gaetano on 21/02/17.
 */

@FunctionalInterface
public interface FSContext {

    FileSystem getFS();

    default Path getUserHomeDirectory() {
        return getFS().getPath(System.getProperty("user.home"));
    }

    default Path getWorkingDirectory() {
        return getFS().getPath(System.getProperty("user.dir"));
    }

    default Path getPath(String... path) {
        String[] paths = Arrays
            .stream(path)
            .toArray(size -> new String[size]);
        return getFS().getPath("", paths);
    }

    default boolean isFile(final Path path) {
        return Files.isRegularFile(path);
    }

    default boolean exists(final Path path) {
        Preconditions.checkNotNull(path);
        return Files.exists(path);
    }

    default Optional<IOException> createDirectory(final Path p) {
        Preconditions.checkNotNull(p);
        final Path path = getFS().getPath(p.toString());
        try {
            Files.createDirectories(path);
        } catch (IOException e) {
            return Optional.of(e);
        }
        if (!Files.isDirectory(path) || !Files.exists(path)) {
            return Optional.of(new IOException("Could not create a directory at " + path));
        }
        return Optional.empty();
    }

    default Either<IOException, String> readFile(final Path p) {
        Preconditions.checkNotNull(p);
        final Path path = getFS().getPath(p.toString());
        try {
            final String content = Files.readAllLines(getFS().getPath(path.toString()), Charset.defaultCharset())
                .stream()
                .collect(Collectors.joining("\n"));
            return Either.right(content);
        } catch (final IOException e) {
            return Either.left(e);
        }
    }

    default Optional<IOException> writeFile(final Path p, final String content, final boolean overwrite) {
        Preconditions.checkNotNull(p);
        Preconditions.checkNotNull(content);
        final Path path = getFS().getPath(p.toString());
        try {
            if (!overwrite && Files.exists(path)) {
                throw new IOException("There is already a file at " + path);
            }
            Files.write(path, ImmutableList.of(content), Charset.defaultCharset());
            return Optional.empty();
        } catch (final IOException e) {
            return Optional.of(e);
        }
    }

    default Optional<IOException> writeFile(final Path path, final String content) {
        return writeFile(path, content, false);
    }

    default Either<IOException, ImmutableList<Path>> listFiles(final Path p) {
        Preconditions.checkNotNull(p);
        final Path path = getFS().getPath(p.toString());
        try (Stream<Path> paths = java.nio.file.Files.walk(path, 1, FileVisitOption.FOLLOW_LINKS)) {
            return Either.right(ImmutableList.copyOf(paths.collect(Collectors.toList())));
        } catch (final IOException e) {
            return Either.left(e);
        }
    }


    static FSContext actual() {
        return create(
            FileSystems.getDefault(),
            System.getProperty("user.home"),
            System.getProperty("user.dir"));
    }

    static FSContext fake() {
        return create(
            Jimfs.newFileSystem(Configuration.unix()),
            System.getProperty("user.home"),
            System.getProperty("user.dir"));
    }

    static FSContext create(final FileSystem fs, final String homeDir, final String workingDir) {
        return new FSContext() {
            @Override
            public FileSystem getFS() {
                return fs;
            }

            @Override
            public Path getUserHomeDirectory() {
                return getFS()
                    .getPath(homeDir);
            }

            @Override
            public Path getWorkingDirectory() {
                return getFS()
                    .getPath(workingDir);
            }
        };
    }
}
{'fraction_non_alphanumeric': 0.11435252199086961, 'fraction_numerical': 0.001558846453624318, 'mean_word_length': 2.2649945474372957, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3868613', 'n_tokens_mistral': 2443, 'n_tokens_neox': 2368, 'n_words': 532}