com.uid2.client.IdentityMapInput Maven / Gradle / Ivy
package com.uid2.client;
import com.google.gson.annotations.SerializedName;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
public class IdentityMapInput {
/**
* @param emails a list of normalized or unnormalized email addresses
* @return a IdentityMapInput instance, to be used in {@link IdentityMapHelper#createEnvelopeForIdentityMapRequest}
*/
public static IdentityMapInput fromEmails(Iterable emails) {
return new IdentityMapInput(IdentityType.Email, emails, false);
}
/**
* @param phones a normalized phone number
* @return an IdentityMapInput instance
*/
public static IdentityMapInput fromPhones(Iterable phones) {
return new IdentityMapInput(IdentityType.Phone, phones, false);
}
/**
* @param hashedEmails a normalized and hashed email address
* @return an IdentityMapInput instance
*/
public static IdentityMapInput fromHashedEmails(Iterable hashedEmails) {
return new IdentityMapInput(IdentityType.Email, hashedEmails, true);
}
/**
* @param hashedPhones a normalized and hashed phone number
* @return an IdentityMapInput instance
*/
public static IdentityMapInput fromHashedPhones(Iterable hashedPhones) {
return new IdentityMapInput(IdentityType.Phone, hashedPhones, true);
}
private IdentityMapInput(IdentityType identityType, Iterable emailsOrPhones, boolean alreadyHashed) {
if (identityType == IdentityType.Email) {
hashedNormalizedEmails = new ArrayList<>();
for (String email : emailsOrPhones) {
if (alreadyHashed) {
hashedNormalizedEmails.add(email);
} else {
String hashedEmail = InputUtil.normalizeAndHashEmail(email);
hashedNormalizedEmails.add(hashedEmail);
addHashedToRawDiiMapping(hashedEmail, email);
}
}
} else { //phone
hashedNormalizedPhones = new ArrayList<>();
for (String phone : emailsOrPhones) {
if (alreadyHashed) {
hashedNormalizedPhones.add(phone);
} else {
if (!InputUtil.isPhoneNumberNormalized(phone)) {
throw new IllegalArgumentException("phone number is not normalized: " + phone);
}
String hashedNormalizedPhone = InputUtil.getBase64EncodedHash(phone);
addHashedToRawDiiMapping(hashedNormalizedPhone, phone);
hashedNormalizedPhones.add(hashedNormalizedPhone);
}
}
}
}
private void addHashedToRawDiiMapping(String hashedDii, String rawDii) {
hashedDiiToRawDiis.computeIfAbsent(hashedDii, k -> new ArrayList<>()).add(rawDii);
}
List getRawDiis(String identifier) {
final boolean wasInputAlreadyHashed = hashedDiiToRawDiis.isEmpty();
if (wasInputAlreadyHashed)
return Collections.singletonList(identifier);
return hashedDiiToRawDiis.get(identifier);
}
@SerializedName("email_hash")
private List hashedNormalizedEmails;
@SerializedName("phone_hash")
private List hashedNormalizedPhones;
private final transient HashMap> hashedDiiToRawDiis = new HashMap<>();
}