001 package org.nakedobjects.applib.value;
002
003 import java.io.Serializable;
004
005 import org.nakedobjects.applib.annotation.Value;
006
007
008 @Value(semanticsProviderName = "org.nakedobjects.metamodel.value.PasswordValueSemanticsProvider")
009 public class Password implements Serializable {
010 private static final long serialVersionUID = 1L;
011 private static final String STARS = "********************";
012 private final String password;
013
014 public Password(final String password) {
015 this.password = password;
016 }
017
018 public boolean checkPassword(final String password) {
019 return this.password.equals(password);
020 }
021
022 public String getPassword() {
023 return password;
024 }
025
026 @Override
027 public boolean equals(final Object other) {
028 if (other == this) {
029 return true;
030 }
031 if (other == null) {
032 return false;
033 }
034 return other.getClass() == this.getClass() && equals((Password) other);
035 }
036
037 public boolean equals(final Password other) {
038 final String otherPassword = other.getPassword();
039 if (getPassword() == null && otherPassword == null) {
040 return true;
041 }
042 if (getPassword() == null || otherPassword == null) {
043 return false;
044 }
045 return getPassword().equals(otherPassword);
046 }
047
048 @Override
049 public int hashCode() {
050 return password != null ? password.hashCode() : 0;
051 }
052
053 @Override
054 public String toString() {
055 return STARS.substring(0, Math.min(STARS.length(), password.length()));
056 }
057 }
058 // Copyright (c) Naked Objects Group Ltd.