Advertisement
Guest User

Untitled

a guest
Oct 15th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.08 KB | None | 0 0
  1. package model;
  2.  
  3. import java.util.HashSet;
  4.  
  5. import model.exceptions.UserException;
  6.  
  7. public final class User {
  8. // object data
  9. private long userId = 0;
  10. private String username = null;
  11. private String password = null;
  12. private long profilePicId = 0; // default profile pic id must be 0
  13. private String description = "";
  14. private HashSet<User> followers = null;
  15. private HashSet<User> following = null;
  16. private HashSet<VisitedLocation> visitedLocations = null;
  17. private HashSet<Location> wishlist = null;
  18. private HashSet<Post> posts = null;
  19.  
  20. // constants
  21. private static final int MIN_USERNAME_LENGTH = 5;
  22. private static final int MAX_USERNAME_LENGTH = 45;
  23. private static final int MIN_PASSWORD_LENGTH = 6;
  24. private static final int MAX_PASSWORD_LENGTH = 255;
  25.  
  26. // constructor to be used when registering new user
  27. public User(String username, String password) throws UserException {
  28. this.setUsername(username);
  29. this.setPassword(password);
  30. }
  31.  
  32. // constructor to be used when loading an existing user from db
  33. public User(long userId, String username, String password, long profilePicId, String description, HashSet<User> followers, HashSet<User> following, HashSet<VisitedLocation> visitedLocations) throws UserException {
  34. this(username, password);
  35. this.userId = userId;
  36. this.setProfilePicId(profilePicId);
  37. this.setDescription(description);
  38. this.followers = followers;
  39. this.following = following;
  40. this.visitedLocations = visitedLocations;
  41. }
  42.  
  43. //accessors
  44. public long getUserId() {
  45. return this.userId;
  46. }
  47.  
  48. public String getUsername() {
  49. return this.username;
  50. }
  51.  
  52. public String getPassword() {
  53. return this.password;
  54. }
  55.  
  56. public long getProfilePicId() {
  57. return this.profilePicId;
  58. }
  59.  
  60. public String getDescription() {
  61. return this.description;
  62. }
  63.  
  64. public HashSet<User> getFollowers() {
  65. return this.followers;
  66. }
  67.  
  68. public HashSet<User> getFollowing() {
  69. return this.following;
  70. }
  71.  
  72. public HashSet<VisitedLocation> getVisitedLocations() {
  73. return this.visitedLocations;
  74. }
  75.  
  76. //mutators
  77. public void setUserId(long userId) {
  78. this.userId = userId;
  79. }
  80.  
  81. public void setUsername(String username) throws UserException {
  82. if (username.length() >= MIN_USERNAME_LENGTH && username.matches("^(?=\\S+$)$")) {
  83. if (this.username.length() <= MAX_USERNAME_LENGTH) {
  84. this.username = username;
  85. } else {
  86. throw new UserException("Username too long!");
  87. }
  88. } else {
  89. throw new UserException(
  90. "Username must be at least " + " characters long and must not contain any whitespace characters!");
  91. }
  92. }
  93.  
  94. /**
  95. * public void setPassword(String password) throws UserException { if
  96. * (password.length() >= MIN_PASWORD_LENGTH) { if
  97. * (password.matches("^(?=.*[a-z])(?=.*[A-Z])$")) { if
  98. * (password.matches("^(?=.*[0-9])$") || password.matches("^(?=.*[@#$%^&+=])$"))
  99. * { this.password = password; // hashing required } else { throw new
  100. * UserException("Password must contain at least one non-alphabetic character!"
  101. * ); } } else { throw new UserException(
  102. * "Password must contain at least one uppercase and at least one lowercase character!"
  103. * ); } } else { throw new UserException("Password must be at least " +
  104. * MIN_PASWORD_LENGTH + " characters long!"); } }
  105. **/
  106. public void setPassword(String password) throws UserException {
  107. if (password.length() >= MIN_PASSWORD_LENGTH && (password.matches("^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])$")
  108. || password.matches("^(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=])$"))) {
  109. if (password.length() <= MAX_PASSWORD_LENGTH) {
  110. this.password = password; // hashing required
  111. } else {
  112. throw new UserException("Password too long!");
  113. }
  114. } else {
  115. throw new UserException("Password must be at least " + MIN_PASSWORD_LENGTH
  116. + " characters long and must contain at least one lowercase character, at least one uppercase character and at least one non-alphabetic character!");
  117. }
  118. }
  119.  
  120. public void setProfilePicId(long profilePicId) {
  121. this.profilePicId = profilePicId;
  122. }
  123.  
  124. public void setDescription(String description) {
  125. this.description = description != null ? description : "";
  126. }
  127.  
  128. public void setFollowers(HashSet<User> followers) {
  129. this.followers = followers;
  130. }
  131.  
  132. public void setFollowing(HashSet<User> following) {
  133. this.following = following;
  134. }
  135.  
  136. public void setVisitedLocations(HashSet<VisitedLocation> visitedLocations) {
  137. this.visitedLocations = visitedLocations;
  138. }
  139.  
  140. @Override
  141. public int hashCode() {
  142. final int prime = 31;
  143. int result = 1;
  144. result = prime * result + (int) (userId ^ (userId >>> 32));
  145. result = prime * result + ((username == null) ? 0 : username.hashCode());
  146. return result;
  147. }
  148.  
  149. @Override
  150. public boolean equals(Object obj) {
  151. if (this == obj)
  152. return true;
  153. if (obj == null)
  154. return false;
  155. if (getClass() != obj.getClass())
  156. return false;
  157. User other = (User) obj;
  158. if (userId != other.userId)
  159. return false;
  160. if (username == null) {
  161. if (other.username != null)
  162. return false;
  163. } else if (!username.equals(other.username))
  164. return false;
  165. return true;
  166. }
  167.  
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement