From 755c2a82abdb528794d1ccc3f342bb3b6d1481a1 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sun, 31 Jan 2016 05:23:55 +0100 Subject: [PATCH] =?utf8?q?Use=20JDK=E2=80=99s=20Optional=20instead=20of=20?= =?utf8?q?Guava=E2=80=99s?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../de/todesbaum/jsite/application/FileOption.java | 9 ++++----- .../de/todesbaum/jsite/application/Project.java | 2 +- .../jsite/application/ProjectInserter.java | 22 +++++++++++----------- .../de/todesbaum/jsite/gui/ProjectFilesPage.java | 2 +- .../de/todesbaum/jsite/main/Configuration.java | 2 +- 5 files changed, 18 insertions(+), 19 deletions(-) diff --git a/src/main/java/de/todesbaum/jsite/application/FileOption.java b/src/main/java/de/todesbaum/jsite/application/FileOption.java index 66efc09..8509257 100644 --- a/src/main/java/de/todesbaum/jsite/application/FileOption.java +++ b/src/main/java/de/todesbaum/jsite/application/FileOption.java @@ -18,10 +18,9 @@ package de.todesbaum.jsite.application; -import static com.google.common.base.Optional.absent; -import static com.google.common.base.Optional.of; +import static java.util.Optional.empty; -import com.google.common.base.Optional; +import java.util.Optional; /** * Container for various file options. @@ -64,7 +63,7 @@ public class FileOption { private String customKey; /** The changed name. */ - private Optional changedName = absent(); + private Optional changedName = empty(); /** The default MIME type. */ private final String defaultMimeType; @@ -311,7 +310,7 @@ public class FileOption { * The new changed name for this file */ public void setChangedName(String changedName) { - this.changedName = ((changedName != null) && (changedName.length() > 0)) ? of(changedName) : Optional.absent(); + this.changedName = ((changedName != null) && (changedName.length() > 0)) ? Optional.of(changedName) : Optional.empty(); } /** diff --git a/src/main/java/de/todesbaum/jsite/application/Project.java b/src/main/java/de/todesbaum/jsite/application/Project.java index d9146a1..aa0c00b 100644 --- a/src/main/java/de/todesbaum/jsite/application/Project.java +++ b/src/main/java/de/todesbaum/jsite/application/Project.java @@ -462,7 +462,7 @@ public class Project implements Comparable { if ((fileOption.getCurrentHash() != null) && (fileOption.getCurrentHash().length() > 0) && (!fileOption.getCurrentHash().equals(fileOption.getLastInsertHash()) || fileOption.isForceInsert())) { fileOption.setLastInsertEdition(edition); fileOption.setLastInsertHash(fileOption.getCurrentHash()); - fileOption.setLastInsertFilename(fileOption.getChangedName().or(fileOptionEntry.getKey())); + fileOption.setLastInsertFilename(fileOption.getChangedName().orElse(fileOptionEntry.getKey())); } fileOption.setForceInsert(false); } diff --git a/src/main/java/de/todesbaum/jsite/application/ProjectInserter.java b/src/main/java/de/todesbaum/jsite/application/ProjectInserter.java index f20c3d0..53b0726 100644 --- a/src/main/java/de/todesbaum/jsite/application/ProjectInserter.java +++ b/src/main/java/de/todesbaum/jsite/application/ProjectInserter.java @@ -30,6 +30,7 @@ import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; +import java.util.Optional; import java.util.Set; import java.util.concurrent.CountDownLatch; import java.util.concurrent.atomic.AtomicInteger; @@ -38,7 +39,6 @@ import java.util.logging.Logger; import net.pterodactylus.util.io.StreamCopier.ProgressListener; -import com.google.common.base.Optional; import de.todesbaum.jsite.gui.FileScanner; import de.todesbaum.jsite.gui.FileScanner.ScannedFile; import de.todesbaum.jsite.gui.FileScannerListener; @@ -195,7 +195,7 @@ public class ProjectInserter implements FileScannerListener, Runnable { * The name and hash of the file to insert * @return A file entry for the given file */ - private FileEntry createFileEntry(ScannedFile file) { + private Optional createFileEntry(ScannedFile file) { String filename = file.getFilename(); FileOption fileOption = project.getFileOption(filename); if (fileOption.isInsert()) { @@ -204,25 +204,25 @@ public class ProjectInserter implements FileScannerListener, Runnable { if (!project.isAlwaysForceInsert() && !fileOption.isForceInsert() && file.getHash().equals(fileOption.getLastInsertHash())) { /* only insert a redirect. */ logger.log(Level.FINE, String.format("Inserting redirect to edition %d for %s.", fileOption.getLastInsertEdition(), filename)); - return new RedirectFileEntry(fileOption.getChangedName().or(filename), fileOption.getMimeType(), "SSK@" + project.getRequestURI() + "/" + project.getPath() + "-" + fileOption.getLastInsertEdition() + "/" + fileOption.getLastInsertFilename()); + return Optional.of(new RedirectFileEntry(fileOption.getChangedName().orElse(filename), fileOption.getMimeType(), "SSK@" + project.getRequestURI() + "/" + project.getPath() + "-" + fileOption.getLastInsertEdition() + "/" + fileOption.getLastInsertFilename())); } try { - return createFileEntry(filename, fileOption.getChangedName(), fileOption.getMimeType()); + return Optional.of(createFileEntry(filename, fileOption.getChangedName(), fileOption.getMimeType())); } catch (IOException ioe1) { /* ignore, null is returned. */ } } else { if (fileOption.isInsertRedirect()) { - return new RedirectFileEntry(fileOption.getChangedName().or(filename), fileOption.getMimeType(), fileOption.getCustomKey()); + return Optional.of(new RedirectFileEntry(fileOption.getChangedName().orElse(filename), fileOption.getMimeType(), fileOption.getCustomKey())); } } - return null; + return Optional.empty(); } private FileEntry createFileEntry(String filename, Optional changedName, String mimeType) throws FileNotFoundException { File physicalFile = new File(project.getLocalPath(), filename); InputStream fileEntryInputStream = new FileInputStream(physicalFile); - return new DirectFileEntry(changedName.or(filename), mimeType, fileEntryInputStream, physicalFile.length()); + return new DirectFileEntry(changedName.orElse(filename), mimeType, fileEntryInputStream, physicalFile.length()); } /** @@ -280,7 +280,7 @@ public class ProjectInserter implements FileScannerListener, Runnable { logger.log(Level.FINEST, "Ignoring {0}.", fileOptionEntry.getKey()); continue; } - String fileName = fileOption.getChangedName().or(fileOptionEntry.getKey()); + String fileName = fileOption.getChangedName().orElse(fileOptionEntry.getKey()); logger.log(Level.FINEST, "Adding “{0}” for {1}.", new Object[] { fileName, fileOptionEntry.getKey() }); if (!fileNames.add(fileName)) { checkReport.addIssue("error.duplicate-file", true, fileName); @@ -363,10 +363,10 @@ public class ProjectInserter implements FileScannerListener, Runnable { putDir.setEarlyEncode(useEarlyEncode); putDir.setPriorityClass(priority); for (ScannedFile file : files) { - FileEntry fileEntry = createFileEntry(file); - if (fileEntry != null) { + Optional fileEntry = createFileEntry(file); + if (fileEntry.isPresent()) { try { - putDir.addFileEntry(fileEntry); + putDir.addFileEntry(fileEntry.get()); } catch (IOException ioe1) { projectInsertListeners.fireProjectInsertFinished(project, false, ioe1); return; diff --git a/src/main/java/de/todesbaum/jsite/gui/ProjectFilesPage.java b/src/main/java/de/todesbaum/jsite/gui/ProjectFilesPage.java index 47b79d3..eaae3bd 100644 --- a/src/main/java/de/todesbaum/jsite/gui/ProjectFilesPage.java +++ b/src/main/java/de/todesbaum/jsite/gui/ProjectFilesPage.java @@ -629,7 +629,7 @@ public class ProjectFilesPage extends TWizardPage implements ActionListener, Lis fileOptionsCustomKeyTextField.setText(fileOption.getCustomKey()); fileOptionsRenameCheckBox.setSelected(fileOption.getChangedName().isPresent()); fileOptionsRenameTextField.setEnabled(fileOption.getChangedName().isPresent()); - fileOptionsRenameTextField.setText(fileOption.getChangedName().or("")); + fileOptionsRenameTextField.setText(fileOption.getChangedName().orElse("")); fileOptionsMIMETypeComboBox.getModel().setSelectedItem(fileOption.getMimeType()); } else { defaultFileCheckBox.setSelected(false); diff --git a/src/main/java/de/todesbaum/jsite/main/Configuration.java b/src/main/java/de/todesbaum/jsite/main/Configuration.java index b0c7118..41eafdb 100644 --- a/src/main/java/de/todesbaum/jsite/main/Configuration.java +++ b/src/main/java/de/todesbaum/jsite/main/Configuration.java @@ -433,7 +433,7 @@ public class Configuration { fileOptionNode.append("insert", String.valueOf(fileOption.isInsert())); fileOptionNode.append("insert-redirect", String.valueOf(fileOption.isInsertRedirect())); fileOptionNode.append("custom-key", fileOption.getCustomKey()); - fileOptionNode.append("changed-name", fileOption.getChangedName().orNull()); + fileOptionNode.append("changed-name", fileOption.getChangedName().orElse(null)); fileOptionNode.append("mime-type", fileOption.getMimeType()); } } -- 2.7.4