View Javadoc
1   package com.atlassian.sal.api.user;
2   
3   import javax.annotation.Nonnull;
4   import java.io.Serializable;
5   
6   import static java.util.Objects.requireNonNull;
7   
8   /**
9    * Represents an identifier that uniquely identifies a user for the duration of its existence.
10   *
11   * @since v2.10
12   */
13  public final class UserKey implements Serializable {
14      private static final long serialVersionUID = 1L;
15  
16      private final String userkey;
17  
18      /**
19       * Default constructor: builds a UserKey from its string representation.
20       *
21       * @param userkey the string representation of a UserKey. Must not be null.
22       */
23      public UserKey(@Nonnull final String userkey) {
24          requireNonNull(userkey, "userkey");
25          this.userkey = userkey;
26      }
27  
28      /**
29       * Returns a string representation of the current key.
30       *
31       * @return a string representation of the current key
32       */
33      @Nonnull
34      public String getStringValue() {
35          return userkey;
36      }
37  
38      @Override
39      public String toString() {
40          return getStringValue();
41      }
42  
43      @Override
44      public boolean equals(Object o) {
45          if (this == o) {
46              return true;
47          }
48          if (o == null || getClass() != o.getClass()) {
49              return false;
50          }
51  
52          UserKey userKey = (UserKey) o;
53  
54          if (userkey != null ? !userkey.equals(userKey.userkey) : userKey.userkey != null) {
55              return false;
56          }
57  
58          return true;
59      }
60  
61      @Override
62      public int hashCode() {
63          return userkey != null ? userkey.hashCode() : 0;
64      }
65  
66      //
67      // Factory methods
68      //
69  
70      /**
71       * Builds a {@link UserKey} object from a long id. The id will be converted to a String first.
72       *
73       * @param userId a user ID
74       * @return a {@link UserKey} object
75       */
76      public static UserKey fromLong(long userId) {
77          return new UserKey(String.valueOf(userId));
78      }
79  }