Add a file query.
[rhynodge.git] / src / main / java / net / pterodactylus / reactor / queries / FileQuery.java
1 /*
2  * Reactor - FileQuery.java - Copyright © 2013 David Roden
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.reactor.queries;
19
20 import static com.google.common.base.Preconditions.checkNotNull;
21
22 import java.io.File;
23
24 import net.pterodactylus.reactor.Query;
25 import net.pterodactylus.reactor.states.FileState;
26
27 /**
28  * Queries the filesystem about a file.
29  *
30  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
31  */
32 public class FileQuery implements Query<FileState> {
33
34         /** The name of the file to query. */
35         private final String filename;
36
37         /**
38          * Creates a new file query.
39          *
40          * @param filename
41          *            The name of the file to query
42          */
43         public FileQuery(String filename) {
44                 this.filename = checkNotNull(filename, "filename must not be null");
45         }
46
47         //
48         // QUERY METHODS
49         //
50
51         /**
52          * {@inheritDoc}
53          */
54         @Override
55         public FileState state() {
56                 File file = new File(filename);
57                 if (!file.exists()) {
58                         return new FileState(false, false, -1, -1);
59                 }
60                 if (!file.canRead()) {
61                         return new FileState(true, false, -1, -1);
62                 }
63                 return new FileState(true, true, file.length(), file.lastModified());
64         }
65
66 }