1.9.30 sync.
[gae.git] / java / src / main / com / google / appengine / api / users / User.java
blob07b093df36ca43d51ceebcfc5185a0f20bac7309
1 // Copyright 2007 Google Inc. All rights reserved.
3 package com.google.appengine.api.users;
5 import java.io.Serializable;
7 /**
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;
17 private String email;
19 private String authDomain;
21 private String userId;
23 private String federatedIdentity;
25 /**
26 * This constructor exists for frameworks (e.g. Google Web Toolkit)
27 * that require it for serialization purposes. It should not be
28 * called explicitly.
30 @SuppressWarnings("unused")
31 private User() {
34 /**
35 * Creates a new User.
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
40 * authentication.
42 public User(String email, String authDomain) {
43 this(email, authDomain, null);
46 /**
47 * Creates a new User.
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
52 * authentication.
53 * @param userId a possibly-null string uniquely identifying the specified user.
55 public User(String email, String authDomain, String userId) {
56 if (email == null) {
57 throw new NullPointerException("email must be specified");
59 if (authDomain == null) {
60 throw new NullPointerException("authDomain must be specified");
62 this.email = email;
63 this.authDomain = authDomain;
64 this.userId = userId;
67 /**
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) {
80 this.authDomain = "";
81 } else {
82 this.authDomain = authDomain;
84 if (userId == null) {
85 this.userId = "";
86 } else {
87 this.userId = userId;
89 this.email = email;
90 this.federatedIdentity = federatedIdentity;
92 /**
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
97 * users, but not all.
99 public String getNickname() {
100 int indexOfDomain = email.indexOf("@" + authDomain);
101 if (indexOfDomain == -1) {
102 return email;
104 return email.substring(0, indexOfDomain);
107 public String getAuthDomain() {
108 return authDomain;
111 public String getEmail() {
112 return email;
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() {
123 return userId;
126 public String getFederatedIdentity() {
127 return federatedIdentity;
130 @Override
131 public String toString() {
132 return email;
135 @Override
136 public boolean equals(Object object) {
137 if (!(object instanceof User)) {
138 return false;
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);
148 @Override
149 public int hashCode() {
150 return 17 * email.hashCode() + authDomain.hashCode();
153 public int compareTo(User user) {
154 return email.compareTo(user.email);