Home > Java > javaTutorial > How to implement empty judgment in Java

How to implement empty judgment in Java

王林
Release: 2023-04-22 18:46:16
forward
993 people have browsed it
NullObject Mode

Countless null judgments in the project have had a very bad impact on the code quality and cleanliness. We call this phenomenon "null judgment disaster".

So, how to manage this phenomenon? You may have heard of the NullObject pattern, but this is not our weapon today, but we still need to introduce the NullObject pattern.

What is NullObject mode?

In object-oriented computer programming, a null object is an object with no referenced value or with defined neutral ("null") behavior. The null object design pattern describes the uses of such objects and their behavior (or lack thereof).

The above analysis comes from Wikipedia.

The NullObject pattern was first published in the "Programming Pattern Language" series of books. Generally, in object-oriented languages, you need to use a null check before calling objects to determine whether these objects are null, because the required methods cannot be called on null references.

The sample code is as follows (the name comes from the Internet, haha, how lazy is it):

Nullable is the related operation interface of the empty object, used to determine whether the object is empty, because in the empty object mode , if the object is empty, it will be packaged into an Object and become Null Object. This object will implement null implementations of all methods of the original object...

public interface Nullable {      boolean isNull(); }
Copy after login

This interface defines the behavior of the business object.

 <br>
Copy after login
public interface DependencyBase extends Nullable {      void Operation();  }
Copy after login

This is the real class of the object, which implements the business behavior interface DependencyBase and the empty object operation interface Nullable.

public class Dependency implements DependencyBase, Nullable {      @Override     public void Operation() {         System.out.print("Test!");     }      @Override     public boolean isNull() {         return false;     }  }
Copy after login

This is an empty object, which implements the behavior of the original object.

public class NullObject implements DependencyBase{      @Override     public void Operation() {         // do nothing     }      @Override     public boolean isNull() {         return true;     }  }
Copy after login

When in use, the empty object can be called through factory calling, or the object can be called through other methods such as reflection (usually taking a few milliseconds), which will not be described in detail here.

public class Factory {      public static DependencyBase get(Nullable dependencyBase){         if (dependencyBase == null){             return new NullObject();         }         return new Dependency();     }  }
Copy after login

This is a usage example. Through this mode, we no longer need to perform the null operation of the object, but can use the object directly without worrying about NPE (NullPointerException).

public class Client {      public void test(DependencyBase dependencyBase){         Factory.get(dependencyBase).Operation();     }  }
Copy after login
.NR Null Object

NR Null Object is an Intellij suitable for Android Studio, IntelliJ IDEA, PhpStorm, WebStorm, PyCharm, RubyMine, AppCode, CLion, GoLand, DataGrip and other IDEAs plugin. It can quickly and easily generate the components required for its empty object mode based on existing objects. Its functions include the following:

  1. Analyze the methods of the selected class that can be declared as interfaces;

  2. Abstract the public interface;

  3. Create an empty object and automatically implement the public interface;

  4. For some functions Make a nullable declaration;

  5. You can append functions to generate again;

