Add comparators for sorting downloads.
[xudocci.git] / src / main / java / net / pterodactylus / xdcc / data / Download.java
index 2d95537..560e7f9 100644 (file)
 
 package net.pterodactylus.xdcc.data;
 
+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.Predicate;
+
 /**
  * Information about an ongoing download.
  *
@@ -28,6 +36,33 @@ import net.pterodactylus.irc.DccReceiver;
  */
 public class Download {
 
+       /** Predicate that identifies downloads that have started. */
+       public static final Predicate<Download> FILTER_RUNNING = new Predicate<Download>() {
+
+               @Override
+               public boolean apply(Download download) {
+                       return download.dccReceiver() != null;
+               }
+       };
+
+       /** {@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;
+               }
+       };
+
        /** The bot that is being downloaded from. */
        private final Bot bot;
 
@@ -35,13 +70,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 final AtomicLong filesize = new AtomicLong();
+
+       /** The remote address. */
+       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.
@@ -52,8 +93,8 @@ public class Download {
         *              The pack being downloaded
         */
        public Download(Bot bot, Pack pack) {
-               this.bot = bot;
-               this.pack = pack;
+               this.bot = checkNotNull(bot, "bot must not be null");
+               this.pack = checkNotNull(pack, "pack must not be null");
        }
 
        //
@@ -84,7 +125,25 @@ public class Download {
         * @return The full name of the file
         */
        public String filename() {
-               return filename;
+               return filename.get();
+       }
+
+       /**
+        * Returns the size of the file.
+        *
+        * @return The size of the file
+        */
+       public long filesize() {
+               return filesize.get();
+       }
+
+       /**
+        * Returns the remote address to download from.
+        *
+        * @return The remote address to download from
+        */
+       public InetAddress remoteAddress() {
+               return remoteAddress.get();
        }
 
        /**
@@ -93,7 +152,7 @@ public class Download {
         * @return The output stream
         */
        public OutputStream outputStream() {
-               return outputStream;
+               return outputStream.get();
        }
 
        /**
@@ -102,7 +161,7 @@ public class Download {
         * @return The DCC receiver
         */
        public DccReceiver dccReceiver() {
-               return dccReceiver;
+               return dccReceiver.get();
        }
 
        //
@@ -117,7 +176,31 @@ public class Download {
         * @return This download
         */
        public Download filename(String filename) {
-               this.filename = filename;
+               this.filename.set(filename);
+               return this;
+       }
+
+       /**
+        * Sets the size of the download.
+        *
+        * @param filesize
+        *              The size of the download
+        * @return This download
+        */
+       public Download filesize(long filesize) {
+               this.filesize.set(filesize);
+               return this;
+       }
+
+       /**
+        * Sets the remote address of the download.
+        *
+        * @param remoteAddress
+        *              The remote address of the download
+        * @return This download
+        */
+       public Download remoteAddress(InetAddress remoteAddress) {
+               this.remoteAddress.set(remoteAddress);
                return this;
        }
 
@@ -129,7 +212,7 @@ public class Download {
         * @return This download
         */
        public Download outputStream(OutputStream outputStream) {
-               this.outputStream = outputStream;
+               this.outputStream.set(outputStream);
                return this;
        }
 
@@ -141,8 +224,26 @@ public class Download {
         * @return This download
         */
        public Download dccReceiver(DccReceiver dccReceiver) {
-               this.dccReceiver = dccReceiver;
+               this.dccReceiver.set(dccReceiver);
                return this;
        }
 
+       //
+       // OBJECT METHODS
+       //
+
+       @Override
+       public boolean equals(Object object) {
+               if (!(object instanceof Download)) {
+                       return false;
+               }
+               Download download = (Download) object;
+               return bot().equals(download.bot()) && pack().equals(download.pack());
+       }
+
+       @Override
+       public int hashCode() {
+               return bot().hashCode() ^ pack().hashCode();
+       }
+
 }