All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.assertj.snapshot.internal.utils.FileUtilsImpl Maven / Gradle / Ivy

The newest version!
package org.assertj.snapshot.internal.utils;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Optional;
import java.util.logging.Logger;

public class FileUtilsImpl implements FileUtils {

  private static Logger LOG = Logger.getLogger(FileUtilsImpl.class.getName());
  private static FileUtils FILE_UTILS = new FileUtilsImpl();

  public FileUtilsImpl() {}

  @SuppressFBWarnings
  public static FileUtils create() {
    return FILE_UTILS;
  }

  /** Visible for test */
  @SuppressFBWarnings
  public static void setFileUtils(final FileUtils fileUtils) {
    LOG.info(FILE_UTILS.toString());
    FILE_UTILS = fileUtils;
  }

  @Override
  public Optional findFileContent(final Path path) {
    if (this.pathExists(path)) {
      try {
        return Optional.of(Files.readString(path));
      } catch (final IOException e) {
        throw new RuntimeException(e);
      }
    }
    return Optional.empty();
  }

  @Override
  public String getFileContent(final Path path) {
    final Optional opt = this.findFileContent(path);
    if (opt.isPresent()) {
      return opt.get();
    }
    throw new RuntimeException(path + " not found");
  }

  @Override
  public void writeFileContent(final Path sourceFile, final String manipulatedSourceFileContent) {
    try {
      Files.writeString(sourceFile, manipulatedSourceFileContent);
    } catch (final IOException e) {
      throw new RuntimeException(sourceFile.toString(), e);
    }
  }

  @Override
  public void createDirs(final Path path) {
    if (!this.pathExists(path)) {
      try {
        Files.createDirectories(path);
      } catch (final IOException e) {
        throw new RuntimeException(e);
      }
    }
  }

  @Override
  public boolean pathExists(final Path path) {
    return Files.exists(path);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy