1 // Copyright 2007 Google Inc. All rights reserved.
3 package com
.google
.appengine
.api
.users
;
5 import java
.io
.Serializable
;
8 * {@code User} represents a specific user, represented by the
9 * combination of an email address and a specific Google Apps domain
10 * (which we call an {@code authDomain}). For normal Google login,
11 * {@code authDomain} will be set to "gmail.com".
14 public final class User
implements Serializable
, Comparable
<User
> {
15 static final long serialVersionUID
= 8691571286358652288L;
19 private String authDomain
;
21 private String userId
;
23 private String federatedIdentity
;
26 * This constructor exists for frameworks (e.g. Google Web Toolkit)
27 * that require it for serialization purposes. It should not be
30 @SuppressWarnings("unused")
37 * @param email a not {@code null} email address.
38 * @param authDomain a not {@code null} domain name into which this
39 * user has authenticated, or "gmail.com" for normal Google
42 public User(String email
, String authDomain
) {
43 this(email
, authDomain
, null);
49 * @param email a not {@code null} email address.
50 * @param authDomain a not {@code null} domain name into which this
51 * user has authenticated, or "gmail.com" for normal Google
53 * @param userId a possibly-null string uniquely identifying the specified user.
55 public User(String email
, String authDomain
, String userId
) {
57 throw new NullPointerException("email must be specified");
59 if (authDomain
== null) {
60 throw new NullPointerException("authDomain must be specified");
63 this.authDomain
= authDomain
;
68 * Creates a new User with a federated identity.
70 * @param email an optional field holding the user's email.
71 * @param authDomain the URL of the identity provider. Could be null.
72 * @param userId a unique id for this user. Could be null.
73 * @param federatedIdentity a not {@code null} asserted federated identity.
75 public User(String email
, String authDomain
, String userId
, String federatedIdentity
) {
76 if (federatedIdentity
== null) {
77 throw new NullPointerException("Identity must be specified");
79 if (authDomain
== null) {
82 this.authDomain
= authDomain
;
90 this.federatedIdentity
= federatedIdentity
;
93 * Return this user's nickname.
95 * The nickname will be a unique, human readable identifier for this user
96 * with respect to this application. It will be an email address for some
99 public String
getNickname() {
100 int indexOfDomain
= email
.indexOf("@" + authDomain
);
101 if (indexOfDomain
== -1) {
104 return email
.substring(0, indexOfDomain
);
107 public String
getAuthDomain() {
111 public String
getEmail() {
116 * Returns an opaque string that uniquely identifies the user
117 * represented by this {@code User} object.
119 * <p>May be null if this {@code User} object was created explicitly
120 * and no user ID was supplied.
122 public String
getUserId() {
126 public String
getFederatedIdentity() {
127 return federatedIdentity
;
131 public String
toString() {
136 public boolean equals(Object object
) {
137 if (!(object
instanceof User
)) {
141 User user
= (User
) object
;
142 if ((federatedIdentity
!= null) && (!federatedIdentity
.isEmpty())) {
143 return user
.federatedIdentity
.equals(federatedIdentity
);
145 return user
.email
.equals(email
) && user
.authDomain
.equals(authDomain
);
149 public int hashCode() {
150 return 17 * email
.hashCode() + authDomain
.hashCode();
153 public int compareTo(User user
) {
154 return email
.compareTo(user
.email
);