我有一个方法,我接受一个字符串,它可以是数字作为字符串或普通字符串。
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;
}
现在我想添加一个检查,假设是否有人clientId
作为负数"-12345"
或零传递"0"
,那么我想解释这个并抛出IllegalArgumentException
消息作为"clientid must not be negative or zero as a number"
或可能是其他一些好消息。如果可能的话,我如何使用番石榴前提条件来做到这一点?
根据建议,我使用以下代码:
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;
}
有没有更好的方法呢?