import entity.Dog;import entity.User2;import org.junit.Test;import java.util.Optional;import static org.junit.Assert.assertEquals;/*** @Author 伍冠源* @Date 2021年5月10日* @Description 1不要将null赋给Optional* 2避免使用Optional.get()。如果你不能证明存在可选项,那么永远不要调用get()。* 3不要在字段,方法参数,集合中使用Optional。* 4只有每当结果不确定时,使用Optional作为返回类型。* 5不要害怕使用map和filter。* 6不要为了链方法而使用optional 。* 7使所有表达式成为单行lambda*/public class OptionalTest2 {@Testpublic void variable1() {String str = "something...";if (str == null) {throw new IllegalArgumentException("No such variable");} else {//100 lines of code}}@Testpublic void variable1Refactor1() {String str = "something...";String result = Optional.ofNullable(str).orElseThrow(() -> new NullPointerException());assertEquals(result, str);}@Test(expected = NullPointerException.class)public void variable1Refactor2() {String str = null;String result = Optional.ofNullable(str).orElseThrow(() -> new NullPointerException());}private String createVariable() {return "created";}private String extracted() {String str = "something...";if (str == null) {return this.createVariable();} else {//100 lines of code}return str;}@Testpublic void variable2() {extracted();}@Testpublic void variable2Refactor1() {String str = "something...";String result = Optional.ofNullable(str).orElseGet(() -> this.createVariable());assertEquals(result, str);}@Testpublic void variable2Refactor2() {String str = null;String result = Optional.ofNullable(str).orElseGet(() -> this.createVariable());assertEquals(this.createVariable(), result);}@Testpublic void variable3() {extracted2();}private String extracted2() {String str = "something...";if (str == null) {return "new variable";} else {//100 lines of code}return str;}@Testpublic void variable3Refactor1() {String str = "something...";String result = Optional.ofNullable(str).orElse("new variable");assertEquals(str, result);}@Testpublic void variable3Refactor2() {String str = null;String result = Optional.ofNullable(str).orElse("new variable");assertEquals("new variable", result);}private String dogToString(User2 user) {if (user != null) {return "DOG'd name is : " + user.getName();} else {return "CAT";}}@Testpublic void variable4() {User2 user = new User2();String dogString = dogToString(user);assertEquals("DOG'd name is : null", dogString);}private String convertToDog(User2 user) {return "DOG'd name is : " + user.getName();}private String convertToDog2(User2 user) {return user.getName();}@Testpublic void variable4Refactor1() {User2 user = new User2("wgy", "18", null);String result = Optional.ofNullable(user).map(this::convertToDog).orElse("CAT");assertEquals("DOG'd name is : wgy", result);}@Testpublic void variable4Refactor2() {User2 user = new User2(null, "18", null);String result = Optional.ofNullable(user).map(this::convertToDog2).orElse("CAT");assertEquals("CAT", result);}private void doBlaBlaBla(Dog dog) {System.out.println("doBlaBlaBla execute...");}@Test(expected = NullPointerException.class)public void variable5() {Dog dog = new Dog();if (dog != null && dog.getIsBigDog()) {this.doBlaBlaBla(dog);}}@Testpublic void variable5Refactor1() {Optional<Dog> optionalDog = Optional.ofNullable(new Dog());optionalDog.filter(dog -> null != dog.getIsBigDog()).ifPresent(this::doBlaBlaBla);}@Testpublic void variable5Refactor2() {Optional<Dog> optionalDog = Optional.ofNullable(null);optionalDog.filter(Dog::getIsBigDog).ifPresent(this::doBlaBlaBla);}/*** 不要为了链方法而使用optional 。*/@Testpublic void variable6() {Dog dog = new Dog();Optional.ofNullable(dog).ifPresent(this::doBlaBlaBla);}/*** Optional* .ofNullable(someVariable)* .map(variable -> {* try{* return someREpozitory.findById(variable.getIdOfOtherObject());* } catch (IOException e){* LOGGER.error(e);* throw new RuntimeException(e);* }})* .filter(variable -> {* if(variable.getSomeField1() != null){* return true;* } else if(variable.getSomeField2() != null){* return false;* } else {* return true;* }* })* .map((variable -> {* try{* return jsonMapper.toJson(variable);* } catch (IOException e){* LOGGER.error(e);* throw new RuntimeException(e);* }}))* .map(String::trim)* .orElseThrow(() -> new RuntimeException("something went horribly wrong."))* =======================================重构为=============================================* Optional* .ofNullable(someVariable)* .map(this::findOtherObject)* .filter(this::isThisOtherObjectStale)* .map(this::convertToJson)* .map(String::trim)* .orElseThrow(() -> new RuntimeException("something went horribly wrong."));*/}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。