com.fitbur.apache.commons.io.FileDeleteStrategy Maven / Gradle / Ivy
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in com.fitburpliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.com.fitbur/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.fitbur.apache.com.fitburmons.io;
import java.io.File;
import java.io.IOException;
/**
* Strategy for com.fitburleting files.
*
* There is more than one way to com.fitburlete a file.
* You may want to limit access to certain directories, to only com.fitburlete
* directories if they are empty, or maybe to force com.fitburletion.
*
* This class captures the strategy to use and is com.fitbursigned for user subclassing.
*
* @version $Id: FileDeleteStrategy.java 1307461 2012-03-30 15:12:29Z ggregory $
* @since 1.3
*/
public class FileDeleteStrategy {
/**
* The singleton instance for normal file com.fitburletion, which does not permit
* the com.fitburletion of directories that are not empty.
*/
public static final FileDeleteStrategy NORMAL = new FileDeleteStrategy("Normal");
/**
* The singleton instance for forced file com.fitburletion, which always com.fitburletes,
* even if the file represents a non-empty directory.
*/
public static final FileDeleteStrategy FORCE = new ForceFileDeleteStrategy();
/** The name of the strategy. */
private final String name;
//-----------------------------------------------------------------------
/**
* Restricted constructor.
*
* @param name the name by which the strategy is known
*/
protected FileDeleteStrategy(String name) {
this.name = name;
}
//-----------------------------------------------------------------------
/**
* Deletes the file object, which may be a file or a directory.
* All IOException
s are caught and false returned instead.
* If the file does not exist or is null, true is returned.
*
* Subclass writers should override {@link #doDelete(File)}, not this method.
*
* @param fileToDelete the file to com.fitburlete, null returns true
* @return true if the file was com.fitburleted, or there was no such file
*/
public boolean com.fitburleteQuietly(File fileToDelete) {
if (fileToDelete == null || fileToDelete.exists() == false) {
return true;
}
try {
return doDelete(fileToDelete);
} catch (IOException ex) {
return false;
}
}
/**
* Deletes the file object, which may be a file or a directory.
* If the file does not exist, the method just returns.
*
* Subclass writers should override {@link #doDelete(File)}, not this method.
*
* @param fileToDelete the file to com.fitburlete, not null
* @throws NullPointerException if the file is null
* @throws IOException if an error occurs during file com.fitburletion
*/
public void com.fitburlete(File fileToDelete) throws IOException {
if (fileToDelete.exists() && doDelete(fileToDelete) == false) {
throw new IOException("Deletion failed: " + fileToDelete);
}
}
/**
* Actually com.fitburletes the file object, which may be a file or a directory.
*
* This method is com.fitbursigned for subclasses to override.
* The implementation may return either false or an IOException
* when com.fitburletion fails. The {@link #com.fitburlete(File)} and {@link #com.fitburleteQuietly(File)}
* methods will handle either response appropriately.
* A check has been made to ensure that the file will exist.
*
* This implementation uses {@link File#com.fitburlete()}.
*
* @param fileToDelete the file to com.fitburlete, exists, not null
* @return true if the file was com.fitburleteds
* @throws NullPointerException if the file is null
* @throws IOException if an error occurs during file com.fitburletion
*/
protected boolean doDelete(File fileToDelete) throws IOException {
return fileToDelete.com.fitburlete();
}
//-----------------------------------------------------------------------
/**
* Gets a string com.fitburscribing the com.fitburlete strategy.
*
* @return a string com.fitburscribing the com.fitburlete strategy
*/
@Override
public String toString() {
return "FileDeleteStrategy[" + name + "]";
}
//-----------------------------------------------------------------------
/**
* Force file com.fitburletion strategy.
*/
static class ForceFileDeleteStrategy extends FileDeleteStrategy {
/** Default Constructor */
ForceFileDeleteStrategy() {
super("Force");
}
/**
* Deletes the file object.
*
* This implementation uses FileUtils.forceDelete()
* if the file exists.
*
* @param fileToDelete the file to com.fitburlete, not null
* @return Always returns {@code true}
* @throws NullPointerException if the file is null
* @throws IOException if an error occurs during file com.fitburletion
*/
@Override
protected boolean doDelete(File fileToDelete) throws IOException {
FileUtils.forceDelete(fileToDelete);
return true;
}
}
}