mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-08-26 15:27:45 +07:00
IDEA-161420 Quick-fix to replace if(optional.isPresent()) with better alternatives
This commit is contained in:
+11
@@ -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);
|
||||
}
|
||||
}
|
||||
+12
@@ -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);
|
||||
}
|
||||
}
|
||||
+11
@@ -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);
|
||||
}
|
||||
}
|
||||
+15
@@ -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 "";
|
||||
}
|
||||
}
|
||||
+9
@@ -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);
|
||||
}
|
||||
}
|
||||
+11
@@ -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);
|
||||
}
|
||||
}
|
||||
+9
@@ -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("");
|
||||
}
|
||||
}
|
||||
+17
@@ -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);
|
||||
}
|
||||
}
|
||||
+9
@@ -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);
|
||||
}
|
||||
}
|
||||
+9
@@ -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("");
|
||||
}
|
||||
}
|
||||
+9
@@ -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("");
|
||||
}
|
||||
}
|
||||
+15
@@ -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);
|
||||
}
|
||||
}
|
||||
+16
@@ -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);
|
||||
}
|
||||
}
|
||||
+15
@@ -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);
|
||||
}
|
||||
}
|
||||
+19
@@ -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 "";
|
||||
}
|
||||
}
|
||||
+11
@@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
+13
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
+11
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
+12
@@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
+12
@@ -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 "";
|
||||
}
|
||||
}
|
||||
+22
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
+13
@@ -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 "";
|
||||
}
|
||||
}
|
||||
+12
@@ -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;
|
||||
}
|
||||
}
|
||||
+12
@@ -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();
|
||||
}
|
||||
}
|
||||
+12
@@ -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;
|
||||
}
|
||||
}
|
||||
+12
@@ -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 "";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user