com.tinypass.client.common.UserRefBuilder Maven / Gradle / Ivy
package com.tinypass.client.common;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
public class UserRefBuilder {
private static final String FIRST_NAME = "first_name";
private static final String LAST_NAME = "last_name";
private static final String UID = "uid";
private static final String EMAIL = "email";
public static final String TIMESTAMP = "timestamp";
public static final String CREATE_DATE = "create_date";
private Map data = new HashMap();
/**
* Instantiates a new UserRefBuilder; Typically you'll want to use the
* static {@link UserRefBuilder.create()} method
*
* @param uid The user's globally unique id, typically the numeric auto-incremented identifier
* @param email The user's email; needs to be globally unique
*/
public UserRefBuilder(String uid, String email) {
data.put(UID, uid);
data.put(EMAIL, email);
}
/**
* Creates a new UserRefBuilder
*
* @param uid The user's globally unique id, typically the numeric auto-incremented identifier
* @param email The user's email; needs to be globally unique
* @return A new {@link UserRefBuilder}
*/
public static UserRefBuilder create(String uid, String email) {
return new UserRefBuilder(uid, email);
}
/**
* Sets the (optional) first name for the user.
*
* @param firstName The user's first name
* @return The {@link UserRefBuilder}
*/
public UserRefBuilder setFirstName(String firstName) {
data.put(FIRST_NAME, firstName);
return this;
}
/**
* Sets the (optional) last name for the user.
*
* @param lastName The user's last name
* @return The {@link UserRefBuilder}
*/
public UserRefBuilder setLastName(String lastName) {
data.put(LAST_NAME, lastName);
return this;
}
/**
* Sets the (optional) create_date
*
* @param date The date created
* @return The {@link UserRefBuilder}
*/
public UserRefBuilder setCreateDate(Date date) {
data.put(CREATE_DATE, String.valueOf(date.getTime() / 1000L));
return this;
}
/**
* Generically set other properties on the user ref object
*
* @param key The key to set
* @param value The value to set
* @return The {@link UserRefBuilder}
*/
public UserRefBuilder set(String key, String value) {
data.put(key, value);
return this;
}
/**
* Builds the encrypted user ref string.
*
* @param privateKey The private key to use when encrypting the user ref
* @return The user ref
* @throws Exception
*/
public String build(String privateKey) throws Exception {
data.put(TIMESTAMP, ""+ (int) (System.currentTimeMillis() / 1000L));
return SecurityUtil.encrypt(privateKey, JsonParser.getInstance().serialize(data));
}
}