PathUtils.java

  1. package com.soebes.itf.jupiter.extension;

  2. /*
  3.  * Licensed to the Apache Software Foundation (ASF) under one
  4.  * or more contributor license agreements.  See the NOTICE file
  5.  * distributed with this work for additional information
  6.  * regarding copyright ownership.  The ASF licenses this file
  7.  * to you under the Apache License, Version 2.0 (the
  8.  * "License"); you may not use this file except in compliance
  9.  * with the License.  You may obtain a copy of the License at
  10.  *
  11.  *  http://www.apache.org/licenses/LICENSE-2.0
  12.  *
  13.  * Unless required by applicable law or agreed to in writing,
  14.  * software distributed under the License is distributed on an
  15.  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16.  * KIND, either express or implied.  See the License for the
  17.  * specific language governing permissions and limitations
  18.  * under the License.
  19.  */

  20. import com.soebes.itf.jupiter.extension.exceptions.PathUtilException;
  21. import org.apiguardian.api.API;

  22. import java.io.IOException;
  23. import java.nio.file.Files;
  24. import java.nio.file.Path;
  25. import java.nio.file.StandardCopyOption;
  26. import java.util.Collections;
  27. import java.util.List;
  28. import java.util.function.Consumer;
  29. import java.util.function.Predicate;
  30. import java.util.stream.Collectors;
  31. import java.util.stream.Stream;

  32. import static org.apiguardian.api.API.Status.INTERNAL;

  33. /**
  34.  * @author Karl Heinz Marbaise
  35.  */
  36. @API(status = INTERNAL, since = "0.12.0")
  37. public class PathUtils {

  38.   private PathUtils() {
  39.   }

  40.   public static Path copy(Path source, Path target) {
  41.     try {
  42.       return Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING);
  43.     } catch (IOException e) {
  44.       throw new PathUtilException(e);
  45.     }
  46.   }

  47.   public static Consumer<Path> deletePath = toDelete -> {
  48.     try {
  49.       Files.delete(toDelete);
  50.     } catch (IOException e) {
  51.       throw new PathUtilException(e);
  52.     }
  53.   };

  54.   public static Path createDirectory(Path directoryToCreate) {
  55.     try {
  56.       return Files.createDirectory(directoryToCreate);
  57.     } catch (IOException e) {
  58.       throw new PathUtilException(e);
  59.     }
  60.   }

  61.   public static Stream<Path> list(Path toList) {
  62.     try {
  63.       return Files.list(toList);
  64.     } catch (IOException e) {
  65.       throw new PathUtilException(e);
  66.     }
  67.   }

  68.   public static Stream<Path> walk(Path toWalk) {
  69.     try {
  70.       return Files.walk(toWalk);
  71.     } catch (IOException e) {
  72.       throw new PathUtilException(e);
  73.     }
  74.   }

  75.   private static final Predicate<Path> FILE = Files::isRegularFile;

  76.   private static final Predicate<Path> DIRECTORY = Files::isDirectory;

  77.   private static final Predicate<Path> FILE_OR_DIRECTORY = s -> FILE.or(DIRECTORY).test(s);

  78.   public static void deleteRecursively(Path target) {
  79.     if (!Files.exists(target)) {
  80.       return;
  81.     }

  82.     try (Stream<Path> walk = walk(target)) {
  83.       List<Path> allElements = walk.filter(FILE_OR_DIRECTORY).collect(Collectors.toList());

  84.       Collections.reverse(allElements);

  85.       allElements.stream().filter(Files::exists).forEachOrdered(deletePath);
  86.     }
  87.   }

  88.   public static void copyDirectoryRecursively(Path source, Path target) {
  89.     if (!Files.isDirectory(source)) {
  90.       copy(source, target);
  91.       return;
  92.     }

  93.     if (Files.notExists(target)) {
  94.       createDirectory(target);
  95.     }

  96.     try (Stream<Path> paths = list(source)) {
  97.       paths.forEachOrdered(s -> copyDirectoryRecursively(s, target.resolve(source.relativize(s))));
  98.     }
  99.   }
  100. }