I have a method in which I am accepting a String clientid and that has below requirements:
clientidcan be a positive number greater than zero. But if it is negative number or zero, then throwIllegalArgumentExceptionwith a message.clientidcannot be anullor empty string. But if it is, then throwIllegalArgumentExceptionwith a message.clientidcan be a normal string as well. For example - it can beabcdefghor any other string.
import static com.google.common.base.Preconditions.checkArgument;
public Builder setClientId(String clientid) {
checkArgument(!Strings.isNullOrEmpty(clientid), "clientid cannot not be null or an empty string, found '%s'.",
clientid);
try {
final long id = Long.parseLong(clientid);
checkArgument(id > 0, "clientid must not be negative or zero, found '%s'.", clientid);
} catch (NumberFormatException e) {}
this.clientid = clientid;
return this;
}
I cannot use Guava library greater than 11 so I cannot use Longs.tryParse method here so just using normal ugly way of parsing string. Is there anything I can improve here which is without try catch block? Anything from Apache Commons?
-1as one? \$\endgroup\$"([-+]?)\\d+"), but if you need to be concerned with overflowing aLong, it won't do the job. You'll need either to catch exceptions fromLong.parseLongas you're doing or re-implement its logic yourself, substituting it'sthrows with what you need done. \$\endgroup\$