Reputation: 455
I am trying to design a system in which user should be unique across different instances.
I have:
I want to generate a unique ID using these three fields so that the ID can be used to find out if the customer is same. Also, the system should generate the same ID if the combination is repeated.
Any suggestions? what could be a good approach in Java.
Upvotes: 0
Views: 4867
Reputation: 89
You can concatenate fName, lName and email and use UUID.nameUUIDFromBytes(concat.getBytes()).toString()
Example:
import java.util.UUID;
public class asd {
public static void main(String args[]) {
String fName = "fName";
String lName = "lName";
String email = "[email protected]";
String concat = fName+lName+email;
String result = UUID.nameUUIDFromBytes(concat.getBytes()).toString();
System.out.println(result);
}
}
Upvotes: 4
Reputation: 4699
Full email should be unique already, so email alone should be good.
If you want to include all then you must be sure that all fields will be included across all instances and concatenate them with some pattern (multiple characters if desired) that will not occur within the fields.
So long as the fields are always separated by this pattern and no user will have the same fields (for all of them) then these ids will always be unique and recreatable.
Eg: email-firstname-lastname
NOTE:
You should be sure that the delimiter will not occur in the fields. Otherwise the fields can bleed into each other.
firstname="billy-bob", lastname="joe"
->
email-billy-bob-joe
would have the same id as
firstname="billy", lastname="bob-joe"
->
email-billy-bob-joe
Upvotes: 4
Reputation: 934
You can use any hash-function (like SHA-1, MD-5 and so on) to produce fixed-size hash as user id from first name, last name and email. I would recommend to make one string from all attributes with some delimiter before calculating hash. And probably to normalize resulted string to upper/lower case is good choice. F.e.
String delimiter = ..;
String email = ..;
String firstName = ..;
String lastName = ..;
String data = email + delimiter + firstName + delimiter + lastName;
String userId = DigestUtils.sha1Hex(data.toLowerCase());
For hash calculation I took Apache Commons Codec in this example.
Upvotes: -1