001package com.randomnoun.common.security;
002
003/* (c) 2013 randomnoun. All Rights Reserved. This work is licensed under a
004 * BSD Simplified License. (http://www.randomnoun.com/bsd-simplified.html)
005 */
006
007import java.io.*;
008import java.util.Locale;
009
010import com.randomnoun.common.security.User;
011
012/**
013 * A class encapsulating information to define an end-user. 
014 *
015 * @author knoxg
016 */
017public class User
018    implements Serializable
019{
020    
021    /** generated serialVersionUID */
022        private static final long serialVersionUID = 3633994518788000533L;
023
024        /** The username for this user; e.g. knoxg . */
025    private String username;
026    
027    /** The customerId for this user. */
028    private long customerId;
029    
030    /** The userId for this user. */
031    private long userId = -1;
032
033    /** The locale for this user. */
034    private Locale locale; // used for language translation
035
036    // default public constructor
037    public User() {
038    
039    }
040    
041    /** Sets the username that partly identifies this user. (Within a realm, both username
042     *   and customerId must be used to uniquely identify a user).
043     *   
044     *  @param value the username for this user
045     */
046    public void setUsername(String username)
047    {
048        this.username = username;
049    }
050
051    /** Returns the username partly identifying this user. (Within a realm, both username
052     *   and customerId must be used to uniquely identify a user).
053     *   
054     *  @return the username for this user.
055     */
056    public String getUsername()
057    {
058        return username;
059    }
060
061    /** Sets the customerId that partly identifies this user. (Both username
062     *   and customerId must be used to uniquely identify a user).
063     *  @param value the customerId for this user
064     */
065    public void setCustomerId(long customerId)
066    {
067        this.customerId = customerId;
068    }
069
070    /** Returns the customerId partly identifying this user. (Both username
071     *   and customerId must be used to uniquely identify a user).
072     *  @return the customerId for this user.
073     */
074    public long getCustomerId()
075    {
076        return customerId;
077    }
078
079    /** Sets the locale for this user. The locale is used to localise text
080     *  that will be sent to the user.
081     *  @param value the locale for this user
082     */
083    public void setLocale(Locale locale)
084    {
085        this.locale = locale;
086    }
087
088    /** Returns the locale for this user.
089     *  @return the locale for this user.
090     */
091    public Locale getLocale()
092    {
093        return locale;
094    }
095
096    /** Sets the userId for this user 
097     * 
098     * @param userId the userId for this user
099     */
100    public void setUserId(long userId)
101    {
102        this.userId = userId;
103    }
104
105    /** Returns the userId for this user 
106     * @return the userId the userId for this user
107     */
108    public long getUserId()
109    {
110        return userId;
111    }
112
113
114    /** Returns a string representation of this user (used for debugging only).
115     *  This representation includes the username, customerId, roles and permissions
116     *  stored for this user
117     *
118     *  @return a string representation of this user
119     */
120    public String toString()
121    {
122        return "username='" + username + "', customerId=" + customerId;
123    }
124
125    /** Two users are considered identical if their customerIds and usernames are identical
126     *
127     * @param  object the other object used in the comparison
128     * @return true if the users are considered identical
129     */
130    public boolean equals(Object object)
131    {
132        if (object == null)
133        {
134            return false;
135        }
136
137        if (!(object instanceof User))
138        {
139            return false;
140        }
141
142        User otherUser = (User)object;
143
144        return ((this.customerId == otherUser.getCustomerId()) &&
145        (this.username.equals(otherUser.getUsername())));
146    }
147
148    /** Must override hasCode() if we override equals().
149     *
150     * @return a hash which will return identical values for identical users
151     *   (as determined by .equals())
152     */
153    public int hashCode()
154    {
155        // hmm. surprised that this worked in the past.
156        // return System.identityHashCode(username + "\u0000" + customerId);
157        return new String(username + "\u0000" + customerId).hashCode();
158    }
159}