yangxu998 / guava-libraries

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

Preconditions.checkNotNullOrEmpty() methods #541

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
How about to add a bunch of checkNotNullOrEmpty() methods?

instead of 2 lines:

Preconditions.checkArgument(!Strings.isEmptyOrNull(s));
someStr = s;

it could be nice one line:

someStr = Preconditions.checkNotNullOrEmpty(s);

-----------------------------------------

Draft checkNotNullOrEmpty implementation:

  public static String checkNotNullOrEmpty(String s) {
    if (Strings.isEmptyOrNull(s)) {
      throw new IllegalStateException();
    }
    return s;
  }

In general it is string oriented variant of checkNotNull().

Original issue reported on code.google.com by kua...@gmail.com on 4 Feb 2011 at 3:16

GoogleCodeExporter commented 9 years ago
All you need is

Preconditions.checkArgument(!s.isEmpty());
someStr = s;

Original comment by kevinb@google.com on 4 Feb 2011 at 11:51

GoogleCodeExporter commented 9 years ago
Using

Preconditions.checkArgument(!s.isEmpty());

Will throw a NPE if s is null, the goal of a checkNotNullOrEmpty is to simplify 
a very common check.

Preconditions.checkNotNull(arg) is a shorthand for 
Preconditions.checkArgument(arg != null)

Preconditions.checkNotNullOrEmpty(arg) would be a shorthand for 
Preconditions.checkArgument(!Strings.isNullOrEmpty(arg))

Original comment by david.morand on 20 Aug 2014 at 8:15

GoogleCodeExporter commented 9 years ago
> Preconditions.checkNotNull(arg) is a shorthand for 
Preconditions.checkArgument(arg != null)

No, it's not. The former throws an NPE. You may like it or not, but the Guava 
(and JDK) guys fell this decision a long time ago. Therefore 
checkArgument(!s.isEmpty()) does exactly the right thing.

For an endless discussion, see http://stackoverflow.com/q/3881/581205

Original comment by Maaarti...@gmail.com on 21 Aug 2014 at 1:51

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

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

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

GoogleCodeExporter commented 9 years ago

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