IDEA-161420 Quick-fix to replace if(optional.isPresent()) with better alternatives

This commit is contained in:
Tagir Valeev
2016-09-21 15:41:20 +07:00
parent 388a96e3b5
commit aadd111eda
30 changed files with 758 additions and 0 deletions
@@ -0,0 +1,11 @@
// "Replace Optional.isPresent() condition with orElse()" "true"
import java.util.*;
public class Main {
public void testOptional(Optional<String> str) {
String val;
val = str.orElse("");
System.out.println(val);
}
}
@@ -0,0 +1,12 @@
// "Replace Optional.isPresent() condition with orElse()" "true"
import java.util.*;
public class Main {
String val;
public void testOptional(Optional<String> str) {
this.val = str.orElse("");
System.out.println(val);
}
}
@@ -0,0 +1,11 @@
// "Replace Optional.isPresent() condition with map().orElse()" "true"
import java.util.*;
public class Main {
public void testOptional(Optional<String> str) {
String val;
val = str.map(String::trim).orElse("");
System.out.println(val);
}
}
@@ -0,0 +1,15 @@
// "Replace Optional.isPresent() condition with map().orElseGet()" "true"
import java.util.*;
public class Main {
public void testOptional(Optional<String> str) {
String val;
val = str.map(String::trim).orElseGet(this::getDefault);
System.out.println(val);
}
public String getDefault() {
return "";
}
}
@@ -0,0 +1,9 @@
// "Replace Optional.isPresent() condition with ifPresent()" "true"
import java.util.*;
public class Main {
public void testOptional(Optional<String> str) {
str.ifPresent(System.out::println);
}
}
@@ -0,0 +1,11 @@
// "Replace Optional.isPresent() condition with ifPresent()" "true"
import java.util.*;
public class Main {
String myString;
public void testOptional(Optional<String> str) {
str.ifPresent(s -> myString = s);
}
}
@@ -0,0 +1,9 @@
// "Replace Optional.isPresent() condition with map().orElse()" "true"
import java.util.*;
public class Main {
public String testOptional(Optional<String> str) {
return str.map(String::trim).orElse("");
}
}
@@ -0,0 +1,17 @@
// "Replace Optional.isPresent() condition with map().orElse()" "true"
import java.util.*;
public class Main {
static class MyList<T extends Number> extends ArrayList<T> {
@Override
public T get(int index) {
return super.get(index);
}
}
public Number testOptionalComments(Optional<MyList> strList) {
/* optional is present *//* optional is absent */
return strList.map(/*return something */myList -> myList.size() > /*too big*/ 1 ? myList.get(1) : 1.0).orElse(/* return null*/null);
}
}
@@ -0,0 +1,9 @@
// "Replace Optional.isPresent() condition with map().orElse()" "true"
import java.util.*;
public class Main {
public int testOptional2(Optional<List<String>> str) {
return str.map(strings -> strings.size() > 5 ? 5 : strings.size()).orElse(0);
}
}
@@ -0,0 +1,9 @@
// "Replace Optional.isPresent() condition with map().orElse()" "true"
import java.util.*;
public class Main {
public String testOptional(Optional<String> str) {
return str.map(String::trim).orElse("");
}
}
@@ -0,0 +1,9 @@
// "Replace Optional.isPresent() condition with orElse()" "true"
import java.util.*;
public class Main {
public String testOptional(Optional<String> str) {
return str.orElse("");
}
}
@@ -0,0 +1,15 @@
// "Replace Optional.isPresent() condition with orElse()" "true"
import java.util.*;
public class Main {
public void testOptional(Optional<String> str) {
String val;
if (str.isPrese<caret>nt()) {
val = str.get();
} else {
val = "";
}
System.out.println(val);
}
}
@@ -0,0 +1,16 @@
// "Replace Optional.isPresent() condition with orElse()" "true"
import java.util.*;
public class Main {
String val;
public void testOptional(Optional<String> str) {
if (str.isPrese<caret>nt()) {
this.val = str.get();
} else {
this.val = "";
}
System.out.println(val);
}
}
@@ -0,0 +1,15 @@
// "Replace Optional.isPresent() condition with map().orElse()" "true"
import java.util.*;
public class Main {
public void testOptional(Optional<String> str) {
String val;
if (str.isPrese<caret>nt()) {
val = str.get().trim();
} else {
val = "";
}
System.out.println(val);
}
}
@@ -0,0 +1,19 @@
// "Replace Optional.isPresent() condition with map().orElseGet()" "true"
import java.util.*;
public class Main {
public void testOptional(Optional<String> str) {
String val;
if (str.isPrese<caret>nt()) {
val = str.get().trim();
} else {
val = getDefault();
}
System.out.println(val);
}
public String getDefault() {
return "";
}
}
@@ -0,0 +1,11 @@
// "Replace Optional.isPresent() condition with ifPresent()" "true"
import java.util.*;
public class Main {
public void testOptional(Optional<String> str) {
if (str.isPrese<caret>nt()) {
System.out.println(str.get());
}
}
}
@@ -0,0 +1,13 @@
// "Replace Optional.isPresent() condition with ifPresent()" "true"
import java.util.*;
public class Main {
String myString;
public void testOptional(Optional<String> str) {
if (str.isPrese<caret>nt()) {
myString = str.get();
}
}
}
@@ -0,0 +1,11 @@
// "Replace Optional.isPresent() condition with ifPresent()" "false"
import java.util.*;
public class Main {
public void testOptional(Optional<String> str) {
if (str.isPrese<caret>nt()) {
System.out.println(str);
}
}
}
@@ -0,0 +1,12 @@
// "Replace Optional.isPresent() condition with ifPresent()" "false"
import java.util.*;
public class Main {
public void testOptional(Optional<String> str) {
if (str.isPrese<caret>nt()) {
System.out.println(str.get());
System.out.println(str.get());
}
}
}
@@ -0,0 +1,12 @@
// "Replace Optional.isPresent() condition with map().orElse()" "true"
import java.util.*;
public class Main {
public String testOptional(Optional<String> str) {
if (str.isPre<caret>sent()) {
return str.get().trim();
}
return "";
}
}
@@ -0,0 +1,22 @@
// "Replace Optional.isPresent() condition with map().orElse()" "true"
import java.util.*;
public class Main {
static class MyList<T extends Number> extends ArrayList<T> {
@Override
public T get(int index) {
return super.get(index);
}
}
public Number testOptionalComments(Optional<MyList> strList) {
if(strList.isPres<caret>ent()) {
/* optional is present */
return /*return something */ strList.get().size() > /*too big*/ 1 ? strList.get().get(1) : 1.0;
} else {
/* optional is absent */
return /* return null*/ null;
}
}
}
@@ -0,0 +1,13 @@
// "Replace Optional.isPresent() condition with map().orElse()" "false"
import java.util.*;
public class Main {
public String testOptional(Optional<String> str) {
if (str.isPre<caret>sent()) {
return str.get().trim();
}
System.out.println("oops");
return "";
}
}
@@ -0,0 +1,12 @@
// "Replace Optional.isPresent() condition with map().orElse()" "true"
import java.util.*;
public class Main {
public int testOptional2(Optional<List<String>> str) {
if(str.isPrese<caret>nt()) {
return str.get().size() > 5 ? 5 : str.get().size();
}
return 0;
}
}
@@ -0,0 +1,12 @@
// "Replace Optional.isPresent() condition with map().orElse()" "true"
import java.util.*;
public class Main {
public String testOptional(Optional<String> str) {
if ((!(str.isPre<caret>sent()))) {
return "";
}
return str.get().trim();
}
}
@@ -0,0 +1,12 @@
// "Replace Optional.isPresent() condition with map().orElse()" "false"
import java.util.*;
public class Main {
public int testOptionalRaw(Optional opt) {
if(opt.isPre<caret>sent()) {
return Math.abs(opt.get().hashCode());
}
return 0;
}
}
@@ -0,0 +1,12 @@
// "Replace Optional.isPresent() condition with orElse()" "true"
import java.util.*;
public class Main {
public String testOptional(Optional<String> str) {
if (str.isPre<caret>sent()) {
return str.get();
}
return "";
}
}