  6. Automatic function naming convention

Let's take a look at a usage example:

How to implement empty judgment in Java

How about it? It seems very fast and convenient. It only needs to be empty-checked multiple times on the original object. In the email pop-up menu, select Generate, and select NR Null Object to automatically generate the corresponding null object component.

So how to get this plug-in?
Installation method

Can be installed directly through the Plugins repository in IDEA's Preferences.

Select Preferences → Plugins → Browse repositories

How to implement empty judgment in Java

##Search for "NR Null Oject" or "Null Oject" for fuzzy query , click Install on the right and restart IDEA.

How to implement empty judgment in Java

Optional

Another way is to use the Optional in the Java8 feature to perform elegant empty judgment. The official introduction of Optional is as follows:

A container object which may or may not contain a non-null value. If a value is present, isPresent() will return true and get() will return the value.

A container object that may or may not contain non-null values. If the value is present, isPresent() will return true and get() will return the value.

Without further ado, let me give you an example.

There is the following code, which needs to obtain the Info information in Test2, but the parameter is Test4. We need to apply layer by layer. The objects obtained at each layer may be empty. The final code looks like so.

public String testSimple(Test4 test) {        if (test == null) {            return "";        }        if (test.getTest3() == null) {            return "";        }        if (test.getTest3().getTest2() == null) {            return "";        }        if (test.getTest3().getTest2().getInfo() == null) {            return "";        }        return test.getTest3().getTest2().getInfo();    }
Copy after login
But after using Optional, the whole thing is different.

public String testOptional(Test test) {         return Optional.ofNullable(test).flatMap(Test::getTest3)                 .flatMap(Test3::getTest2)                 .map(Test2::getInfo)                 .orElse("");     }
Copy after login
1. Optional.ofNullable(test), if test is empty, a singleton empty Optional object is returned. If it is not empty, an Optional packaging object is returned. Optional wraps test. ;

public static <t> Optional<t> ofNullable(T value) {         return value == null ? empty() : of(value);     }</t></t>
Copy after login
2. flatMap(Test::getTest3) determines whether test is empty. If it is empty, continue to return the singleton Optional object in the first step, otherwise call Test’s getTest3 method;

public<u> Optional<u> flatMap(Function super T, Optional<u>> mapper) {         Objects.requireNonNull(mapper);         if (!isPresent())             return empty();         else {             return Objects.requireNonNull(mapper.apply(value));         }     }</u></u></u>
Copy after login

3、flatMap(Test3::getTest2)同上调用Test3的getTest2方法;

4、map(Test2::getInfo)同flatMap类似,但是flatMap要求Test3::getTest2返回值为Optional类型,而map不需要,flatMap不会多层包装,map返回会再次包装Optional;  

public<u> Optional<u> map(Function super T, ? extends U> mapper) {        Objects.requireNonNull(mapper);        if (!isPresent())            return empty();        else {            return Optional.ofNullable(mapper.apply(value));        }    }</u></u>
Copy after login

5、orElse("");获得map中的value,不为空则直接返回value,为空则返回传入的参数作为默认值。

public T orElse(T other) {     return value != null ? value : other; }
Copy after login

怎么样,使用Optional后我们的代码是不是瞬间变得非常整洁,或许看到这段代码你会有很多疑问,针对复杂的一长串判空,Optional有它的优势,但是对于简单的判空使用Optional也会增加代码的阅读成本、编码量以及团队新成员的学习成本。毕竟Optional在现在还并没有像RxJava那样流行,它还拥有一定的局限性。

如果直接使用Java8中的Optional,需要保证安卓API级别在24及以上。

How to implement empty judgment in Java

你也可以直接引入Google的Guava。(啥是Guava?来自官方的提示)

Guava  is a set of core libraries that includes new collection types (such as  multimap and multiset), immutable collections, a graph library,  functional types, an in-memory cache, and APIs/utilities for  concurrency, I/O, hashing, primitives, reflection, string processing,  and much more!

引用方式,就像这样:    

dependencies {       compile 'com.google.guava:guava:27.0-jre'       // or, for Android:       api 'com.google.guava:guava:27.0-android'     }
Copy after login

不过IDEA默认会显示黄色,提示让你将Guava表达式迁移到Java Api上。

How to implement empty judgment in Java

当然,你也可以通过在Preferences搜索"Guava"来Kill掉这个Yellow的提示。

How to implement empty judgment in Java

使用Optional具有如下优点:
  1. 将防御式编程代码完美包装

  2. 链式调用

  3. 有效避免程序代码中的空指针

但是也同样具有一些缺点:
  1. 流行性不是非常理想,团队新成员需要学习成本

  2. 安卓中需要引入Guava,需要团队每个人处理IDEA默认提示,或者忍受黄色提示

当然,Kotlin以具有优秀的空安全性为一大特色,并可以与Java很好的混合使用,like this:    

test1?.test2?.test3?.test4
Copy after login

The above is the detailed content of How to implement empty judgment in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template