Override Object.hashCode() and Object.equals().
[demoscenemusic.git] / src / main / java / net / pterodactylus / demoscenemusic / data / DefaultUser.java
1 /*
2  * DemosceneMusic - DefaultUser.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.io.UnsupportedEncodingException;
21 import java.util.Collection;
22
23 import net.pterodactylus.util.crypto.MessageDigestHasher;
24 import net.pterodactylus.util.number.Hex;
25 import net.pterodactylus.util.object.Default;
26
27 /**
28  * Default implementation of a user data container.
29  *
30  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
31  */
32 public class DefaultUser extends DefaultBase implements User {
33
34         /**
35          * Creates a new user data container.
36          *
37          * @param id
38          *            The ID of the user
39          */
40         public DefaultUser(String id) {
41                 super(id);
42         }
43
44         /**
45          * {@inheritDoc}
46          */
47         @Override
48         public String getName() {
49                 return getValue("name", String.class).get();
50         }
51
52         /**
53          * {@inheritDoc}
54          */
55         @Override
56         public User setName(String name) {
57                 getValue("name", String.class).set(name);
58                 return this;
59         }
60
61         /**
62          * {@inheritDoc}
63          */
64         @Override
65         public User setPassword(String password) {
66                 try {
67                         getValue("password", String.class).set(Hex.toHex(MessageDigestHasher.getSHA256Hasher().hash(password.getBytes("UTF-8"))));
68                         return this;
69                 } catch (UnsupportedEncodingException uee1) {
70                         throw new RuntimeException("VM does not support UTF-8.", uee1);
71                 }
72         }
73
74         /**
75          * {@inheritDoc}
76          */
77         @Override
78         public User setPasswordHash(String passwordHash) {
79                 getValue("password", String.class).set(passwordHash);
80                 return this;
81         }
82
83         /**
84          * {@inheritDoc}
85          */
86         @Override
87         public boolean verifyPassword(String password) {
88                 try {
89                         return getValue("password", String.class).get().equalsIgnoreCase(Hex.toHex(MessageDigestHasher.getSHA256Hasher().hash(password.getBytes("UTF-8"))));
90                 } catch (UnsupportedEncodingException uee1) {
91                         throw new RuntimeException("VM does not support UTF-8.", uee1);
92                 }
93         }
94
95         /**
96          * {@inheritDoc}
97          */
98         @Override
99         public int getLevel() {
100                 return Default.forNull(getValue("level", Integer.class).get(), 0);
101         }
102
103         /**
104          * {@inheritDoc}
105          */
106         @Override
107         public User setLevel(int level) {
108                 getValue("level", Integer.class).set(level);
109                 return this;
110         }
111
112         /**
113          * {@inheritDoc}
114          */
115         @Override
116         @SuppressWarnings("unchecked")
117         public Collection<String> getOpenIds() {
118                 return getValue("openIds", Collection.class).get();
119         }
120
121         /**
122          * {@inheritDoc}
123          */
124         @Override
125         public User setOpenIds(Collection<String> openIds) {
126                 getValue("openIds", Collection.class).set(openIds);
127                 return this;
128         }
129
130         //
131         // OBJECT METHODS
132         //
133
134         /**
135          * {@inheritDoc}
136          */
137         @Override
138         public int hashCode() {
139                 return getId().hashCode();
140         }
141
142         /**
143          * {@inheritDoc}
144          */
145         @Override
146         public boolean equals(Object object) {
147                 if (!(object instanceof User)) {
148                         return false;
149                 }
150                 return ((User) object).getId().equals(getId());
151         }
152
153 }