Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Idiomatic way of converting from Long object to long primitive safely in java

Tags:

java

I was trying to pass a Long object value to a method that expects long primitive, passing directly works except for the case when the Long object is null. In this case I get a Null Pointer Exception.

Long foo=null;
bar.methodExpects_long_primitive(foo);

I can create a check if foo is null and skip calling the method, like

Long foo=null;
if(foo!=null){
bar.methodExpects_long_primitive(foo);
}

or if I want to provide a default value

Long foo=null;
bar.methodExpects_long_primitive(foo==null?defaultValue:foo);

Is there a elegant/better way to do this? I have to repeat this multiple times in the codebase and doing it like this seems to add a lot of conditionals.

I could create my own method to do this, but I would like to know if there is already any such library method.

like image 867
Dhairya Seth Avatar asked Mar 23 '17 17:03

Dhairya Seth


People also ask

How do you return a long value in Java?

Long. longValue() is an inbuilt method of the Long class in Java which returns the value of this Long object as a long after the conversion. Parameters: This method do not take any parameters. Return Value: This method will return the numeric value represented by this object after conversion to long type.

What is the correct way to cast a long to an int?

There are basically three methods to convert long to int: By type-casting. Using toIntExact() method. Using intValue() method of the Long wrapper class.


2 Answers

As of Java 8, one option is to wrap your Long in an java.util.Optional. Wherever possible, pass this around instead of the Long. When you must get a long value, you can provide a default with Optional.orElse():

Long foo = null;

Optional<Long> optFoo = Optional.ofNullable( foo );
long longFoo = optFoo.orElse( -1L );

System.out.println( longFoo );

Or you can test whether the Optional holds a value with Optional.empty():

    Long foo = null;

    Optional<Long> optFoo = Optional.ofNullable( foo );
    if ( ! optFoo.empty() ) {
        long longFoo = optFoo.get();

        System.out.println( longFoo );
    }

One nice side effect of this approach is that it is clear that the value might be null, no matter where you pass the Optional. Another is that an attempt to call get() when the value is null will throw an exception immediately.

Before Java 8, you could use Google's guava library, which provides a very similar class com.google.common.base.Optional.

like image 55
Andy Thomas Avatar answered Oct 21 '22 22:10

Andy Thomas


There's nothing built into Long (e.g., some static method) to do it.

Assuming you have multiple methods accepting long you need to use this with, you'll probably want your own method, something along the lines of

// On some utility class, say LongUtil
public static long val(Long l, long defaultvalue) {
    return l == null ? defaultValue : l.longValue();
}

Then

bar.methodExpects_long_primitive(LongUtil.val(foo, defaultValue));

Of course, that's not a lot shorter than

bar.methodExpects_long_primitive(foo == null ? defaultValue : (long)foo);

If it's just methodExpects_long_primitive, I'd probably modify it to accept a Long instead, and handle the null within it.

like image 3
T.J. Crowder Avatar answered Oct 22 '22 00:10

T.J. Crowder