DaveAKing / guava-libraries

Automatically exported from code.google.com/p/guava-libraries
Apache License 2.0
0 stars 0 forks source link

Preconditions with return of item checked #1384

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Preconditions could return the item provided for the check so they can be used 
in expressions. 

There are two cases. One is for null checks, where the object passed can be 
returned and then used, quite useful for a final check before returning:

String failWhenResultNull() {
  String result = null;
  // do something to result
  return checkNotNull(result);
}

The other one is to have a simple fail or continue strategy. Problably a new 
set of methods would be addecuate for this one:

R check(boolean condition, R result);
R check(boolean condition, R result, Object message);
R check(boolean condition, R result, String template, Object... arguments);

Here is an example where this could be useful:

void setPropertySafely(T item, P property) {
  check(item.isReady(), item).setProperty(property);
}

and using the checkNull as well it could look something like this:

void setPropertySafely(T item, P property) {
  check(checkNotNull(item).isReady(), item)
    .setProperty(property);
}

Original issue reported on code.google.com by pab...@gmail.com on 25 Apr 2013 at 10:46

GoogleCodeExporter commented 9 years ago

Original comment by wasserman.louis on 25 Apr 2013 at 3:40

GoogleCodeExporter commented 9 years ago
This issue has been migrated to GitHub.

It can be found at https://github.com/google/guava/issues/<issue id>

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:12

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 3 Nov 2014 at 9:08