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 java.util.Optional.empty;
23 import java.util.Optional;
26 * Container for various file options.
28 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
30 public class FileOption {
32 /** The default for the insert state. */
33 private static final boolean DEFAULT_INSERT = true;
35 /** The default for the insert redirect state. */
36 private static final boolean DEFAULT_INSERT_REDIRECT = true;
38 /** The default for the custom key. */
39 private static final String DEFAULT_CUSTOM_KEY = "CHK@";
41 /** The insert state. */
42 private boolean insert;
44 /** Whether to force an insert. */
45 private boolean forceInsert;
47 /** Whether to insert a redirect. */
48 private boolean insertRedirect;
50 /** The hash of the last insert. */
51 private String lastInsertHash;
53 /** The edition of the last insert. */
54 private int lastInsertEdition;
56 /** The filename of the last insert. */
57 private String lastInsertFilename;
59 /** The current hash of the file. */
60 private String currentHash;
62 /** The custom key. */
63 private String customKey;
65 /** The changed name. */
66 private Optional<String> changedName = empty();
68 /** The default MIME type. */
69 private final String defaultMimeType;
71 /** The current MIME type. */
72 private String mimeType;
75 * Creates new file options.
77 * @param defaultMimeType
78 * The default MIME type of the file
80 public FileOption(String defaultMimeType) {
81 insert = DEFAULT_INSERT;
82 insertRedirect = DEFAULT_INSERT_REDIRECT;
83 customKey = DEFAULT_CUSTOM_KEY;
84 this.defaultMimeType = defaultMimeType;
85 mimeType = defaultMimeType;
88 public FileOption(FileOption other) {
89 this.insert = other.insert;
90 this.forceInsert = other.forceInsert;
91 this.insertRedirect = other.insertRedirect;
92 this.lastInsertHash = other.lastInsertHash;
93 this.lastInsertEdition = other.lastInsertEdition;
94 this.lastInsertFilename = other.lastInsertFilename;
95 this.currentHash = other.currentHash;
96 this.customKey = other.customKey;
97 this.changedName = other.changedName;
98 this.defaultMimeType = other.defaultMimeType;
99 this.mimeType = other.mimeType;
103 * Returns the custom key. The custom key is only used when
104 * {@link #isInsert()} and {@link #isInsertRedirect()} both return {@code
107 * @return The custom key
109 public String getCustomKey() {
114 * Sets the custom key. The custom key is only used when {@link #isInsert()}
115 * and {@link #isInsertRedirect()} both return {@code true}.
120 public void setCustomKey(String customKey) {
121 if (customKey == null) {
124 this.customKey = customKey;
129 * Returns whether the file should be inserted. If a file is not inserted
130 * and {@link #isInsertRedirect()} is also {@code false}, the file will not
131 * be inserted at all.
133 * @see #setCustomKey(String)
134 * @return <code>true</code> if the file should be inserted,
135 * <code>false</code> otherwise
137 public boolean isInsert() {
142 * Sets whether the file should be inserted. If a file is not inserted and
143 * {@link #isInsertRedirect()} is also {@code false}, the file will not be
147 * <code>true</code> if the file should be inserted,
148 * <code>false</code> otherwise
150 public void setInsert(boolean insert) {
151 this.insert = insert;
155 * Returns whether the insert of this file should be forced, even if its
156 * current hash matches the last insert hash.
158 * @return {@code true} to force the insert of this file, {@code false}
161 public boolean isForceInsert() {
166 * Sets whether to force the insert of this file, even if its current hash
167 * matches the last insert hash.
170 * {@code true} to force the insert of this file, {@code false}
172 * @return These file options
174 public FileOption setForceInsert(boolean forceInsert) {
175 this.forceInsert = forceInsert;
180 * Returns whether a redirect to a different key should be inserted. This
181 * will only matter if {@link #isInsert()} returns {@code false}. The key
182 * that should be redirected to still needs to be specified via
183 * {@link #setCustomKey(String)}.
185 * @return {@code true} if a redirect should be inserted, {@code false}
188 public boolean isInsertRedirect() {
189 return insertRedirect;
193 * Sets whether a redirect should be inserted. This will only matter if
194 * {@link #isInsert()} returns {@code false}, i.e. it has been
195 * {@link #setInsert(boolean)} to {@code false}. The key that should be
196 * redirected to still needs to be specified via
197 * {@link #setCustomKey(String)}.
199 * @param insertRedirect
200 * {@code true} if a redirect should be inserted, {@code false}
203 public void setInsertRedirect(boolean insertRedirect) {
204 this.insertRedirect = insertRedirect;
208 * Returns the hash of the file when it was last inserted
210 * @return The last hash of the file
212 public String getLastInsertHash() {
213 return lastInsertHash;
217 * Sets the hash of the file when it was last inserted.
219 * @param lastInsertHash
220 * The last hash of the file
221 * @return These file options
223 public FileOption setLastInsertHash(String lastInsertHash) {
224 this.lastInsertHash = lastInsertHash;
229 * Returns the last edition at which this file was inserted.
231 * @return The last insert edition of this file
233 public int getLastInsertEdition() {
234 return lastInsertEdition;
238 * Sets the last insert edition of this file.
240 * @param lastInsertEdition
241 * The last insert edition of this file
242 * @return These file options
244 public FileOption setLastInsertEdition(int lastInsertEdition) {
245 this.lastInsertEdition = lastInsertEdition;
250 * Returns the name of the file when it was last inserted.
252 * @return The name of the file at the last insert
254 public String getLastInsertFilename() {
255 return lastInsertFilename;
259 * Sets the name of the file when it was last inserted.
261 * @param lastInsertFilename
262 * The name of the file at the last insert.
263 * @return These file options
265 public FileOption setLastInsertFilename(String lastInsertFilename) {
266 this.lastInsertFilename = lastInsertFilename;
271 * Returns the current hash of the file. This value is ony a temporary value
272 * that is copied to {@link #getLastInsertHash()} when a project has
273 * finished inserting.
275 * @see Project#onSuccessfulInsert()
276 * @return The current hash of the file
278 public String getCurrentHash() {
283 * Sets the current hash of the file.
286 * The current hash of the file
287 * @return These file options
289 public FileOption setCurrentHash(String currentHash) {
290 this.currentHash = currentHash;
295 * Returns the changed name for this file. This method will return {@code
296 * null} or an empty {@link String} if this file should not be renamed.
298 * @return The changed name, or {@code null} if this file should not be
301 public Optional<String> getChangedName() {
306 * Sets the changed name for this file. Setting the changed file to {@code
307 * null} or an empty {@link String} will disable renaming.
310 * The new changed name for this file
312 public void setChangedName(String changedName) {
313 this.changedName = ((changedName != null) && (changedName.length() > 0)) ? Optional.of(changedName) : Optional.<String>empty();
317 * Sets the MIME type of the file. Setting the MIME type to
318 * <code>null</code> will set the MIME type to the default MIME type.
321 * The MIME type of the file
323 public void setMimeType(String mimeType) {
324 if (mimeType == null) {
325 this.mimeType = defaultMimeType;
327 this.mimeType = mimeType;
332 * Returns the MIME type of the file. If no custom MIME type has been set,
333 * the default MIME type is returned.
335 * @return The MIME type of the file
337 public String getMimeType() {
342 * Returns whether the options for this file have been modified, i.e. are
343 * not at their default values.
345 * @return <code>true</code> if the options have been modified,
346 * <code>false</code> if they are at default values
348 public boolean isCustom() {
349 if (insert != DEFAULT_INSERT) {
352 if (!customKey.equals(DEFAULT_CUSTOM_KEY)) {
355 if (changedName.isPresent()) {
358 if (!defaultMimeType.equals(mimeType)) {
361 if (insertRedirect != DEFAULT_INSERT_REDIRECT) {