|
| 1 | +package com.uid2.client; |
| 2 | + |
| 3 | +import com.google.gson.annotations.SerializedName; |
| 4 | + |
| 5 | +import java.util.*; |
| 6 | + |
| 7 | +public class IdentityMapV3Input { |
| 8 | + /** |
| 9 | + * @param emails a list of normalized or unnormalized email addresses |
| 10 | + * @return a IdentityMapV3Input instance, to be used in {@link IdentityMapV3Helper#createEnvelopeForIdentityMapRequest} |
| 11 | + */ |
| 12 | + public static IdentityMapV3Input fromEmails(List<String> emails) { |
| 13 | + return new IdentityMapV3Input().withEmails(emails); |
| 14 | + } |
| 15 | + |
| 16 | + /** |
| 17 | + * @param hashedEmails a <a href="https://unifiedid.com/docs/getting-started/gs-normalization-encoding#email-address-normalization">normalized</a> and <a href="https://unifiedid.com/docs/getting-started/gs-normalization-encoding#email-address-hash-encoding">hashed</a> email address |
| 18 | + * @return an IdentityMapV3Input instance |
| 19 | + */ |
| 20 | + public static IdentityMapV3Input fromHashedEmails(List<String> hashedEmails) { |
| 21 | + return new IdentityMapV3Input().withHashedEmails(hashedEmails); |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * @param phones a <a href="https://unifiedid.com/docs/getting-started/gs-normalization-encoding#phone-number-normalization">normalized</a> phone number |
| 26 | + * @return an IdentityMapV3Input instance |
| 27 | + */ |
| 28 | + public static IdentityMapV3Input fromPhones(List<String> phones) { |
| 29 | + return new IdentityMapV3Input().withPhones(phones); |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * @param hashedPhones a <a href="https://unifiedid.com/docs/getting-started/gs-normalization-encoding#phone-number-normalization">normalized</a> and <a href="https://unifiedid.com/docs/getting-started/gs-normalization-encoding#phone-number-hash-encoding">hashed</a> phone number |
| 34 | + * @return an IdentityMapV3Input instance |
| 35 | + */ |
| 36 | + public static IdentityMapV3Input fromHashedPhones(List<String> hashedPhones) { |
| 37 | + return new IdentityMapV3Input().withHashedPhones(hashedPhones); |
| 38 | + } |
| 39 | + |
| 40 | + // Transient as this should not be part of the serialized JSON payload we send to UID2 Operator |
| 41 | + private transient final Map<String, List<String>> hashedDiiToRawDii = new HashMap<>(); |
| 42 | + |
| 43 | + @SerializedName("email_hash") |
| 44 | + private final List<String> hashedEmails = new ArrayList<>(); |
| 45 | + |
| 46 | + @SerializedName("phone_hash") |
| 47 | + private final List<String> hashedPhones = new ArrayList<>(); |
| 48 | + |
| 49 | + // We never send unhashed emails or phone numbers in the SDK, but they are required fields in the API request |
| 50 | + @SerializedName("email") |
| 51 | + private List<String> emails = Collections.unmodifiableList(new ArrayList<>()); |
| 52 | + @SerializedName("phone") |
| 53 | + private List<String> phones = Collections.unmodifiableList(new ArrayList<>()); |
| 54 | + |
| 55 | + public IdentityMapV3Input() {} |
| 56 | + |
| 57 | + /** |
| 58 | + * @param hashedEmails a <a href="https://unifiedid.com/docs/getting-started/gs-normalization-encoding#email-address-normalization">normalized</a> and <a href="https://unifiedid.com/docs/getting-started/gs-normalization-encoding#email-address-hash-encoding">hashed</a> email address |
| 59 | + * @return this IdentityMapV3Input instance |
| 60 | + */ |
| 61 | + public IdentityMapV3Input withHashedEmails(List<String> hashedEmails) { |
| 62 | + for (String hashedEmail : hashedEmails) { |
| 63 | + withHashedEmail(hashedEmail); |
| 64 | + } |
| 65 | + return this; |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * @param hashedEmail a <a href="https://unifiedid.com/docs/getting-started/gs-normalization-encoding#email-address-normalization">normalized</a> and <a href="https://unifiedid.com/docs/getting-started/gs-normalization-encoding#email-address-hash-encoding">hashed</a> email address |
| 70 | + * @return this IdentityMapV3Input instance |
| 71 | + */ |
| 72 | + public IdentityMapV3Input withHashedEmail(String hashedEmail) { |
| 73 | + this.hashedEmails.add(hashedEmail); |
| 74 | + addToDiiMappings(hashedEmail, hashedEmail); |
| 75 | + return this; |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * @param hashedPhones a <a href="https://unifiedid.com/docs/getting-started/gs-normalization-encoding#phone-number-normalization">normalized</a> and <a href="https://unifiedid.com/docs/getting-started/gs-normalization-encoding#phone-number-hash-encoding">hashed</a> phone number |
| 80 | + * @return this IdentityMapV3Input instance |
| 81 | + */ |
| 82 | + public IdentityMapV3Input withHashedPhones(List<String> hashedPhones) { |
| 83 | + for (String hashedPhone : hashedPhones) { |
| 84 | + withHashedPhone(hashedPhone); |
| 85 | + } |
| 86 | + return this; |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * @param hashedPhone a <a href="https://unifiedid.com/docs/getting-started/gs-normalization-encoding#phone-number-normalization">normalized</a> and <a href="https://unifiedid.com/docs/getting-started/gs-normalization-encoding#phone-number-hash-encoding">hashed</a> phone number |
| 91 | + * @return this IdentityMapV3Input instance |
| 92 | + */ |
| 93 | + public IdentityMapV3Input withHashedPhone(String hashedPhone) { |
| 94 | + this.hashedPhones.add(hashedPhone); |
| 95 | + addToDiiMappings(hashedPhone, hashedPhone); |
| 96 | + return this; |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * @param emails a list of normalized or unnormalized email addresses |
| 101 | + * @return this IdentityMapV3Input instance |
| 102 | + */ |
| 103 | + public IdentityMapV3Input withEmails(List<String> emails) { |
| 104 | + for (String email : emails) { |
| 105 | + withEmail(email); |
| 106 | + } |
| 107 | + return this; |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * @param email a normalized or unnormalized email address |
| 112 | + * @return this IdentityMapV3Input instance |
| 113 | + */ |
| 114 | + public IdentityMapV3Input withEmail(String email) { |
| 115 | + String hashedEmail = InputUtil.normalizeAndHashEmail(email); |
| 116 | + this.hashedEmails.add(hashedEmail); |
| 117 | + addToDiiMappings(hashedEmail, email); |
| 118 | + return this; |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * @param phones a <a href="https://unifiedid.com/docs/getting-started/gs-normalization-encoding#phone-number-normalization">normalized</a> phone number |
| 123 | + * @return this IdentityMapV3Input instance |
| 124 | + */ |
| 125 | + public IdentityMapV3Input withPhones(List<String> phones) { |
| 126 | + for (String phone : phones) { |
| 127 | + withPhone(phone); |
| 128 | + } |
| 129 | + return this; |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * @param phone a <a href="https://unifiedid.com/docs/getting-started/gs-normalization-encoding#phone-number-normalization">normalized</a> phone number |
| 134 | + * @return this IdentityMapV3Input instance |
| 135 | + */ |
| 136 | + public IdentityMapV3Input withPhone(String phone) { |
| 137 | + if (!InputUtil.isPhoneNumberNormalized(phone)) { |
| 138 | + throw new IllegalArgumentException("phone number is not normalized: " + phone); |
| 139 | + } |
| 140 | + |
| 141 | + String hashedPhone = InputUtil.getBase64EncodedHash(phone); |
| 142 | + this.hashedPhones.add(hashedPhone); |
| 143 | + addToDiiMappings(hashedPhone, phone); |
| 144 | + return this; |
| 145 | + } |
| 146 | + |
| 147 | + List<String> getInputDiis(String identityType, int i) { |
| 148 | + return hashedDiiToRawDii.get(getHashedDii(identityType, i)); |
| 149 | + } |
| 150 | + |
| 151 | + private void addToDiiMappings(String hashedDii, String rawDii) { |
| 152 | + hashedDiiToRawDii.computeIfAbsent(hashedDii, k -> new ArrayList<>()).add(rawDii); |
| 153 | + } |
| 154 | + |
| 155 | + private String getHashedDii(String identityType, int i) { |
| 156 | + switch (identityType) { |
| 157 | + case "email_hash": return hashedEmails.get(i); |
| 158 | + case "phone_hash": return hashedPhones.get(i); |
| 159 | + } |
| 160 | + throw new Uid2Exception("Unexpected identity type: " + identityType); |
| 161 | + } |
| 162 | +} |
0 commit comments