Move time formatting to command reader; use function to calculate the remaining trans...
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 6 Sep 2013 23:46:38 +0000 (01:46 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 6 Sep 2013 23:46:38 +0000 (01:46 +0200)
src/main/java/net/pterodactylus/xdcc/data/Download.java
src/main/java/net/pterodactylus/xdcc/ui/stdin/CommandReader.java
src/main/java/net/pterodactylus/xdcc/ui/stdin/ListDownloadsCommand.java

index 560e7f9..9664f7c 100644 (file)
@@ -27,6 +27,8 @@ import java.util.concurrent.atomic.AtomicReference;
 
 import net.pterodactylus.irc.DccReceiver;
 
+import com.google.common.base.Function;
+import com.google.common.base.Optional;
 import com.google.common.base.Predicate;
 
 /**
@@ -63,6 +65,22 @@ public class Download {
                }
        };
 
+       /**
+        * Converts a download into the number of seconds left in the transfer at the
+        * current rate.
+        */
+       public static final Function<Download, Optional<Long>> SECONDS_LEFT = new Function<Download, Optional<Long>>() {
+               @Override
+               public Optional<Long> apply(Download download) {
+                       DccReceiver dccReceiver = download.dccReceiver();
+                       if ((dccReceiver == null) || (dccReceiver.size() == -1) || (dccReceiver.currentRate() == 0)) {
+                               return Optional.absent();
+                       }
+                       long secondsLeft = (dccReceiver.size() - dccReceiver.progress()) / dccReceiver.currentRate();
+                       return Optional.of(secondsLeft);
+               }
+       };
+
        /** The bot that is being downloaded from. */
        private final Bot bot;
 
index 46b22ba..01d0051 100644 (file)
@@ -38,6 +38,7 @@ import net.pterodactylus.xdcc.core.event.MessageReceived;
 import net.pterodactylus.xdcc.data.Download;
 
 import com.google.common.base.Joiner;
+import com.google.common.base.Optional;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableSet;
 import com.google.common.eventbus.Subscribe;
@@ -251,4 +252,21 @@ public class CommandReader extends AbstractExecutionThreadService {
                return String.format("%dB", number);
        }
 
+       /**
+        * Formats the given number of seconds into a more easily readable string.
+        *
+        * @param seconds
+        *              The number of seconds
+        * @return The formatted time, or “unknown” if the time is unknown
+        */
+       static String t(Optional<Long> seconds) {
+               if (!seconds.isPresent()) {
+                       return "unknown";
+               }
+               if (seconds.get() > 3600) {
+                       return String.format("%02d:%02d:%02d", seconds.get() / 3600, (seconds.get() / 60) % 60, seconds.get() % 60);
+               }
+               return String.format("%02d:%02d", (seconds.get() / 60) % 60, seconds.get() % 60);
+       }
+
 }
index 753b677..8a19dff 100644 (file)
@@ -22,7 +22,9 @@ import static com.google.common.collect.Lists.newArrayList;
 import static java.util.Arrays.asList;
 import static net.pterodactylus.xdcc.data.Download.BY_NAME;
 import static net.pterodactylus.xdcc.data.Download.BY_RUNNING;
+import static net.pterodactylus.xdcc.data.Download.SECONDS_LEFT;
 import static net.pterodactylus.xdcc.ui.stdin.CommandReader.f;
+import static net.pterodactylus.xdcc.ui.stdin.CommandReader.t;
 
 import java.io.IOException;
 import java.io.Writer;
@@ -82,7 +84,7 @@ public class ListDownloadsCommand implements Command {
                        }
                        outputWriter.write(String.format("[%d] %s from %s (%s, ", counter++, dccReceiver.filename(), download.bot().name(), f(dccReceiver.size())));
                        if (dccReceiver.isRunning()) {
-                               outputWriter.write(String.format("%.1f%%, %s/s, %s", dccReceiver.progress() * 100.0 / dccReceiver.size(), f(dccReceiver.currentRate()), getTimeLeft(dccReceiver)));
+                               outputWriter.write(String.format("%.1f%%, %s/s, %s", dccReceiver.progress() * 100.0 / dccReceiver.size(), f(dccReceiver.currentRate()), t(SECONDS_LEFT.apply(download))));
                        } else {
                                if (dccReceiver.progress() >= dccReceiver.size()) {
                                        outputWriter.write(String.format("complete, %s/s", f(dccReceiver.overallRate())));
@@ -97,27 +99,4 @@ public class ListDownloadsCommand implements Command {
                return state.setLastDownloads(downloads);
        }
 
-       //
-       // PRIVATE METHODS
-       //
-
-       /**
-        * Returns the estimated time left for the given transfer.
-        *
-        * @param dccReceiver
-        *              The DCC receiver to get the time left for
-        * @return The time left for the transfer, or “unknown” if the time can not be
-        *         estimated
-        */
-       private static String getTimeLeft(DccReceiver dccReceiver) {
-               if ((dccReceiver.size() == -1) || (dccReceiver.currentRate() == 0)) {
-                       return "unknown";
-               }
-               long secondsLeft = (dccReceiver.size() - dccReceiver.progress()) / dccReceiver.currentRate();
-               if (secondsLeft > 3600) {
-                       return String.format("%02d:%02d:%02d", secondsLeft / 3600, (secondsLeft / 60) % 60, secondsLeft % 60);
-               }
-               return String.format("%02d:%02d", (secondsLeft / 60) % 60, secondsLeft % 60);
-       }
-
 }