I am looking to hide an email id name, by only showing first 4 letters of the email. The code works fine. Looking for opinion if I could make it better, more readable and maybe even more efficient if possible. I am using Commons lang library for this and wondering if that is an unnecessary overhead.
Please look for the comment <-- HERE -->
. I am looking for opinion in this region. I am using Java 7, no way around that.
import org.apache.commons.lang.StringUtils;
import java.util.HashMap;
import java.util.Map;
public class App {
public static void main(String[] args) {
//all these prints out as expected.
String newEmail1 = getEmail("sample1");
System.out.println("Expected: some**@gmail.com | actual: " + newEmail1);
String newEmail2 = getEmail("sample2");
System.out.println("Expected: null | actual: " + newEmail2);
String newEmail3 = getEmail("short");
System.out.println("Expected: q**@gmail.com | actual: " + newEmail3);
String newEmail4 = getEmail("noname");
System.out.println("Expected: null | actual: " + newEmail4);
}
private static String getEmail(String param){
//added these solely for testing to show
Map<String, String> emails = new HashMap<>();
emails.put("sample1", "[email protected]");
emails.put("short", "[email protected]");
emails.put("noname", "@gmail.com");
//added these solely for testing to show
//I am looking for opinions from here onwards. <-- HERE -->
String emailAddress = emails.get(param);
if(emailAddress != null){
String emailAddressFront = StringUtils.substringBefore(emailAddress, "@");
String emailAddressBack = StringUtils.substringAfter(emailAddress, "@");
// i need to hide emails by only revealing first 4 characters.
// but also need to check in case email name is shorter than 4.
if(emailAddressFront.isEmpty()){
return null;
}
int shortenedLength = 4;
int emailNameLength = emailAddressFront.length();
if(emailNameLength < 4){
shortenedLength = emailNameLength - 1;
}
emailAddress = emailAddressFront.substring(0, shortenedLength) + "**@" + emailAddressBack;
}
return emailAddress;
//I am looking for opinions till here. <-- HERE -->
}
}
java.mail.internet.InternetAddress
class that may do the email parsing you want (ie can return just the front). I found the documentation for Java EE 7, can't find anything newer. \$\endgroup\$