averbraeck / djutils-base

Basic utility classes for djutils, djunits, dsol and other projects
BSD 3-Clause "New" or "Revised" License
0 stars 0 forks source link

Throw.whenNull() without "may not be null" #9

Closed averbraeck closed 7 hours ago

averbraeck commented 7 hours ago

It is un-progammer-like to write may not be null for the frequent null-checks that are performed with Throw.whenNull(). For example in Point2d.closestPointOnSegment():

        Throw.whenNull(segmentPoint1, "linePoint1 may not be null");
        Throw.whenNull(segmentPoint2, "linePoint2 may not be null");

Note also that the names are not correct in the provided messages. It is not possible in java to know what the name of a variable was at the level of the calling class. That is, Throw cannot know about the names segmentPoint1 and segmentPoint2. Therefore, simplifying this to Throw.whenNull(segmentPoint1) does not suffice.

What we can do is the following:

        Throw.whenNull(segmentPoint1, "segmentPoint1");
        Throw.whenNull(segmentPoint2, "segmentPoint2");

This makes it very clear whether the name is correctly given to a programmer. Throw can figure out whether to add " may not be null" by checking whether there is a blank in the provided message. Specifically using a regular expression pattern matching with \s. Note that this is only done when in fact a NullPointerException will be thrown. More elaborate messages, that should contain a \s, are still possible in this way and will not be appended with " may not be null".

averbraeck commented 7 hours ago

This was issue https://github.com/averbraeck/djutils/issues/25.

averbraeck commented 7 hours ago

Solved with the following code:

        if (object == null)
        {
            if (message.matches("\\S+")) // \S+ is any non-whitespace character
                throwMessage(NullPointerException.class, message + " may not be null", null);
            else
                throwMessage(NullPointerException.class, message, null);
        }
averbraeck commented 7 hours ago

Note that the private throwMessage(...) method now accepts null in case there are no arguments to be passed. Earlier, the method needed a new ArrayList<>() which was a waste of resources.

averbraeck commented 7 hours ago

The ThrowTest test class now also tests for the case where only a variable name (without any whitespace) is provided to the method.

averbraeck commented 7 hours ago

Solved.