Add copy constructor
[jSite.git] / src / main / java / de / todesbaum / jsite / application / FileOption.java
1 /*
2  * jSite - FileOption.java - Copyright © 2006–2014 David Roden
3  *
4  * This program is free software; you can redistribute it and/or modify it under
5  * the terms of the GNU General Public License as published by the Free Software
6  * Foundation; either version 2 of the License, or (at your option) any later
7  * version.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12  * details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
16  * Place - Suite 330, Boston, MA 02111-1307, USA.
17  */
18
19 package de.todesbaum.jsite.application;
20
21 import static com.google.common.base.Optional.absent;
22 import static com.google.common.base.Optional.of;
23
24 import com.google.common.base.Optional;
25
26 /**
27  * Container for various file options.
28  *
29  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
30  */
31 public class FileOption {
32
33         /** The default for the insert state. */
34         private static final boolean DEFAULT_INSERT = true;
35
36         /** The default for the insert redirect state. */
37         private static final boolean DEFAULT_INSERT_REDIRECT = true;
38
39         /** The default for the custom key. */
40         private static final String DEFAULT_CUSTOM_KEY = "CHK@";
41
42         /** The insert state. */
43         private boolean insert;
44
45         /** Whether to force an insert. */
46         private boolean forceInsert;
47
48         /** Whether to insert a redirect. */
49         private boolean insertRedirect;
50
51         /** The hash of the last insert. */
52         private String lastInsertHash;
53
54         /** The edition of the last insert. */
55         private int lastInsertEdition;
56
57         /** The filename of the last insert. */
58         private String lastInsertFilename;
59
60         /** The current hash of the file. */
61         private String currentHash;
62
63         /** The custom key. */
64         private String customKey;
65
66         /** The changed name. */
67         private Optional<String> changedName = absent();
68
69         /** The default MIME type. */
70         private final String defaultMimeType;
71
72         /** The current MIME type. */
73         private String mimeType;
74
75         /**
76          * Creates new file options.
77          *
78          * @param defaultMimeType
79          *            The default MIME type of the file
80          */
81         public FileOption(String defaultMimeType) {
82                 insert = DEFAULT_INSERT;
83                 insertRedirect = DEFAULT_INSERT_REDIRECT;
84                 customKey = DEFAULT_CUSTOM_KEY;
85                 this.defaultMimeType = defaultMimeType;
86                 mimeType = defaultMimeType;
87         }
88
89         public FileOption(FileOption other) {
90                 this.insert = other.insert;
91                 this.forceInsert = other.forceInsert;
92                 this.insertRedirect = other.insertRedirect;
93                 this.lastInsertHash = other.lastInsertHash;
94                 this.lastInsertEdition = other.lastInsertEdition;
95                 this.lastInsertFilename = other.lastInsertFilename;
96                 this.currentHash = other.currentHash;
97                 this.customKey = other.customKey;
98                 this.changedName = other.changedName;
99                 this.defaultMimeType = other.defaultMimeType;
100                 this.mimeType = other.mimeType;
101         }
102
103         /**
104          * Returns the custom key. The custom key is only used when
105          * {@link #isInsert()} and {@link #isInsertRedirect()} both return {@code
106          * true}.
107          *
108          * @return The custom key
109          */
110         public String getCustomKey() {
111                 return customKey;
112         }
113
114         /**
115          * Sets the custom key. The custom key is only used when {@link #isInsert()}
116          * and {@link #isInsertRedirect()} both return {@code true}.
117          *
118          * @param customKey
119          *            The custom key
120          */
121         public void setCustomKey(String customKey) {
122                 if (customKey == null) {
123                         this.customKey = "";
124                 } else {
125                         this.customKey = customKey;
126                 }
127         }
128
129         /**
130          * Returns whether the file should be inserted. If a file is not inserted
131          * and {@link #isInsertRedirect()} is also {@code false}, the file will not
132          * be inserted at all.
133          *
134          * @see #setCustomKey(String)
135          * @return <code>true</code> if the file should be inserted,
136          *         <code>false</code> otherwise
137          */
138         public boolean isInsert() {
139                 return insert;
140         }
141
142         /**
143          * Sets whether the file should be inserted. If a file is not inserted and
144          * {@link #isInsertRedirect()} is also {@code false}, the file will not be
145          * inserted at all.
146          *
147          * @param insert
148          *            <code>true</code> if the file should be inserted,
149          *            <code>false</code> otherwise
150          */
151         public void setInsert(boolean insert) {
152                 this.insert = insert;
153         }
154
155         /**
156          * Returns whether the insert of this file should be forced, even if its
157          * current hash matches the last insert hash.
158          *
159          * @return {@code true} to force the insert of this file, {@code false}
160          *         otherwise
161          */
162         public boolean isForceInsert() {
163                 return forceInsert;
164         }
165
166         /**
167          * Sets whether to force the insert of this file, even if its current hash
168          * matches the last insert hash.
169          *
170          * @param forceInsert
171          *            {@code true} to force the insert of this file, {@code false}
172          *            otherwise
173          * @return These file options
174          */
175         public FileOption setForceInsert(boolean forceInsert) {
176                 this.forceInsert = forceInsert;
177                 return this;
178         }
179
180         /**
181          * Returns whether a redirect to a different key should be inserted. This
182          * will only matter if {@link #isInsert()} returns {@code false}. The key
183          * that should be redirected to still needs to be specified via
184          * {@link #setCustomKey(String)}.
185          *
186          * @return {@code true} if a redirect should be inserted, {@code false}
187          *         otherwise
188          */
189         public boolean isInsertRedirect() {
190                 return insertRedirect;
191         }
192
193         /**
194          * Sets whether a redirect should be inserted. This will only matter if
195          * {@link #isInsert()} returns {@code false}, i.e. it has been
196          * {@link #setInsert(boolean)} to {@code false}. The key that should be
197          * redirected to still needs to be specified via
198          * {@link #setCustomKey(String)}.
199          *
200          * @param insertRedirect
201          *            {@code true} if a redirect should be inserted, {@code false}
202          *            otherwise
203          */
204         public void setInsertRedirect(boolean insertRedirect) {
205                 this.insertRedirect = insertRedirect;
206         }
207
208         /**
209          * Returns the hash of the file when it was last inserted
210          *
211          * @return The last hash of the file
212          */
213         public String getLastInsertHash() {
214                 return lastInsertHash;
215         }
216
217         /**
218          * Sets the hash of the file when it was last inserted.
219          *
220          * @param lastInsertHash
221          *            The last hash of the file
222          * @return These file options
223          */
224         public FileOption setLastInsertHash(String lastInsertHash) {
225                 this.lastInsertHash = lastInsertHash;
226                 return this;
227         }
228
229         /**
230          * Returns the last edition at which this file was inserted.
231          *
232          * @return The last insert edition of this file
233          */
234         public int getLastInsertEdition() {
235                 return lastInsertEdition;
236         }
237
238         /**
239          * Sets the last insert edition of this file.
240          *
241          * @param lastInsertEdition
242          *            The last insert edition of this file
243          * @return These file options
244          */
245         public FileOption setLastInsertEdition(int lastInsertEdition) {
246                 this.lastInsertEdition = lastInsertEdition;
247                 return this;
248         }
249
250         /**
251          * Returns the name of the file when it was last inserted.
252          *
253          * @return The name of the file at the last insert
254          */
255         public String getLastInsertFilename() {
256                 return lastInsertFilename;
257         }
258
259         /**
260          * Sets the name of the file when it was last inserted.
261          *
262          * @param lastInsertFilename
263          *            The name of the file at the last insert.
264          * @return These file options
265          */
266         public FileOption setLastInsertFilename(String lastInsertFilename) {
267                 this.lastInsertFilename = lastInsertFilename;
268                 return this;
269         }
270
271         /**
272          * Returns the current hash of the file. This value is ony a temporary value
273          * that is copied to {@link #getLastInsertHash()} when a project has
274          * finished inserting.
275          *
276          * @see Project#onSuccessfulInsert()
277          * @return The current hash of the file
278          */
279         public String getCurrentHash() {
280                 return currentHash;
281         }
282
283         /**
284          * Sets the current hash of the file.
285          *
286          * @param currentHash
287          *            The current hash of the file
288          * @return These file options
289          */
290         public FileOption setCurrentHash(String currentHash) {
291                 this.currentHash = currentHash;
292                 return this;
293         }
294
295         /**
296          * Returns the changed name for this file. This method will return {@code
297          * null} or an empty {@link String} if this file should not be renamed.
298          *
299          * @return The changed name, or {@code null} if this file should not be
300          *         renamed
301          */
302         public Optional<String> getChangedName() {
303                 return changedName;
304         }
305
306         /**
307          * Sets the changed name for this file. Setting the changed file to {@code
308          * null} or an empty {@link String} will disable renaming.
309          *
310          * @param changedName
311          *            The new changed name for this file
312          */
313         public void setChangedName(String changedName) {
314                 this.changedName = ((changedName != null) && (changedName.length() > 0)) ? of(changedName) : Optional.<String>absent();
315         }
316
317         /**
318          * Sets the MIME type of the file. Setting the MIME type to
319          * <code>null</code> will set the MIME type to the default MIME type.
320          *
321          * @param mimeType
322          *            The MIME type of the file
323          */
324         public void setMimeType(String mimeType) {
325                 if (mimeType == null) {
326                         this.mimeType = defaultMimeType;
327                 } else {
328                         this.mimeType = mimeType;
329                 }
330         }
331
332         /**
333          * Returns the MIME type of the file. If no custom MIME type has been set,
334          * the default MIME type is returned.
335          *
336          * @return The MIME type of the file
337          */
338         public String getMimeType() {
339                 return mimeType;
340         }
341
342         /**
343          * Returns whether the options for this file have been modified, i.e. are
344          * not at their default values.
345          *
346          * @return <code>true</code> if the options have been modified,
347          *         <code>false</code> if they are at default values
348          */
349         public boolean isCustom() {
350                 if (insert != DEFAULT_INSERT) {
351                         return true;
352                 }
353                 if (!customKey.equals(DEFAULT_CUSTOM_KEY)) {
354                         return true;
355                 }
356                 if (changedName.isPresent()) {
357                         return true;
358                 }
359                 if (!defaultMimeType.equals(mimeType)) {
360                         return true;
361                 }
362                 if (insertRedirect != DEFAULT_INSERT_REDIRECT) {
363                         return true;
364                 }
365                 return false;
366         }
367
368 }