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