kucci / guava-libraries

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

propose nullToZero(Integer) method for Ints #440

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
public static int nullToZero(Integer input)
{
   return input == null ? 0 : input.intValue();
}

and probably same for other similar helper classes (Floats, Doubles, Longs, 
etc.)

Original issue reported on code.google.com by ali.bagh...@gmail.com on 1 Oct 2010 at 4:27

GoogleCodeExporter commented 9 years ago
Some Thoughts/Possible Alternatives:
 - In each primitive class have a defaultIfNull(T value, T default), maybe with defaultIfNull(T value) delegating to the 2 param. version with a suitable default (ex 0 for numeric primitives)
 - Functions<T, T> defaultIfNullFunction(T value, T default) in Functions

Original comment by boppenh...@google.com on 1 Oct 2010 at 6:23

GoogleCodeExporter commented 9 years ago
Or...

Objects.firstNotNull(input, 0).intValue();

Original comment by jim.andreou on 1 Oct 2010 at 7:42

GoogleCodeExporter commented 9 years ago
I was just trying to stick to the naming convention used for Strings.nullToEmpty

Original comment by ali.bagh...@gmail.com on 1 Oct 2010 at 12:24

GoogleCodeExporter commented 9 years ago
I'd use Jim's suggestion.

int value = (int) Objects.firstNonNull(input, 0);

Original comment by kevinb@google.com on 4 Oct 2010 at 1:54

GoogleCodeExporter commented 9 years ago
Thanks for that.

Also, I have something like the following:

y = z == null ? "" : z.getX();

I know this one is a bit tricky, but is there any helper method for this kind 
of thing?

Original comment by ali.bagh...@gmail.com on 4 Oct 2010 at 10: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