Override Object.hashCode() and Object.equals().
[demoscenemusic.git] / src / main / java / net / pterodactylus / demoscenemusic / data / DefaultUser.java
index 74a65a1..824d54e 100644 (file)
 
 package net.pterodactylus.demoscenemusic.data;
 
+import java.io.UnsupportedEncodingException;
 import java.util.Collection;
 
+import net.pterodactylus.util.crypto.MessageDigestHasher;
+import net.pterodactylus.util.number.Hex;
+import net.pterodactylus.util.object.Default;
+
 /**
  * Default implementation of a user data container.
  *
@@ -57,6 +62,57 @@ public class DefaultUser extends DefaultBase implements User {
         * {@inheritDoc}
         */
        @Override
+       public User setPassword(String password) {
+               try {
+                       getValue("password", String.class).set(Hex.toHex(MessageDigestHasher.getSHA256Hasher().hash(password.getBytes("UTF-8"))));
+                       return this;
+               } catch (UnsupportedEncodingException uee1) {
+                       throw new RuntimeException("VM does not support UTF-8.", uee1);
+               }
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       public User setPasswordHash(String passwordHash) {
+               getValue("password", String.class).set(passwordHash);
+               return this;
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       public boolean verifyPassword(String password) {
+               try {
+                       return getValue("password", String.class).get().equalsIgnoreCase(Hex.toHex(MessageDigestHasher.getSHA256Hasher().hash(password.getBytes("UTF-8"))));
+               } catch (UnsupportedEncodingException uee1) {
+                       throw new RuntimeException("VM does not support UTF-8.", uee1);
+               }
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       public int getLevel() {
+               return Default.forNull(getValue("level", Integer.class).get(), 0);
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       public User setLevel(int level) {
+               getValue("level", Integer.class).set(level);
+               return this;
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
        @SuppressWarnings("unchecked")
        public Collection<String> getOpenIds() {
                return getValue("openIds", Collection.class).get();
@@ -71,4 +127,27 @@ public class DefaultUser extends DefaultBase implements User {
                return this;
        }
 
+       //
+       // OBJECT METHODS
+       //
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       public int hashCode() {
+               return getId().hashCode();
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       public boolean equals(Object object) {
+               if (!(object instanceof User)) {
+                       return false;
+               }
+               return ((User) object).getId().equals(getId());
+       }
+
 }