Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to correctly use Optional to solve null Security problems

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article focuses on "how to correctly use Optional to solve null security problems", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to correctly use Optional to solve null security problems.

Using Optional, we can rewrite the following code.

Public static String getName (User u) {

If (u = = null)

Return "Unknown"

Return u.name

}

But don't rewrite it like this.

Public static String getName (User u) {

Optional user = Optional.ofNullable (u)

If (! user.isPresent ()

Return "Unknown"

Return user.get () name

}

Not only is this rewriting not concise, but its operation is the same as the first piece of code. It is nothing more than using the isPresent method to replace u==null. This kind of rewriting is not the correct use of Optional, let's rewrite it again.

Public static String getName (User u) {

Return Optional.ofNullable (u)

.map (user- > user.name)

.orElse ("Unknown")

}

This is the correct way to use Optional. So according to this line of thinking, we can safely make chain calls instead of judging layer by layer. Look at a piece of code:

Public static String getChampionName (Competition comp) throws IllegalArgumentException {

If (comp! = null) {

CompResult result = comp.getResult ()

If (result! = null) {

User champion = result.getChampion ()

If (champion! = null) {

Return champion.getName ()

}

}

}

Throw new IllegalArgumentException ("The value of param comp isn't available.")

}

Due to a variety of reasons (for example: the competition has not yet produced a champion, abnormal calls to a method, a big gift package buried in the implementation of a method, etc.), we can not be happy all the way comp.getResult (). GetChampion (). GetName () to the end. Other languages, such as kotlin, provide operator blessing at the syntax level: comp?.getResult ()? .getChampion ()? .getName (). So be reasonable what do we do in Java!

Let's see what this code looks like after being blessed by Optional.

Public static String getChampionName (Competition comp) throws IllegalArgumentException {

Return Optional.ofNullable (comp)

.map (c-> c.getResult ())

.map (r-> r.getChampion ())

.map (u-> u.getName ())

.orElseThrow (()-> new IllegalArgumentException ("The value of param comp isn't available."))

}

This is very comfortable. Optional gives us a really elegant Java-style approach to solving null security problems. Although it does not directly provide an operator that is short to write, the code still looks cool and comfortable. Not to mention?. It is a matter of opinion whether such grammar looks good or not.

There are also many good postures, for example, if you don't print if you leave it empty, you can write like this:

String.ifPresent (System.out::println)

The charm of Optional is more than that. Optional also has some magical uses, such as Optional, which can be used to verify the validity of parameters.

Public void setName (String name) throws IllegalArgumentException {

This.name = Optional.ofNullable (name) .filter (User::isNameValid)

.orElseThrow (()-> new IllegalArgumentException ("Invalid username."))

}

It should be elegant enough to write the validity test of parameters in this way.

At this point, I believe you have a deeper understanding of "how to correctly use Optional to solve null security problems". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report