My SmsRecipientDetails
class constructor accepts String
value as recipient phone number.
I would like to accept number with spaces:
assertDoesNotThrow(() -> new SmsRecipientDetails("123456789 "));
assertDoesNotThrow(() -> new SmsRecipientDetails(" 123456789 "));
assertDoesNotThrow(() -> new SmsRecipientDetails(" 123456789"));
Current constructor that does not accept phone number with spaces:
public SmsRecipientDetails(final String phoneNumber) {
AssertUtils.notNull(phoneNumber, "phoneNumber");
AssertUtils.validState(phoneNumber.matches("^[0-9]{9}$"), "Phone number is not valid");
this.phoneNumber = phoneNumber;
}
Q1: Where and how to trim input phoneNumber
parameter? Is this implementation with one temp value correct? I am not sure when there will be more input parameters because of clarity.
public SmsRecipientDetails(final String phoneNumber) {
AssertUtils.notNull(phoneNumber, "phoneNumber"); // throws exception when phoneNumber is null
final String toCheck = phoneNumber.trim();
AssertUtils.validState(toCheck.matches("^[0-9]{9}$"), "Phone number is not valid");
this.phoneNumber = toCheck;
}
Q2: Is it good idea to allow not correct values and preprocess it in constructor?