| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| FileDeleteStrategy |
|
| 2.142857142857143;2.143 | ||||
| FileDeleteStrategy$ForceFileDeleteStrategy |
|
| 2.142857142857143;2.143 |
| 1 | /* | |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one or more | |
| 3 | * contributor license agreements. See the NOTICE file distributed with | |
| 4 | * this work for additional information regarding copyright ownership. | |
| 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 | |
| 6 | * (the "License"); you may not use this file except in compliance with | |
| 7 | * the License. You may obtain a copy of the License at | |
| 8 | * | |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 | |
| 10 | * | |
| 11 | * Unless required by applicable law or agreed to in writing, software | |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, | |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 14 | * See the License for the specific language governing permissions and | |
| 15 | * limitations under the License. | |
| 16 | */ | |
| 17 | package org.apache.commons.io; | |
| 18 | ||
| 19 | import java.io.File; | |
| 20 | import java.io.IOException; | |
| 21 | ||
| 22 | /** | |
| 23 | * Strategy for deleting files. | |
| 24 | * <p> | |
| 25 | * There is more than one way to delete a file. | |
| 26 | * You may want to limit access to certain directories, to only delete | |
| 27 | * directories if they are empty, or maybe to force deletion. | |
| 28 | * <p> | |
| 29 | * This class captures the strategy to use and is designed for user subclassing. | |
| 30 | * | |
| 31 | * @version $Id: FileDeleteStrategy.java 1415850 2012-11-30 20:51:39Z ggregory $ | |
| 32 | * @since 1.3 | |
| 33 | */ | |
| 34 | public class FileDeleteStrategy { | |
| 35 | ||
| 36 | /** | |
| 37 | * The singleton instance for normal file deletion, which does not permit | |
| 38 | * the deletion of directories that are not empty. | |
| 39 | */ | |
| 40 | 3 | public static final FileDeleteStrategy NORMAL = new FileDeleteStrategy("Normal"); |
| 41 | /** | |
| 42 | * The singleton instance for forced file deletion, which always deletes, | |
| 43 | * even if the file represents a non-empty directory. | |
| 44 | */ | |
| 45 | 3 | public static final FileDeleteStrategy FORCE = new ForceFileDeleteStrategy(); |
| 46 | ||
| 47 | /** The name of the strategy. */ | |
| 48 | private final String name; | |
| 49 | ||
| 50 | //----------------------------------------------------------------------- | |
| 51 | /** | |
| 52 | * Restricted constructor. | |
| 53 | * | |
| 54 | * @param name the name by which the strategy is known | |
| 55 | */ | |
| 56 | 6 | protected FileDeleteStrategy(final String name) { |
| 57 | 6 | this.name = name; |
| 58 | 6 | } |
| 59 | ||
| 60 | //----------------------------------------------------------------------- | |
| 61 | /** | |
| 62 | * Deletes the file object, which may be a file or a directory. | |
| 63 | * All <code>IOException</code>s are caught and false returned instead. | |
| 64 | * If the file does not exist or is null, true is returned. | |
| 65 | * <p> | |
| 66 | * Subclass writers should override {@link #doDelete(File)}, not this method. | |
| 67 | * | |
| 68 | * @param fileToDelete the file to delete, null returns true | |
| 69 | * @return true if the file was deleted, or there was no such file | |
| 70 | */ | |
| 71 | public boolean deleteQuietly(final File fileToDelete) { | |
| 72 | 17 | if (fileToDelete == null || fileToDelete.exists() == false) { |
| 73 | 2 | return true; |
| 74 | } | |
| 75 | try { | |
| 76 | 15 | return doDelete(fileToDelete); |
| 77 | 0 | } catch (final IOException ex) { |
| 78 | 0 | return false; |
| 79 | } | |
| 80 | } | |
| 81 | ||
| 82 | /** | |
| 83 | * Deletes the file object, which may be a file or a directory. | |
| 84 | * If the file does not exist, the method just returns. | |
| 85 | * <p> | |
| 86 | * Subclass writers should override {@link #doDelete(File)}, not this method. | |
| 87 | * | |
| 88 | * @param fileToDelete the file to delete, not null | |
| 89 | * @throws NullPointerException if the file is null | |
| 90 | * @throws IOException if an error occurs during file deletion | |
| 91 | */ | |
| 92 | public void delete(final File fileToDelete) throws IOException { | |
| 93 | 7 | if (fileToDelete.exists() && doDelete(fileToDelete) == false) { |
| 94 | 1 | throw new IOException("Deletion failed: " + fileToDelete); |
| 95 | } | |
| 96 | 5 | } |
| 97 | ||
| 98 | /** | |
| 99 | * Actually deletes the file object, which may be a file or a directory. | |
| 100 | * <p> | |
| 101 | * This method is designed for subclasses to override. | |
| 102 | * The implementation may return either false or an <code>IOException</code> | |
| 103 | * when deletion fails. The {@link #delete(File)} and {@link #deleteQuietly(File)} | |
| 104 | * methods will handle either response appropriately. | |
| 105 | * A check has been made to ensure that the file will exist. | |
| 106 | * <p> | |
| 107 | * This implementation uses {@link File#delete()}. | |
| 108 | * | |
| 109 | * @param fileToDelete the file to delete, exists, not null | |
| 110 | * @return true if the file was deleteds | |
| 111 | * @throws NullPointerException if the file is null | |
| 112 | * @throws IOException if an error occurs during file deletion | |
| 113 | */ | |
| 114 | protected boolean doDelete(final File fileToDelete) throws IOException { | |
| 115 | 16 | return fileToDelete.delete(); |
| 116 | } | |
| 117 | ||
| 118 | //----------------------------------------------------------------------- | |
| 119 | /** | |
| 120 | * Gets a string describing the delete strategy. | |
| 121 | * | |
| 122 | * @return a string describing the delete strategy | |
| 123 | */ | |
| 124 | @Override | |
| 125 | public String toString() { | |
| 126 | 2 | return "FileDeleteStrategy[" + name + "]"; |
| 127 | } | |
| 128 | ||
| 129 | //----------------------------------------------------------------------- | |
| 130 | /** | |
| 131 | * Force file deletion strategy. | |
| 132 | */ | |
| 133 | static class ForceFileDeleteStrategy extends FileDeleteStrategy { | |
| 134 | /** Default Constructor */ | |
| 135 | ForceFileDeleteStrategy() { | |
| 136 | 3 | super("Force"); |
| 137 | 3 | } |
| 138 | ||
| 139 | /** | |
| 140 | * Deletes the file object. | |
| 141 | * <p> | |
| 142 | * This implementation uses <code>FileUtils.forceDelete() <code> | |
| 143 | * if the file exists. | |
| 144 | * | |
| 145 | * @param fileToDelete the file to delete, not null | |
| 146 | * @return Always returns {@code true} | |
| 147 | * @throws NullPointerException if the file is null | |
| 148 | * @throws IOException if an error occurs during file deletion | |
| 149 | */ | |
| 150 | @Override | |
| 151 | protected boolean doDelete(final File fileToDelete) throws IOException { | |
| 152 | 3 | FileUtils.forceDelete(fileToDelete); |
| 153 | 3 | return true; |
| 154 | } | |
| 155 | } | |
| 156 | ||
| 157 | } |