dnrajugade / guava-libraries

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

Booleans.nullToFalse(Boolean) #1188

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
It would be great to have something along the lines of

  public static boolean nullToFalse(Boolean b) {
    return (b == null) ? false : booleanValue;
  }

as part of Booleans, analogous to Strings.nullToEmpty(String).

Original issue reported on code.google.com by foo...@gmail.com on 2 Nov 2012 at 7:20

GoogleCodeExporter commented 9 years ago
Here's the comparison:

Booleans.nullToFalse(b)
(b == null) ? false : b

OK, probably we'd static import to shorten it, but we can shorten the other, 
too:

nullToFalse(b)
value != b && b

So the method wouldn't save quite as much space as nullToEmpty, which replaces 
this:

(s == null) ? "" : s

But maybe the real advantage is having to write |b| only once, since it may be 
a large expression that's expensive to compute. In that case, have a look at 
Objects.firstNonNull:

firstNonNull(b, false)

That's several characters longer, but does it come up often enough to justify 
an extra method?  Arguably even nullToEmpty was unnecessary:

nullToEmpty(s)
firstNonNull(s, "");

Underlying all this is the question of where the nullable Boolean is coming 
from. If there are three values (true, false, null), then another type might be 
more suitable, especially given how easy it is to write "if (!b)" with a 
nullable Boolean. Of course, there are times where a nullable Boolean works 
well enough -- maybe in user preferences that optionally override a default. 
But then do we need both nullToFalse and nullToTrue? firstNonNull is probably a 
better fit.

Original comment by cpov...@google.com on 2 Nov 2012 at 8:34

GoogleCodeExporter commented 9 years ago
Actually, can we a) mark this WorkingAsIntended, and b) consider deprecating 
Strings.nullToEmpty in the first place?

(Also, relevant discussion: 
https://groups.google.com/forum/?fromgroups=#!searchin/guava-discuss/nullToEmpty
/guava-discuss/mfD6uP9QopM/W7pEfVg98NYJ discsus)

Original comment by wasserman.louis on 2 Nov 2012 at 8:37

GoogleCodeExporter commented 9 years ago

Original comment by wasserman.louis on 2 Nov 2012 at 8:37

GoogleCodeExporter commented 9 years ago
(I think about deprecating Strings.nullToEmpty() every single time it gets used 
as justification for adding another method like it. Which is approximately 
monthly.)

Original comment by kevinb@google.com on 2 Nov 2012 at 9:53

GoogleCodeExporter commented 9 years ago
Looks like I wasn't aware of the existence of Objects.firstNonNull. That's even 
better of course, given its flexibility.

Original comment by foo...@gmail.com on 3 Nov 2012 at 9:58

GoogleCodeExporter commented 9 years ago
Another option instead of Objects.firstNonNull:
Optional.fromNullable(bool).or(false);

Original comment by kak@google.com on 3 Nov 2012 at 2:47

GoogleCodeExporter commented 9 years ago

Original comment by kevinb@google.com on 3 Nov 2012 at 3:00

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:13

GoogleCodeExporter commented 9 years ago

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