001package org.unix4j.unix.find;
002
003import java.io.File;
004import java.io.IOException;
005import java.nio.file.Files;
006import java.nio.file.attribute.BasicFileAttributes;
007
008/**
009 * Helper to access file attributes.
010 */
011enum FileAttributes {
012        ;
013
014        private static BasicFileAttributes getBasicFileAttributes(File file) {
015                try {
016                        return Files.readAttributes(file.toPath(), BasicFileAttributes.class);
017                } catch (IOException e) {
018                        return null;
019                }
020        }
021
022        static long getLastModifiedTime(File file) {
023                final BasicFileAttributes atts = getBasicFileAttributes(file);
024                return atts != null ? atts.lastModifiedTime().toMillis() : file.lastModified();
025        }
026
027        static long getCreationTime(File file) {
028                final BasicFileAttributes atts = getBasicFileAttributes(file);
029                return atts != null ? atts.creationTime().toMillis() : file.lastModified();
030        }
031        
032        static long getLastAccessedTime(File file) {
033                final BasicFileAttributes atts = getBasicFileAttributes(file);
034                return atts != null ? atts.lastAccessTime().toMillis() : file.lastModified();
035        }
036        
037        static boolean isDirectory(File file) {
038                final BasicFileAttributes atts = getBasicFileAttributes(file);
039                return atts != null ? atts.isDirectory() : file.isDirectory();
040        }
041        
042        static boolean isRegularFile(File file) {
043                final BasicFileAttributes atts = getBasicFileAttributes(file);
044                return atts != null ? atts.isRegularFile() : file.isFile();
045        }
046        
047        static boolean isSymbolicLink(File file) {
048                final BasicFileAttributes atts = getBasicFileAttributes(file);
049                return atts != null ? atts.isSymbolicLink() : false;
050        }
051        
052        static boolean isOther(File file) {
053                final BasicFileAttributes atts = getBasicFileAttributes(file);
054                return atts != null ? atts.isOther() : !isDirectory(file) && !isRegularFile(file) && !isSymbolicLink(file);
055        }
056}