2

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?

user1950349
  • 4,738
  • 19
  • 67
  • 119

1 Answers1

2

I think the simplest way of doing this is as follows:

 public Builder setClientId(String clientId) {
    final Integer id = Ints.tryParse(clientId);
    checkArgument(id != null && id.intValue() > 0,
      "clientId must be a positive number, found: '%s'.", clientId);
    this.clientId = clientId;
    return this;
  }

When calling this method, this gives:

.setClientId("+-2"); 
// java.lang.IllegalArgumentException: clientId must be a positive number, found: '+-2'.

.setClientId("-1"); 
// java.lang.IllegalArgumentException: clientId must be a positive number, found: '-1'.

.setClientId(null); 
// java.lang.NullPointerException

This code uses Ints.tryParse. From the JavaDoc:

Returns:

the integer value represented by string, or null if string has a length of zero or cannot be parsed as an integer value

Also, it throws a NullPointerException when a null is received.


Edit: however, if any other string is allowed, the code changes to:

public Builder setClientId(String clientId) {
    checkArgument(!Strings.isNullOrEmpty(clientId),
      "clientId may not be null or an empty string, found '%s'.", clientId);
    final Integer id = Ints.tryParse(clientId);
    if (id != null) {
      checkArgument(id.intValue() > 0,
        "clientId must be a positive number, found: '%s'.", clientId);
    }
    this.clientId = clientId;
    return this;
  }

This code will accept all strings that are either a strictly positive integer OR non-null and non-empty.

Community
  • 1
  • 1
rinde
  • 1,181
  • 1
  • 8
  • 20
  • Also my requirement is if clientId is a proper string, then I want to allow that as well. So basically requirement is - `clientId` can be a number and if it is then it should be positive number greater than zero and `clientId` can be normal string as well. – user1950349 Jan 20 '16 at 17:29
  • I added a second version that allows normal strings as well. – rinde Jan 20 '16 at 18:23
  • Thanks looks good. Instead of using Integer for id, is there any long as well like `Longs.tryParse` because clientId can be Long instead of Integer. – user1950349 Jan 20 '16 at 19:45
  • Yes, Longs.tryParse and Doubles.tryParse exist so you can use the one you like. – rinde Jan 20 '16 at 19:48
  • I don't see for Longs. I get this error `The method tryParse(String) is undefined for the type Longs` – user1950349 Jan 20 '16 at 19:58
  • 1
    Are you using Guava 14 or later? This is the [JavaDoc](http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/primitives/Longs.html#tryParse(java.lang.String)). – rinde Jan 20 '16 at 20:00