Use JDK’s Optional instead of Guava’s
authorDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Sun, 31 Jan 2016 04:23:55 +0000 (05:23 +0100)
committerDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Sun, 31 Jan 2016 04:23:55 +0000 (05:23 +0100)
src/main/java/de/todesbaum/jsite/application/FileOption.java
src/main/java/de/todesbaum/jsite/application/Project.java
src/main/java/de/todesbaum/jsite/application/ProjectInserter.java
src/main/java/de/todesbaum/jsite/gui/ProjectFilesPage.java
src/main/java/de/todesbaum/jsite/main/Configuration.java

index 66efc09..8509257 100644 (file)
 
 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<String> changedName = absent();
+       private Optional<String> 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.<String>absent();
+               this.changedName = ((changedName != null) && (changedName.length() > 0)) ? Optional.of(changedName) : Optional.<String>empty();
        }
 
        /**
index d9146a1..aa0c00b 100644 (file)
@@ -462,7 +462,7 @@ public class Project implements Comparable<Project> {
                        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);
                }
index f20c3d0..53b0726 100644 (file)
@@ -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<FileEntry> 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<String> 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> fileEntry = createFileEntry(file);
+                       if (fileEntry.isPresent()) {
                                try {
-                                       putDir.addFileEntry(fileEntry);
+                                       putDir.addFileEntry(fileEntry.get());
                                } catch (IOException ioe1) {
                                        projectInsertListeners.fireProjectInsertFinished(project, false, ioe1);
                                        return;
index 47b79d3..eaae3bd 100644 (file)
@@ -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);
index b0c7118..41eafdb 100644 (file)
@@ -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());
                                }
                        }