View Javadoc
1   package com.randomnoun.common.security;
2   
3   /* (c) 2013 randomnoun. All Rights Reserved. This work is licensed under a
4    * BSD Simplified License. (http://www.randomnoun.com/bsd-simplified.html)
5    */
6   
7   import java.io.*;
8   import java.util.Locale;
9   
10  import com.randomnoun.common.security.User;
11  
12  /**
13   * A class encapsulating information to define an end-user. 
14   *
15   * @author knoxg
16   */
17  public class User
18      implements Serializable
19  {
20      
21      /** generated serialVersionUID */
22  	private static final long serialVersionUID = 3633994518788000533L;
23  
24  	/** The username for this user; e.g. knoxg . */
25      private String username;
26      
27      /** The customerId for this user. */
28      private long customerId;
29      
30      /** The userId for this user. */
31      private long userId = -1;
32  
33      /** The locale for this user. */
34      private Locale locale; // used for language translation
35  
36      // default public constructor
37      public User() {
38      
39      }
40      
41      /** Sets the username that partly identifies this user. (Within a realm, both username
42       *   and customerId must be used to uniquely identify a user).
43       *   
44       *  @param value the username for this user
45       */
46      public void setUsername(String username)
47      {
48          this.username = username;
49      }
50  
51      /** Returns the username partly identifying this user. (Within a realm, both username
52       *   and customerId must be used to uniquely identify a user).
53       *   
54       *  @return the username for this user.
55       */
56      public String getUsername()
57      {
58          return username;
59      }
60  
61      /** Sets the customerId that partly identifies this user. (Both username
62       *   and customerId must be used to uniquely identify a user).
63       *  @param value the customerId for this user
64       */
65      public void setCustomerId(long customerId)
66      {
67          this.customerId = customerId;
68      }
69  
70      /** Returns the customerId partly identifying this user. (Both username
71       *   and customerId must be used to uniquely identify a user).
72       *  @return the customerId for this user.
73       */
74      public long getCustomerId()
75      {
76          return customerId;
77      }
78  
79      /** Sets the locale for this user. The locale is used to localise text
80       *  that will be sent to the user.
81       *  @param value the locale for this user
82       */
83      public void setLocale(Locale locale)
84      {
85          this.locale = locale;
86      }
87  
88      /** Returns the locale for this user.
89       *  @return the locale for this user.
90       */
91      public Locale getLocale()
92      {
93          return locale;
94      }
95  
96      /** Sets the userId for this user 
97       * 
98       * @param userId the userId for this user
99       */
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 }