Coverage Report - org.apache.commons.io.FileDeleteStrategy
 
Classes in this File Line Coverage Branch Coverage Complexity
FileDeleteStrategy
86%
13/15
100%
8/8
2,143
FileDeleteStrategy$ForceFileDeleteStrategy
100%
4/4
N/A
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  
  * @since 1.3
 32  
  */
 33  
 public class FileDeleteStrategy {
 34  
 
 35  
     /**
 36  
      * The singleton instance for normal file deletion, which does not permit
 37  
      * the deletion of directories that are not empty.
 38  
      */
 39  6
     public static final FileDeleteStrategy NORMAL = new FileDeleteStrategy("Normal");
 40  
     /**
 41  
      * The singleton instance for forced file deletion, which always deletes,
 42  
      * even if the file represents a non-empty directory.
 43  
      */
 44  6
     public static final FileDeleteStrategy FORCE = new ForceFileDeleteStrategy();
 45  
 
 46  
     /** The name of the strategy. */
 47  
     private final String name;
 48  
 
 49  
     //-----------------------------------------------------------------------
 50  
     /**
 51  
      * Restricted constructor.
 52  
      *
 53  
      * @param name  the name by which the strategy is known
 54  
      */
 55  12
     protected FileDeleteStrategy(final String name) {
 56  12
         this.name = name;
 57  12
     }
 58  
 
 59  
     //-----------------------------------------------------------------------
 60  
     /**
 61  
      * Deletes the file object, which may be a file or a directory.
 62  
      * All <code>IOException</code>s are caught and false returned instead.
 63  
      * If the file does not exist or is null, true is returned.
 64  
      * <p>
 65  
      * Subclass writers should override {@link #doDelete(File)}, not this method.
 66  
      *
 67  
      * @param fileToDelete  the file to delete, null returns true
 68  
      * @return true if the file was deleted, or there was no such file
 69  
      */
 70  
     public boolean deleteQuietly(final File fileToDelete) {
 71  34
         if (fileToDelete == null || fileToDelete.exists() == false) {
 72  4
             return true;
 73  
         }
 74  
         try {
 75  30
             return doDelete(fileToDelete);
 76  0
         } catch (final IOException ex) {
 77  0
             return false;
 78  
         }
 79  
     }
 80  
 
 81  
     /**
 82  
      * Deletes the file object, which may be a file or a directory.
 83  
      * If the file does not exist, the method just returns.
 84  
      * <p>
 85  
      * Subclass writers should override {@link #doDelete(File)}, not this method.
 86  
      *
 87  
      * @param fileToDelete  the file to delete, not null
 88  
      * @throws NullPointerException if the file is null
 89  
      * @throws IOException if an error occurs during file deletion
 90  
      */
 91  
     public void delete(final File fileToDelete) throws IOException {
 92  14
         if (fileToDelete.exists() && doDelete(fileToDelete) == false) {
 93  2
             throw new IOException("Deletion failed: " + fileToDelete);
 94  
         }
 95  10
     }
 96  
 
 97  
     /**
 98  
      * Actually deletes the file object, which may be a file or a directory.
 99  
      * <p>
 100  
      * This method is designed for subclasses to override.
 101  
      * The implementation may return either false or an <code>IOException</code>
 102  
      * when deletion fails. The {@link #delete(File)} and {@link #deleteQuietly(File)}
 103  
      * methods will handle either response appropriately.
 104  
      * A check has been made to ensure that the file will exist.
 105  
      * <p>
 106  
      * This implementation uses {@link File#delete()}.
 107  
      *
 108  
      * @param fileToDelete  the file to delete, exists, not null
 109  
      * @return true if the file was deleted
 110  
      * @throws NullPointerException if the file is null
 111  
      * @throws IOException if an error occurs during file deletion
 112  
      */
 113  
     protected boolean doDelete(final File fileToDelete) throws IOException {
 114  32
         return fileToDelete.delete();
 115  
     }
 116  
 
 117  
     //-----------------------------------------------------------------------
 118  
     /**
 119  
      * Gets a string describing the delete strategy.
 120  
      *
 121  
      * @return a string describing the delete strategy
 122  
      */
 123  
     @Override
 124  
     public String toString() {
 125  4
         return "FileDeleteStrategy[" + name + "]";
 126  
     }
 127  
 
 128  
     //-----------------------------------------------------------------------
 129  
     /**
 130  
      * Force file deletion strategy.
 131  
      */
 132  
     static class ForceFileDeleteStrategy extends FileDeleteStrategy {
 133  
         /** Default Constructor */
 134  
         ForceFileDeleteStrategy() {
 135  6
             super("Force");
 136  6
         }
 137  
 
 138  
         /**
 139  
          * Deletes the file object.
 140  
          * <p>
 141  
          * This implementation uses <code>FileUtils.forceDelete()</code>
 142  
          * if the file exists.
 143  
          *
 144  
          * @param fileToDelete  the file to delete, not null
 145  
          * @return Always returns {@code true}
 146  
          * @throws NullPointerException if the file is null
 147  
          * @throws IOException if an error occurs during file deletion
 148  
          */
 149  
         @Override
 150  
         protected boolean doDelete(final File fileToDelete) throws IOException {
 151  6
             FileUtils.forceDelete(fileToDelete);
 152  6
             return true;
 153  
         }
 154  
     }
 155  
 
 156  
 }