Move time formatting to command reader; use function to calculate the remaining trans...
[xudocci.git] / src / main / java / net / pterodactylus / xdcc / data / Download.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;