Coverage Report - org.apache.commons.io.monitor.FileEntry
 
Classes in this File Line Coverage Branch Coverage Complexity
FileEntry
61%
26/42
59%
13/22
1.579
 
 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.monitor;
 18  
 
 19  
 import java.io.File;
 20  
 import java.io.Serializable;
 21  
 
 22  
 /**
 23  
  * The state of a file or directory, capturing the following {@link File} attributes at a point in time.
 24  
  * <ul>
 25  
  *   <li>File Name (see {@link File#getName()})</li>
 26  
  *   <li>Exists - whether the file exists or not (see {@link File#exists()})</li>
 27  
  *   <li>Directory - whether the file is a directory or not (see {@link File#isDirectory()})</li>
 28  
  *   <li>Last Modified Date/Time (see {@link File#lastModified()})</li>
 29  
  *   <li>Length (see {@link File#length()}) - directories treated as zero</li>
 30  
  *   <li>Children - contents of a directory (see {@link File#listFiles(java.io.FileFilter)})</li>
 31  
  * </ul>
 32  
  * 
 33  
  * <h3>Custom Implementations</h3>
 34  
  * <p>
 35  
  * If the state of additional {@link File} attributes is required then create a custom
 36  
  * {@link FileEntry} with properties for those attributes. Override the
 37  
  * {@link #newChildInstance(File)} to return a new instance of the appropriate type.
 38  
  * You may also want to override the {@link #refresh(File)} method.
 39  
  * </p>
 40  
  * @see FileAlterationObserver
 41  
  * @since 2.0
 42  
  */
 43  
 public class FileEntry implements Serializable {
 44  
 
 45  4
     static final FileEntry[] EMPTY_ENTRIES = new FileEntry[0];
 46  
 
 47  
     private final FileEntry parent;
 48  
     private FileEntry[] children;
 49  
     private final File file;
 50  
     private String name;
 51  
     private boolean exists;
 52  
     private boolean directory;
 53  
     private long lastModified;
 54  
     private long length;
 55  
 
 56  
     /**
 57  
      * Construct a new monitor for a specified {@link File}.
 58  
      *
 59  
      * @param file The file being monitored
 60  
      */
 61  
     public FileEntry(final File file) {
 62  32
         this((FileEntry)null, file);
 63  32
     }
 64  
 
 65  
     /**
 66  
      * Construct a new monitor for a specified {@link File}.
 67  
      *
 68  
      * @param parent The parent
 69  
      * @param file The file being monitored
 70  
      */
 71  88
     public FileEntry(final FileEntry parent, final File file) {
 72  88
         if (file == null) {
 73  0
             throw new IllegalArgumentException("File is missing");
 74  
         }
 75  88
         this.file = file;
 76  88
         this.parent = parent;
 77  88
         this.name = file.getName();
 78  88
     }
 79  
 
 80  
     /**
 81  
      * Refresh the attributes from the {@link File}, indicating
 82  
      * whether the file has changed.
 83  
      * <p>
 84  
      * This implementation refreshes the <code>name</code>, <code>exists</code>,
 85  
      * <code>directory</code>, <code>lastModified</code> and <code>length</code>
 86  
      * properties.
 87  
      * <p>
 88  
      * The <code>exists</code>, <code>directory</code>, <code>lastModified</code>
 89  
      * and <code>length</code> properties are compared for changes
 90  
      *
 91  
      * @param file the file instance to compare to
 92  
      * @return {@code true} if the file has changed, otherwise {@code false}
 93  
      */
 94  
     public boolean refresh(final File file) {
 95  
 
 96  
         // cache original values
 97  226
         final boolean origExists       = exists;
 98  226
         final long    origLastModified = lastModified;
 99  226
         final boolean origDirectory    = directory;
 100  226
         final long    origLength       = length;
 101  
 
 102  
         // refresh the values
 103  226
         name         = file.getName();
 104  226
         exists       = file.exists();
 105  226
         directory    = exists ? file.isDirectory() : false;
 106  226
         lastModified = exists ? file.lastModified() : 0;
 107  226
         length       = exists && !directory ? file.length() : 0;
 108  
 
 109  
         // Return if there are changes
 110  226
         return exists != origExists ||
 111  
                 lastModified != origLastModified ||
 112  
                 directory != origDirectory ||
 113  
                 length != origLength;
 114  
     }
 115  
 
 116  
     /**
 117  
      * Create a new child instance.
 118  
      * <p>
 119  
      * Custom implementations should override this method to return
 120  
      * a new instance of the appropriate type.
 121  
      *
 122  
      * @param file The child file
 123  
      * @return a new child instance
 124  
      */
 125  
     public FileEntry newChildInstance(final File file) {
 126  56
         return new FileEntry(this, file);
 127  
     }
 128  
 
 129  
     /**
 130  
      * Return the parent entry.
 131  
      *
 132  
      * @return the parent entry
 133  
      */
 134  
     public FileEntry getParent() {
 135  0
         return parent;
 136  
     }
 137  
 
 138  
     /**
 139  
      * Return the level
 140  
      *
 141  
      * @return the level
 142  
      */
 143  
     public int getLevel() {
 144  0
         return parent == null ? 0 : parent.getLevel() + 1;
 145  
     }
 146  
 
 147  
     /**
 148  
      * Return the directory's files.
 149  
      *
 150  
      * @return This directory's files or an empty
 151  
      * array if the file is not a directory or the
 152  
      * directory is empty
 153  
      */
 154  
     public FileEntry[] getChildren() {
 155  295
         return children != null ? children : EMPTY_ENTRIES;
 156  
     }
 157  
 
 158  
     /**
 159  
      * Set the directory's files.
 160  
      *
 161  
      * @param children This directory's files, may be null
 162  
      */
 163  
     public void setChildren(final FileEntry[] children) {
 164  323
         this.children = children;
 165  323
     }
 166  
 
 167  
     /**
 168  
      * Return the file being monitored.
 169  
      *
 170  
      * @return the file being monitored
 171  
      */
 172  
     public File getFile() {
 173  629
         return file;
 174  
     }
 175  
 
 176  
     /**
 177  
      * Return the file name.
 178  
      *
 179  
      * @return the file name
 180  
      */
 181  
     public String getName() {
 182  0
         return name;
 183  
     }
 184  
 
 185  
     /**
 186  
      * Set the file name.
 187  
      *
 188  
      * @param name the file name
 189  
      */
 190  
     public void setName(final String name) {
 191  0
         this.name = name;
 192  0
     }
 193  
 
 194  
     /**
 195  
      * Return the last modified time from the last time it
 196  
      * was checked.
 197  
      *
 198  
      * @return the last modified time
 199  
      */
 200  
     public long getLastModified() {
 201  0
         return lastModified;
 202  
     }
 203  
 
 204  
     /**
 205  
      * Return the last modified time from the last time it
 206  
      * was checked.
 207  
      *
 208  
      * @param lastModified The last modified time
 209  
      */
 210  
     public void setLastModified(final long lastModified) {
 211  0
         this.lastModified = lastModified;
 212  0
     }
 213  
 
 214  
     /**
 215  
      * Return the length.
 216  
      *
 217  
      * @return the length
 218  
      */
 219  
     public long getLength() {
 220  0
         return length;
 221  
     }
 222  
 
 223  
     /**
 224  
      * Set the length.
 225  
      *
 226  
      * @param length the length
 227  
      */
 228  
     public void setLength(final long length) {
 229  0
         this.length = length;
 230  0
     }
 231  
 
 232  
     /**
 233  
      * Indicate whether the file existed the last time it
 234  
      * was checked.
 235  
      *
 236  
      * @return whether the file existed
 237  
      */
 238  
     public boolean isExists() {
 239  2
         return exists;
 240  
     }
 241  
 
 242  
     /**
 243  
      * Set whether the file existed the last time it
 244  
      * was checked.
 245  
      *
 246  
      * @param exists whether the file exists or not
 247  
      */
 248  
     public void setExists(final boolean exists) {
 249  0
         this.exists = exists;
 250  0
     }
 251  
 
 252  
     /**
 253  
      * Indicate whether the file is a directory or not.
 254  
      *
 255  
      * @return whether the file is a directory or not
 256  
      */
 257  
     public boolean isDirectory() {
 258  224
         return directory;
 259  
     }
 260  
 
 261  
     /**
 262  
      * Set whether the file is a directory or not.
 263  
      *
 264  
      * @param directory whether the file is a directory or not
 265  
      */
 266  
     public void setDirectory(final boolean directory) {
 267  0
         this.directory = directory;
 268  0
     }
 269  
 }