Override Object.hashCode() and Object.equals().
[demoscenemusic.git] / src / main / java / net / pterodactylus / demoscenemusic / data / DefaultParty.java
index 993d69a..3e0f296 100644 (file)
 
 package net.pterodactylus.demoscenemusic.data;
 
+import java.util.Collection;
+import java.util.Comparator;
+
+import net.pterodactylus.util.number.Numbers;
+
 /**
  * Default implementation of a party data container.
  *
@@ -24,6 +29,23 @@ package net.pterodactylus.demoscenemusic.data;
  */
 public class DefaultParty extends DefaultBase implements Party {
 
+       /** Comparator that sorts parties by name and descending year. */
+       public static final Comparator<Party> NAME_YEAR_COMPARATOR = new Comparator<Party>() {
+
+               @Override
+               public int compare(Party leftParty, Party rightParty) {
+                       int diff = leftParty.getName().compareToIgnoreCase(rightParty.getName());
+                       if (diff != 0) {
+                               return diff;
+                       }
+                       int leftYear = Numbers.safeParseInteger(leftParty.getProperties().get("party/year"), 0);
+                       int rightYear = Numbers.safeParseInteger(rightParty.getProperties().get("party/year"), 0);
+                       /* show newest parties first. */
+                       return rightYear - leftYear;
+               }
+
+       };
+
        /**
         * Creates a new party data container.
         *
@@ -55,4 +77,45 @@ public class DefaultParty extends DefaultBase implements Party {
                return this;
        }
 
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       @SuppressWarnings("unchecked")
+       public Collection<Track> getReleases() {
+               return getValue("releases", Collection.class).get();
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       public Party setReleases(Collection<Track> tracks) {
+               getValue("releases", Collection.class).set(tracks);
+               return this;
+       }
+
+       //
+       // OBJECT METHODS
+       //
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       public int hashCode() {
+               return getId().hashCode();
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       public boolean equals(Object object) {
+               if (!(object instanceof Party)) {
+                       return false;
+               }
+               return ((Party) object).getId().equals(getId());
+       }
+
 }