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;
90 * Returns the custom key. The custom key is only used when
91 * {@link #isInsert()} and {@link #isInsertRedirect()} both return {@code
94 * @return The custom key
96 public String getCustomKey() {
101 * Sets the custom key. The custom key is only used when {@link #isInsert()}
102 * and {@link #isInsertRedirect()} both return {@code true}.
107 public void setCustomKey(String customKey) {
108 if (customKey == null) {
111 this.customKey = customKey;
116 * Returns whether the file should be inserted. If a file is not inserted
117 * and {@link #isInsertRedirect()} is also {@code false}, the file will not
118 * be inserted at all.
120 * @see #setCustomKey(String)
121 * @return <code>true</code> if the file should be inserted,
122 * <code>false</code> otherwise
124 public boolean isInsert() {
129 * Sets whether the file should be inserted. If a file is not inserted and
130 * {@link #isInsertRedirect()} is also {@code false}, the file will not be
134 * <code>true</code> if the file should be inserted,
135 * <code>false</code> otherwise
137 public void setInsert(boolean insert) {
138 this.insert = insert;
142 * Returns whether the insert of this file should be forced, even if its
143 * current hash matches the last insert hash.
145 * @return {@code true} to force the insert of this file, {@code false}
148 public boolean isForceInsert() {
153 * Sets whether to force the insert of this file, even if its current hash
154 * matches the last insert hash.
157 * {@code true} to force the insert of this file, {@code false}
159 * @return These file options
161 public FileOption setForceInsert(boolean forceInsert) {
162 this.forceInsert = forceInsert;
167 * Returns whether a redirect to a different key should be inserted. This
168 * will only matter if {@link #isInsert()} returns {@code false}. The key
169 * that should be redirected to still needs to be specified via
170 * {@link #setCustomKey(String)}.
172 * @return {@code true} if a redirect should be inserted, {@code false}
175 public boolean isInsertRedirect() {
176 return insertRedirect;
180 * Sets whether a redirect should be inserted. This will only matter if
181 * {@link #isInsert()} returns {@code false}, i.e. it has been
182 * {@link #setInsert(boolean)} to {@code false}. The key that should be
183 * redirected to still needs to be specified via
184 * {@link #setCustomKey(String)}.
186 * @param insertRedirect
187 * {@code true} if a redirect should be inserted, {@code false}
190 public void setInsertRedirect(boolean insertRedirect) {
191 this.insertRedirect = insertRedirect;
195 * Returns the hash of the file when it was last inserted
197 * @return The last hash of the file
199 public String getLastInsertHash() {
200 return lastInsertHash;
204 * Sets the hash of the file when it was last inserted.
206 * @param lastInsertHash
207 * The last hash of the file
208 * @return These file options
210 public FileOption setLastInsertHash(String lastInsertHash) {
211 this.lastInsertHash = lastInsertHash;
216 * Returns the last edition at which this file was inserted.
218 * @return The last insert edition of this file
220 public int getLastInsertEdition() {
221 return lastInsertEdition;
225 * Sets the last insert edition of this file.
227 * @param lastInsertEdition
228 * The last insert edition of this file
229 * @return These file options
231 public FileOption setLastInsertEdition(int lastInsertEdition) {
232 this.lastInsertEdition = lastInsertEdition;
237 * Returns the name of the file when it was last inserted.
239 * @return The name of the file at the last insert
241 public String getLastInsertFilename() {
242 return lastInsertFilename;
246 * Sets the name of the file when it was last inserted.
248 * @param lastInsertFilename
249 * The name of the file at the last insert.
250 * @return These file options
252 public FileOption setLastInsertFilename(String lastInsertFilename) {
253 this.lastInsertFilename = lastInsertFilename;
258 * Returns the current hash of the file. This value is ony a temporary value
259 * that is copied to {@link #getLastInsertHash()} when a project has
260 * finished inserting.
262 * @see Project#onSuccessfulInsert()
263 * @return The current hash of the file
265 public String getCurrentHash() {
270 * Sets the current hash of the file.
273 * The current hash of the file
274 * @return These file options
276 public FileOption setCurrentHash(String currentHash) {
277 this.currentHash = currentHash;
282 * Returns the changed name for this file. This method will return {@code
283 * null} or an empty {@link String} if this file should not be renamed.
285 * @return The changed name, or {@code null} if this file should not be
288 public Optional<String> getChangedName() {
293 * Sets the changed name for this file. Setting the changed file to {@code
294 * null} or an empty {@link String} will disable renaming.
297 * The new changed name for this file
299 public void setChangedName(String changedName) {
300 this.changedName = ((changedName != null) && (changedName.length() > 0)) ? of(changedName) : Optional.<String>absent();
304 * Sets the MIME type of the file. Setting the MIME type to
305 * <code>null</code> will set the MIME type to the default MIME type.
308 * The MIME type of the file
310 public void setMimeType(String mimeType) {
311 if (mimeType == null) {
312 this.mimeType = defaultMimeType;
314 this.mimeType = mimeType;
319 * Returns the MIME type of the file. If no custom MIME type has been set,
320 * the default MIME type is returned.
322 * @return The MIME type of the file
324 public String getMimeType() {
329 * Returns whether the options for this file have been modified, i.e. are
330 * not at their default values.
332 * @return <code>true</code> if the options have been modified,
333 * <code>false</code> if they are at default values
335 public boolean isCustom() {
336 if (insert != DEFAULT_INSERT) {
339 if (!customKey.equals(DEFAULT_CUSTOM_KEY)) {
342 if (changedName.isPresent()) {
345 if (!defaultMimeType.equals(mimeType)) {
348 if (insertRedirect != DEFAULT_INSERT_REDIRECT) {