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 926adad..9664f7c 100644 (file)
@@ -21,9 +21,14 @@ import static com.google.common.base.Preconditions.checkNotNull;
 
 import java.io.OutputStream;
 import java.net.InetAddress;
+import java.util.Comparator;
+import java.util.concurrent.atomic.AtomicLong;
+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;
 
 /**
@@ -42,6 +47,40 @@ public class Download {
                }
        };
 
+       /** {@link Comparator} that sorts downloads by their name. */
+       public static final Comparator<Download> BY_NAME = new Comparator<Download>() {
+
+               @Override
+               public int compare(Download leftDownload, Download rightDownload) {
+                       return leftDownload.pack().name().compareToIgnoreCase(rightDownload.pack().name());
+               }
+       };
+
+       /**  {@link Comparator} that sorts running downloads first. */
+       public static final Comparator<Download> BY_RUNNING = new Comparator<Download>() {
+
+               @Override
+               public int compare(Download leftDownload, Download rightDownload) {
+                       return (leftDownload.dccReceiver() != null) ? -1 : (rightDownload.dccReceiver() != null) ? 1 : 0;
+               }
+       };
+
+       /**
+        * 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;
 
@@ -49,19 +88,19 @@ public class Download {
        private final Pack pack;
 
        /** The name of the file being downloaded. */
-       private String filename;
+       private final AtomicReference<String> filename = new AtomicReference<String>();
 
        /** The size of the file being downloaded. */
-       private long filesize;
+       private final AtomicLong filesize = new AtomicLong();
 
        /** The remote address. */
-       private InetAddress remoteAddress;
+       private final AtomicReference<InetAddress> remoteAddress = new AtomicReference<InetAddress>();
 
        /** The output stream. */
-       private OutputStream outputStream;
+       private final AtomicReference<OutputStream> outputStream = new AtomicReference<OutputStream>();
 
        /** The DCC receiver. */
-       private DccReceiver dccReceiver;
+       private final AtomicReference<DccReceiver> dccReceiver = new AtomicReference<DccReceiver>();
 
        /**
         * Creates a new download.
@@ -104,7 +143,7 @@ public class Download {
         * @return The full name of the file
         */
        public String filename() {
-               return filename;
+               return filename.get();
        }
 
        /**
@@ -113,7 +152,7 @@ public class Download {
         * @return The size of the file
         */
        public long filesize() {
-               return filesize;
+               return filesize.get();
        }
 
        /**
@@ -122,7 +161,7 @@ public class Download {
         * @return The remote address to download from
         */
        public InetAddress remoteAddress() {
-               return remoteAddress;
+               return remoteAddress.get();
        }
 
        /**
@@ -131,7 +170,7 @@ public class Download {
         * @return The output stream
         */
        public OutputStream outputStream() {
-               return outputStream;
+               return outputStream.get();
        }
 
        /**
@@ -140,7 +179,7 @@ public class Download {
         * @return The DCC receiver
         */
        public DccReceiver dccReceiver() {
-               return dccReceiver;
+               return dccReceiver.get();
        }
 
        //
@@ -155,7 +194,7 @@ public class Download {
         * @return This download
         */
        public Download filename(String filename) {
-               this.filename = filename;
+               this.filename.set(filename);
                return this;
        }
 
@@ -167,7 +206,7 @@ public class Download {
         * @return This download
         */
        public Download filesize(long filesize) {
-               this.filesize = filesize;
+               this.filesize.set(filesize);
                return this;
        }
 
@@ -179,7 +218,7 @@ public class Download {
         * @return This download
         */
        public Download remoteAddress(InetAddress remoteAddress) {
-               this.remoteAddress = remoteAddress;
+               this.remoteAddress.set(remoteAddress);
                return this;
        }
 
@@ -191,7 +230,7 @@ public class Download {
         * @return This download
         */
        public Download outputStream(OutputStream outputStream) {
-               this.outputStream = outputStream;
+               this.outputStream.set(outputStream);
                return this;
        }
 
@@ -203,7 +242,7 @@ public class Download {
         * @return This download
         */
        public Download dccReceiver(DccReceiver dccReceiver) {
-               this.dccReceiver = dccReceiver;
+               this.dccReceiver.set(dccReceiver);
                return this;
        }