<commit_before>package org.smoothbuild.fs.mem;

import static org.smoothbuild.fs.base.PathState.DIR;
import static org.smoothbuild.fs.base.PathState.FILE;
import static org.smoothbuild.fs.base.PathState.NOTHING;
import static org.smoothbuild.fs.base.RecursiveFilesIterable.recursiveFilesIterable;

import java.io.InputStream;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.List;

import org.smoothbuild.fs.base.FileSystem;
import org.smoothbuild.fs.base.Path;
import org.smoothbuild.fs.base.PathState;
import org.smoothbuild.fs.base.exc.FileSystemException;
import org.smoothbuild.fs.base.exc.NoSuchDirException;
import org.smoothbuild.fs.base.exc.NoSuchFileException;

/**
 * In memory implementation of FileSystem.
 */
public class MemoryFileSystem implements FileSystem {
  private final MemoryDirectory root = new MemoryDirectory(null, "");

  @Override
  public Path root() {
    return Path.rootPath();
  }

  @Override
  public PathState pathState(Path path) {
    MemoryElement element = findElement(path);
    if (element == null) {
      return NOTHING;
    }
    if (element.isDirectory()) {
      return DIR;
    }
    return FILE;
  }

  @Override
  public List<String> childNames(Path directory) {
    return getDirectory(directory).childNames();
  }

  @Override
  public Iterable<Path> filesFrom(Path directory) {
    return recursiveFilesIterable(this, directory);
  }

  private MemoryDirectory createDirectory(Path directory) {
    Iterator<Path> it = directory.parts().iterator();
    MemoryDirectory currentDir = root;
    while (it.hasNext()) {
      String name = it.next().value();
      if (currentDir.hasChild(name)) {
        MemoryElement child = currentDir.child(name);
        if (child.isDirectory()) {
          currentDir = (MemoryDirectory) child;
        } else {
          throw new FileSystemException("Path (or subpath) of to be created directory ("
              + directory + ") is taken by some file.");
        }
      } else {
        MemoryDirectory newDir = new MemoryDirectory(currentDir, name);
        currentDir.addChild(newDir);
        currentDir = newDir;
      }
    }
    return currentDir;
  }

  @Override
  public void deleteDirectoryRecursively(Path directory) {
    MemoryElement element = getDirectory(directory);
    if (element.isDirectory()) {
      MemoryDirectory parent = element.parent();
      if (parent == null) {
        throw new IllegalArgumentException("Cannot delete root directory.");
      } else {
        parent.removeChild(element);
      }
    } else {
      throw new NoSuchDirException(directory);
    }
  }

  @Override
  public InputStream openInputStream(Path path) {
    MemoryElement element = getFile(path);
    if (element.isFile()) {
      return element.createInputStream();
    } else {
      throw new FileSystemException("Cannot read from file '" + path + "' as it is directory.");
    }
  }

  @Override
  public OutputStream openOutputStream(Path path) {
    if (path.isRoot()) {
      throw new FileSystemException("Cannot open file '" + path + "' as it is directory.");
    }
    MemoryDirectory dir = createDirectory(path.parent());

    String name = path.lastPart().value();
    if (dir.hasChild(name)) {
      MemoryElement child = dir.child(name);
      if (child.isFile()) {
        return child.createOutputStream();
      } else {
        throw new FileSystemException("Cannot open file '" + path + "' as it is directory.");
      }
    }

    MemoryFile child = new MemoryFile(dir, name);
    dir.addChild(child);
    return child.createOutputStream();
  }

  private MemoryElement getFile(Path path) {
    MemoryElement found = findElement(path);
    if (found == null) {
      throw new NoSuchFileException(path);
    } else {
      return found;
    }
  }

  private MemoryElement getDirectory(Path path) {
    MemoryElement found = findElement(path);
    if (found == null) {
      throw new NoSuchDirException(path);
    } else {
      return found;
    }
  }

  private MemoryElement findElement(Path path) {
    Iterator<Path> it = path.parts().iterator();
    MemoryElement current = root;
    while (it.hasNext()) {
      String name = it.next().value();
      if (current.hasChild(name)) {
        current = current.child(name);
      } else {
        return null;
      }
    }
    return current;
  }
}
<commit_msg>fixed exception messages in MemoryFileSystem<commit_after>package org.smoothbuild.fs.mem;

