Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check for null object and null value contained in object in Java 8 way

how to rewrite this function to be more Java 8 with Optionals? Or should I just leave it as it is?

public void setMemory(ArrayList<Integer> memory) {
    if (memory == null)
        throw new IllegalArgumentException("ERROR: memory object can't be null.");
    if (memory.contains(null))
        throw new IllegalArgumentException("ERROR: memory object can't contain null value.");

    this.memory = memory;
}
like image 295
Jump3r Avatar asked Nov 28 '17 09:11

Jump3r


People also ask

How do you check if all fields of an object are null in Java 8?

Java Check if Object Is Null Using java. Objects class has static utility methods for operating an object. One of the methods is isNull() , which returns a boolean value if the provided reference is null, otherwise it returns false. We have created two classes - User1 and User2 as shown in the code below.

How do you check for null in Java 8?

In order to check whether a Java object is Null or not, we can either use the isNull() method of the Objects class or comparison operator.

How do you check if an object is null?

Typically, you'll check for null using the triple equality operator ( === or !== ), also known as the strict equality operator, to be sure that the value in question is definitely not null: object !== null . That code checks that the variable object does not have the value null .

How do I filter null values in Java 8?

Java 8 Example: Filter null values from a stream We can use lambda expression str -> str!= null inside stream filter() to filter out null values from a stream.


2 Answers

For the first case I would use: Objects.requireNonNull().

I don't think Optional is a way to go here as null is an illegal value.

like image 70
ps-aux Avatar answered Sep 19 '22 17:09

ps-aux


You've got a pattern condition -> throw an exception which can be moved to a method:

private void checkOrElseThrow(boolean condition, Supplier<? extends RuntimeException> exceptionSupplier) {
    if (condition) {
        throw exceptionSupplier.get();
    }
}

public void setMemory(List<Integer> memory) {

    checkOrElseThrow(memory == null, () -> new IllegalArgumentException("message #1"));
    checkOrElseThrow(memory.contains(null), () -> new IllegalArgumentException("message #2"));

    this.memory = memory;
}

If the type of the exception is not going to be changed, it's reasonable to pass only the message of the exception (thank @tobias_k for pointing it out):

private void checkOrElseThrow(boolean condition, String exceptionMessage) {
    if (condition) {
        throw new IllegalArgumentException(exceptionMessage);
    }
}

public void setMemory(List<Integer> memory) {

    checkOrElseThrow(memory == null, "message #1");
    checkOrElseThrow(memory.contains(null), "message #2");

    this.memory = memory;
}
like image 26
Andrew Tobilko Avatar answered Sep 20 '22 17:09

Andrew Tobilko