ac0e895d924eb8740a2b9e3998dc919aa61197b7
[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.State;
26 import net.pterodactylus.reactor.states.FileState;
27
28 /**
29  * Queries the filesystem about a file.
30  *
31  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
32  */
33 public class FileQuery implements Query {
34
35         /** The name of the file to query. */
36         private final String filename;
37
38         /**
39          * Creates a new file query.
40          *
41          * @param filename
42          *            The name of the file to query
43          */
44         public FileQuery(String filename) {
45                 this.filename = checkNotNull(filename, "filename must not be null");
46         }
47
48         //
49         // QUERY METHODS
50         //
51
52         /**
53          * {@inheritDoc}
54          */
55         @Override
56         public State state() {
57                 File file = new File(filename);
58                 if (!file.exists()) {
59                         return new FileState(false, false, -1, -1);
60                 }
61                 if (!file.canRead()) {
62                         return new FileState(true, false, -1, -1);
63                 }
64                 return new FileState(true, true, file.length(), file.lastModified());
65         }
66
67 }