How can I get rid of redundant zero checking in java?

Suppose I have code like this:

Vehicle vehicle = vehicleRepostory.findByIdInitialized(vehicleId); 

If Vehicle cannot be found, this method will return null . The specification says that in this case I should throw a MyObjectNotFoundException so that the code becomes something like this:

 Vehicle vehicle = vehicleRepostory.findByIdInitialized(vehicleId); MyObjectNotFoundException.throwIfNull(vehicle, Vehicle.class, vehicleId); 

What if I want to get rid of calls to throwIfNull . In any case, this is not a DRY and bad design. There must be some kind of design template for me. I searched on the Internet, but it did not display anything useful.

A simple solution can put code in my repository, but I use SpringData for this, so this is just an interface:

 public interface VehicleRepository extends Repository<Vehicle, Long> // ... @Query("select v from Vehicle v LEFT JOIN FETCH v.technicalData td LEFT JOIN FETCH v.registrationData rd" + " where v.vehicleId = ?") Vehicle findByIdInitialized(Long vehicleId); 
+4
source share
7 answers

It looks like you want your repository to support this:

 Vehicle vehicle = vehicleRepository.loadExisting(vehicleId); 

where the difference between loadExisting and findByIdInitialized will be that the first expects the entity to be present and will throw an exception if it is not.

Thus, all clients of the repository can decide whether they will perform a search, they want the absence of an entity to lead to an exception or not. You may even find that all search queries by ID must throw an exception - in this case, you can return to one method.

EDIT: if the repository is auto-generated, I would take one of three approaches:

  • Wrap it
  • Expand it
  • Change generator

Which of these options will be most suitable will depend on the exact details.

+3
source

You can view @NotNull annotations in Java. There are several varieties of this, some of which do their compile-time checking and some of them at runtime. Some editors (such as IntelliJ IDEA) may alert you when you perform an operation that may lead to NPE.

What @NotNull Java annotation should I use? Avoid! = Null statements http://docs.oracle.com/javaee/6/api/javax/validation/constraints/NotNull.html

0
source

Of course, it would be better if you could reverse engineer the repository itself to create the exception you need. Without this, you will need workarounds that correct the behavior.

One approach would be a reflection: terminates the repository calls in a method that gets findByIdInitialized as a string and calls it reflexively. Then the method may throw an appropriate exception.

Another way could be to enable some AOPs to do the same, but perhaps with more type safety.

0
source

I suggest you add a nullable parameter to findByIdInitialized, and it will look like this:

 Vehicle findByIdInitialized(int vehicleId, boolean throwException) { // implementation behavior... if (throwException) { MyObjectNotFoundException.throwIfNull(vehicle, Vehicle.class, vehicleId); } } 
0
source

Use this:

 import static java.util.Objects.requireNonNull; . . . Object o = requireNonNull(obj); 
0
source

It sounds like you want an “after method return” AOP tip that catches errors and throws an exception.

0
source

You can use the NullObject dessign pattern, but an IMHO exception exception is the best choice.

To save in DRY, you must put this cast in findbyIdInitialized .

-2
source

All Articles