I have a method in which I am accepting a String and that can be number as a string or a normal string.
public Builder setClientId(String clientId) {
checkNotNull(clientId, "clientId cannot be null");
checkArgument(clientId.length() > 0, "clientId can't be an empty string");
this.clientId = clientId;
return this;
}
Now I want to add a check let's say if anyone is passing clientId
as negative number "-12345"
or zero "0"
, then I want to interpret this and throw IllegalArgumentException
with message as "clientid must not be negative or zero as a number"
or may be some other good message. How can I do this using guava Preconditions if possible?
As per suggestion I am using below code:
public Builder setClientId(String clientId) {
checkNotNull(clientId, "clientId cannot be null");
checkArgument(clientId.length() > 0, "clientId can't be an empty string");
checkArgument(!clientid.matches("-\\d+|0"), "clientid must not be negative or zero");
this.clientId = clientId;
return this;
}
Is there any better way of doing it?