import static org.smoothbuild.fs.base.PathState.DIR;
import static org.smoothbuild.fs.base.PathState.FILE;
import static org.smoothbuild.fs.base.PathState.NOTHING;
import static org.smoothbuild.fs.base.RecursiveFilesIterable.recursiveFilesIterable;

import java.io.InputStream;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.List;

import org.smoothbuild.fs.base.FileSystem;
import org.smoothbuild.fs.base.Path;
import org.smoothbuild.fs.base.PathState;
import org.smoothbuild.fs.base.exc.FileSystemException;
import org.smoothbuild.fs.base.exc.NoSuchDirException;
import org.smoothbuild.fs.base.exc.NoSuchFileException;

/**
 * In memory implementation of FileSystem.
 */
public class MemoryFileSystem implements FileSystem {
  private final MemoryDirectory root = new MemoryDirectory(null, "");

  @Override
  public Path root() {
    return Path.rootPath();
  }

  @Override
  public PathState pathState(Path path) {
    MemoryElement element = findElement(path);
    if (element == null) {
      return NOTHING;
    }
    if (element.isDirectory()) {
      return DIR;
    }
    return FILE;
  }

  @Override
  public List<String> childNames(Path directory) {
    return getDirectory(directory).childNames();
  }

  @Override
  public Iterable<Path> filesFrom(Path directory) {
    return recursiveFilesIterable(this, directory);
  }

  private MemoryDirectory createDirectory(Path directory) {
    Iterator<Path> it = directory.parts().iterator();
    MemoryDirectory currentDir = root;
    while (it.hasNext()) {
      String name = it.next().value();
      if (currentDir.hasChild(name)) {
        MemoryElement child = currentDir.child(name);
        if (child.isDirectory()) {
          currentDir = (MemoryDirectory) child;
        } else {
          throw new FileSystemException("Path (or subpath) of to be created directory ("
              + directory + ") is taken by some file.");
        }
      } else {
        MemoryDirectory newDir = new MemoryDirectory(currentDir, name);
        currentDir.addChild(newDir);
        currentDir = newDir;
      }
    }
    return currentDir;
  }

  @Override
  public void deleteDirectoryRecursively(Path directory) {
    MemoryElement element = getDirectory(directory);
    if (element.isDirectory()) {
      MemoryDirectory parent = element.parent();
      if (parent == null) {
        throw new IllegalArgumentException("Cannot delete root directory.");
      } else {
        parent.removeChild(element);
      }
    } else {
      throw new NoSuchDirException(directory);
    }
  }

  @Override
  public InputStream openInputStream(Path path) {
    MemoryElement element = getFile(path);
    if (element.isFile()) {
      return element.createInputStream();
    } else {
      throw new FileSystemException("Cannot read from file " + path + " as it is directory.");
    }
  }

  @Override
  public OutputStream openOutputStream(Path path) {
    if (path.isRoot()) {
      throw new FileSystemException("Cannot open file " + path + " as it is directory.");
    }
    MemoryDirectory dir = createDirectory(path.parent());

    String name = path.lastPart().value();
    if (dir.hasChild(name)) {
      MemoryElement child = dir.child(name);
      if (child.isFile()) {
        return child.createOutputStream();
      } else {
        throw new FileSystemException("Cannot open file " + path + " as it is directory.");
      }
    }

    MemoryFile child = new MemoryFile(dir, name);
    dir.addChild(child);
    return child.createOutputStream();
  }

  private MemoryElement getFile(Path path) {
    MemoryElement found = findElement(path);
    if (found == null) {
      throw new NoSuchFileException(path);
    } else {
      return found;
    }
  }

  private MemoryElement getDirectory(Path path) {
    MemoryElement found = findElement(path);
    if (found == null) {
      throw new NoSuchDirException(path);
    } else {
      return found;
    }
  }

  private MemoryElement findElement(Path path) {
    Iterator<Path> it = path.parts().iterator();
    MemoryElement current = root;
    while (it.hasNext()) {
      String name = it.next().value();
      if (current.hasChild(name)) {
        current = current.child(name);
      } else {
        return null;
      }
    }
    return current;
  }
}

{'fraction_non_alphanumeric': 0.11089516405624786, 'fraction_numerical': 0.0, 'mean_word_length': 3.0821278581427904, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '5007698', 'n_tokens_mistral': 2493, 'n_tokens_neox': 2438, 'n_words': 608}