View Javadoc

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