Override Object.hashCode() and Object.equals().
[demoscenemusic.git] / src / main / java / net / pterodactylus / demoscenemusic / data / DefaultParty.java
1 /*
2  * DemosceneMusic - AbstractParty.java - Copyright © 2012 David Roden
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.demoscenemusic.data;
19
20 import java.util.Collection;
21 import java.util.Comparator;
22
23 import net.pterodactylus.util.number.Numbers;
24
25 /**
26  * Default implementation of a party data container.
27  *
28  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
29  */
30 public class DefaultParty extends DefaultBase implements Party {
31
32         /** Comparator that sorts parties by name and descending year. */
33         public static final Comparator<Party> NAME_YEAR_COMPARATOR = new Comparator<Party>() {
34
35                 @Override
36                 public int compare(Party leftParty, Party rightParty) {
37                         int diff = leftParty.getName().compareToIgnoreCase(rightParty.getName());
38                         if (diff != 0) {
39                                 return diff;
40                         }
41                         int leftYear = Numbers.safeParseInteger(leftParty.getProperties().get("party/year"), 0);
42                         int rightYear = Numbers.safeParseInteger(rightParty.getProperties().get("party/year"), 0);
43                         /* show newest parties first. */
44                         return rightYear - leftYear;
45                 }
46
47         };
48
49         /**
50          * Creates a new party data container.
51          *
52          * @param id
53          *            The ID of the party
54          */
55         public DefaultParty(String id) {
56                 super(id);
57         }
58
59         //
60         // PARTY METHODS
61         //
62
63         /**
64          * {@inheritDoc}
65          */
66         @Override
67         public String getName() {
68                 return getValue("name", String.class).get();
69         }
70
71         /**
72          * {@inheritDoc}
73          */
74         @Override
75         public DefaultParty setName(String name) {
76                 getValue("name", String.class).set(name);
77                 return this;
78         }
79
80         /**
81          * {@inheritDoc}
82          */
83         @Override
84         @SuppressWarnings("unchecked")
85         public Collection<Track> getReleases() {
86                 return getValue("releases", Collection.class).get();
87         }
88
89         /**
90          * {@inheritDoc}
91          */
92         @Override
93         public Party setReleases(Collection<Track> tracks) {
94                 getValue("releases", Collection.class).set(tracks);
95                 return this;
96         }
97
98         //
99         // OBJECT METHODS
100         //
101
102         /**
103          * {@inheritDoc}
104          */
105         @Override
106         public int hashCode() {
107                 return getId().hashCode();
108         }
109
110         /**
111          * {@inheritDoc}
112          */
113         @Override
114         public boolean equals(Object object) {
115                 if (!(object instanceof Party)) {
116                         return false;
117                 }
118                 return ((Party) object).getId().equals(getId());
119         }
120
121 }