I have a method in which I am accepting a String clientid
and that has below requirements:
clientid
can be a positive number greater than zero. But if it is negative number or zero, then throwIllegalArgumentException
with a message.clientid
cannot be anull
or empty string. But if it is, then throwIllegalArgumentException
with a message.clientid
can be a normal string as well. For example - it can beabcdefgh
or 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);
final Long id = Longs.tryParse(clientid);
if (id != null) {
checkArgument(id.longValue() > 0, "clientid must not be negative or zero, found '%s'.", clientid);
}
this.clientid = clientid;
return this;
}
This code works fine. Now the problem is, I cannot use guava library greater than version 11. If I do use it, then it cause problem to our customer who use this library so in short I am looking for substitute for this line final Long id = Longs.tryParse(clientid);
without using guava or may be with older guava version 11. Since Longs.tryParse
method was added in Guava 14 or later.
What is the best way to do that? Anything we can use from Apache Commons?