001/*
002 * PlotSquared, a land and world management plugin for Minecraft.
003 * Copyright (C) IntellectualSites <https://intellectualsites.com>
004 * Copyright (C) IntellectualSites team and contributors
005 *
006 * This program is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 *
011 * This program is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014 * GNU General Public License for more details.
015 *
016 * You should have received a copy of the GNU General Public License
017 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
018 */
019package com.plotsquared.core.util;
020
021import org.jetbrains.annotations.NotNull;
022import org.jetbrains.annotations.Nullable;
023
024import java.io.IOException;
025import java.nio.file.FileVisitResult;
026import java.nio.file.Files;
027import java.nio.file.Path;
028import java.nio.file.SimpleFileVisitor;
029import java.nio.file.attribute.BasicFileAttributes;
030
031/**
032 * A {@link SimpleFileVisitor} that recursively deletes a directory tree.
033 */
034public class RecursiveDirectoryRemovalWalker extends SimpleFileVisitor<Path> {
035
036    public static final RecursiveDirectoryRemovalWalker INSTANCE = new RecursiveDirectoryRemovalWalker();
037
038    private RecursiveDirectoryRemovalWalker() {
039    }
040
041    @Override
042    public @NotNull FileVisitResult postVisitDirectory(@NotNull final Path dir, @Nullable final IOException exc) throws
043            IOException {
044        Files.delete(dir);
045        return FileVisitResult.CONTINUE;
046    }
047
048    @Override
049    public @NotNull FileVisitResult visitFile(@NotNull final Path file, @NotNull final BasicFileAttributes attrs) throws
050            IOException {
051        Files.delete(file);
052        return FileVisitResult.CONTINUE;
053    }
054
055}