🔀 Merge branch 'website/epic-games' into next
[rhynodge.git] / src / main / java / net / pterodactylus / rhynodge / states / FileState.java
1 /*
2  * Rhynodge - FileState.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.rhynodge.states;
19
20 import javax.annotation.Nonnull;
21
22 import net.pterodactylus.rhynodge.State;
23
24 /**
25  * A {@link State} that contains information about a file.
26  *
27  * @author <a href="mailto:bombe@pterodactylus.net">David â€˜Bombe’ Roden</a>
28  */
29 public class FileState extends AbstractState {
30
31         /** Whether the file exists. */
32         private final boolean exists;
33
34         /** Whether the file is readable. */
35         private final boolean readable;
36
37         /** The size of the file. */
38         private final long size;
39
40         /** The modification time of the file. */
41         private final long modificationTime;
42
43         /**
44          * Creates a new file state that signals that an exceptio occured during
45          * retrieval.
46          *
47          * @param exception
48          *            The exception that occured
49          */
50         public FileState(Throwable exception) {
51                 super(exception);
52                 exists = false;
53                 readable = false;
54                 size = -1;
55                 modificationTime = -1;
56         }
57
58         /**
59          * Creates a new file state.
60          *
61          * @param exists
62          *            {@code true} if the file exists, {@code false} otherwise
63          * @param readable
64          *            {@code true} if the file is readable, {@code false} otherwise
65          * @param size
66          *            The size of the file (in bytes)
67          * @param modificationTime
68          *            The modification time of the file (in milliseconds since Jan
69          *            1, 1970 UTC)
70          */
71         public FileState(boolean exists, boolean readable, long size, long modificationTime) {
72                 this.exists = exists;
73                 this.readable = readable;
74                 this.size = size;
75                 this.modificationTime = modificationTime;
76         }
77
78         //
79         // ACCESSORS
80         //
81
82         @Override
83         public boolean isEmpty() {
84                 return !exists;
85         }
86
87         /**
88          * Returns whether the file exists.
89          *
90          * @return {@code true} if the file exists, {@code false} otherwise
91          */
92         public boolean exists() {
93                 return exists;
94         }
95
96         /**
97          * Returns whether the file is readable.
98          *
99          * @return {@code true} if the file is readable, {@code false} otherwise
100          */
101         public boolean readable() {
102                 return readable;
103         }
104
105         /**
106          * Returns the size of the file.
107          *
108          * @return The size of the file (in bytes)
109          */
110         public long size() {
111                 return size;
112         }
113
114         /**
115          * Returns the modification time of the file.
116          *
117          * @return The modification time of the file (in milliseconds since Jan 1,
118          *         1970 UTC)
119          */
120         public long modificationTime() {
121                 return modificationTime;
122         }
123
124         @Nonnull
125         @Override
126         protected String plainText() {
127                 return toString();
128         }
129
130         //
131         // OBJECT METHODS
132         //
133
134         /**
135          * {@inheritDoc}
136          */
137         @Override
138         public String toString() {
139                 return String.format("%s[exists=%s,readable=%s,size=%s,modificationTime=%d(%5$tc)", getClass().getSimpleName(), exists(), readable(), size(), modificationTime());
140         }
141
142 }