View Javadoc

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