2 * jSite - FileOption.java - Copyright © 2006–2014 David Roden
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
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
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.
19 package de.todesbaum.jsite.application;
21 import static com.google.common.base.Optional.absent;
22 import static com.google.common.base.Optional.of;
24 import com.google.common.base.Optional;
27 * Container for various file options.
29 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
31 public class FileOption {
33 /** The default for the insert state. */
34 private static final boolean DEFAULT_INSERT = true;
36 /** The default for the insert redirect state. */
37 private static final boolean DEFAULT_INSERT_REDIRECT = true;
39 /** The default for the custom key. */
40 private static final String DEFAULT_CUSTOM_KEY = "CHK@";
42 /** The insert state. */
43 private boolean insert;
45 /** Whether to force an insert. */
46 private boolean forceInsert;
48 /** Whether to insert a redirect. */
49 private boolean insertRedirect;
51 /** The hash of the last insert. */
52 private String lastInsertHash;
54 /** The edition of the last insert. */
55 private int lastInsertEdition;
57 /** The filename of the last insert. */
58 private String lastInsertFilename;
60 /** The current hash of the file. */
61 private String currentHash;
63 /** The custom key. */
64 private String customKey;
66 /** The changed name. */
67 private Optional<String> changedName = absent();
69 /** The default MIME type. */
70 private final String defaultMimeType;
72 /** The current MIME type. */
73 private String mimeType;
76 * Creates new file options.
78 * @param defaultMimeType
79 * The default MIME type of the file
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;
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;
104 * Returns the custom key. The custom key is only used when
105 * {@link #isInsert()} and {@link #isInsertRedirect()} both return {@code
108 * @return The custom key
110 public String getCustomKey() {
115 * Sets the custom key. The custom key is only used when {@link #isInsert()}
116 * and {@link #isInsertRedirect()} both return {@code true}.
121 public void setCustomKey(String customKey) {
122 if (customKey == null) {
125 this.customKey = customKey;
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.
134 * @see #setCustomKey(String)
135 * @return <code>true</code> if the file should be inserted,
136 * <code>false</code> otherwise
138 public boolean isInsert() {
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
148 * <code>true</code> if the file should be inserted,
149 * <code>false</code> otherwise
151 public void setInsert(boolean insert) {
152 this.insert = insert;
156 * Returns whether the insert of this file should be forced, even if its
157 * current hash matches the last insert hash.
159 * @return {@code true} to force the insert of this file, {@code false}
162 public boolean isForceInsert() {
167 * Sets whether to force the insert of this file, even if its current hash
168 * matches the last insert hash.
171 * {@code true} to force the insert of this file, {@code false}
173 * @return These file options
175 public FileOption setForceInsert(boolean forceInsert) {
176 this.forceInsert = forceInsert;
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)}.
186 * @return {@code true} if a redirect should be inserted, {@code false}
189 public boolean isInsertRedirect() {
190 return insertRedirect;
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)}.
200 * @param insertRedirect
201 * {@code true} if a redirect should be inserted, {@code false}
204 public void setInsertRedirect(boolean insertRedirect) {
205 this.insertRedirect = insertRedirect;
209 * Returns the hash of the file when it was last inserted
211 * @return The last hash of the file
213 public String getLastInsertHash() {
214 return lastInsertHash;
218 * Sets the hash of the file when it was last inserted.
220 * @param lastInsertHash
221 * The last hash of the file
222 * @return These file options
224 public FileOption setLastInsertHash(String lastInsertHash) {
225 this.lastInsertHash = lastInsertHash;
230 * Returns the last edition at which this file was inserted.
232 * @return The last insert edition of this file
234 public int getLastInsertEdition() {
235 return lastInsertEdition;
239 * Sets the last insert edition of this file.
241 * @param lastInsertEdition
242 * The last insert edition of this file
243 * @return These file options
245 public FileOption setLastInsertEdition(int lastInsertEdition) {
246 this.lastInsertEdition = lastInsertEdition;
251 * Returns the name of the file when it was last inserted.
253 * @return The name of the file at the last insert
255 public String getLastInsertFilename() {
256 return lastInsertFilename;
260 * Sets the name of the file when it was last inserted.
262 * @param lastInsertFilename
263 * The name of the file at the last insert.
264 * @return These file options
266 public FileOption setLastInsertFilename(String lastInsertFilename) {
267 this.lastInsertFilename = lastInsertFilename;
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.
276 * @see Project#onSuccessfulInsert()
277 * @return The current hash of the file
279 public String getCurrentHash() {
284 * Sets the current hash of the file.
287 * The current hash of the file
288 * @return These file options
290 public FileOption setCurrentHash(String currentHash) {
291 this.currentHash = currentHash;
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.
299 * @return The changed name, or {@code null} if this file should not be
302 public Optional<String> getChangedName() {
307 * Sets the changed name for this file. Setting the changed file to {@code
308 * null} or an empty {@link String} will disable renaming.
311 * The new changed name for this file
313 public void setChangedName(String changedName) {
314 this.changedName = ((changedName != null) && (changedName.length() > 0)) ? of(changedName) : Optional.<String>absent();
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.
322 * The MIME type of the file
324 public void setMimeType(String mimeType) {
325 if (mimeType == null) {
326 this.mimeType = defaultMimeType;
328 this.mimeType = mimeType;
333 * Returns the MIME type of the file. If no custom MIME type has been set,
334 * the default MIME type is returned.
336 * @return The MIME type of the file
338 public String getMimeType() {
343 * Returns whether the options for this file have been modified, i.e. are
344 * not at their default values.
346 * @return <code>true</code> if the options have been modified,
347 * <code>false</code> if they are at default values
349 public boolean isCustom() {
350 if (insert != DEFAULT_INSERT) {
353 if (!customKey.equals(DEFAULT_CUSTOM_KEY)) {
356 if (changedName.isPresent()) {
359 if (!defaultMimeType.equals(mimeType)) {
362 if (insertRedirect != DEFAULT_INSERT_REDIRECT) {