001package org.unix4j.unix.find;
002
003import java.io.File;
004import java.io.FileFilter;
005import java.util.Date;
006
007/**
008 * File filter based on last modified, last accessed or created time of the
009 * file.
010 */
011class TimeFilter implements FileFilter {
012        public static enum TimeType implements Optionable<FindOption> {
013                Create(FindOption.timeCreate) {
014                        @Override
015                        public long getTime(File file) {
016                                return FileAttributes.getCreationTime(file);
017                        }
018                },
019                Access(FindOption.timeAccess) {
020                        @Override
021                        public long getTime(File file) {
022                                return FileAttributes.getLastAccessedTime(file);
023                        }
024                },
025                Modified(FindOption.timeModified) {
026                        @Override
027                        public long getTime(File file) {
028                                return FileAttributes.getLastModifiedTime(file);
029                        }
030                };
031                abstract public long getTime(File file);
032
033                private final FindOption option;
034
035                private TimeType(FindOption option) {
036                        this.option = option;
037                }
038
039                @Override
040                public FindOption getOption() {
041                        return option;
042                }
043        }
044
045        public static enum TimeComparator implements Optionable<FindOption> {
046                Older(FindOption.timeOlder) {
047                        @Override
048                        public boolean accept(File file, TimeType timeType, Date time) {
049                                return timeType.getTime(file) <= time.getTime();
050                        }
051                },
052                Newer(FindOption.timeNewer) {
053                        @Override
054                        public boolean accept(File file, TimeType timeType, Date time) {
055                                return timeType.getTime(file) >= time.getTime();
056                        }
057                };
058                abstract public boolean accept(File file, TimeType timeType, Date time);
059
060                private final FindOption option;
061
062                private TimeComparator(FindOption option) {
063                        this.option = option;
064                }
065
066                @Override
067                public FindOption getOption() {
068                        return option;
069                }
070        }
071
072        private final Date time;
073        private final TimeType timeType;;
074        private final TimeComparator comparator;
075
076        public TimeFilter(Date time, FindOptions options) {
077                this(time,
078                                OptionableUtil.findFirstEnumByOptionInSet(TimeType.class, options, TimeType.Modified),
079                                OptionableUtil.findFirstEnumByOptionInSet(TimeComparator.class, options, TimeComparator.Newer));
080        }
081
082        public TimeFilter(Date time, TimeType timeType, TimeComparator comparator) {
083                this.time = time;
084                this.timeType = timeType;
085                this.comparator = comparator;
086        }
087
088        @Override
089        public boolean accept(File file) {
090                return comparator.accept(file, timeType, time);
091        }
092
093}