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