IDEA-185167 Allow to convert automatically Optional.orElse(...) to Optional.orElseGet(() -> ...)

This commit is contained in:
Tagir Valeev
2018-01-19 15:07:21 +07:00
parent b3b019e5b7
commit c841cb3b44
18 changed files with 469 additions and 155 deletions
@@ -0,0 +1,10 @@
// "Use 'computeIfAbsent' method with functional argument" "true"
import java.util.Map;
class Test {
public void test(Map<String, List<Integer>> map, String key) {
map.computeIfAbsent(key, k -> new ArrayList<>());
map.get(key).add(0);
}
}
@@ -0,0 +1,13 @@
// "Use 'orElseGet' method with functional argument" "true"
import java.util.Optional;
class Test {
String createDefaultString() {
return "foo";
}
public void test(Optional<String> opt) {
String result = opt.orElseGet(this::createDefaultString);
}
}
@@ -0,0 +1,14 @@
// "Use 'orElseGet' method with functional argument" "true"
import java.util.Optional;
class Test {
String createDefaultString(int x) {
return "foo"+x;
}
public void test(Optional<String> opt) {
int x = 5;
String result = opt.orElseGet(() -> createDefaultString(x));
}
}
@@ -0,0 +1,10 @@
// "Use 'computeIfAbsent' method with functional argument" "true"
import java.util.Map;
class Test {
public void test(Map<String, List<Integer>> map, String key) {
map.putIfAbsent(key, new ArrayL<caret>ist<>());
map.get(key).add(0);
}
}
@@ -0,0 +1,10 @@
// "Use 'computeIfAbsent' method with functional argument" "false"
import java.util.Map;
class Test {
public void test(Map<String, List<Integer>> map, String key) {
List<Integer> old = map.putIfAbsent(key, new ArrayL<caret>ist<>());
map.get(key).add(0);
}
}
@@ -0,0 +1,13 @@
// "Use 'orElseGet' method with functional argument" "true"
import java.util.Optional;
class Test {
String createDefaultString() {
return "foo";
}
public void test(Optional<String> opt) {
String result = opt.orElse(createDef<caret>aultString());
}
}
@@ -0,0 +1,14 @@
// "Use 'orElseGet' method with functional argument" "true"
import java.util.Optional;
class Test {
String createDefaultString(int x) {
return "foo"+x;
}
public void test(Optional<String> opt) {
int x = 5;
String result = opt.orElse(createDef<caret>aultString(x));
}
}
@@ -0,0 +1,15 @@
// "Use 'orElseGet' method with functional argument" "false"
import java.util.Optional;
class Test {
String createDefaultString(int x) {
return "foo"+x;
}
public void test(Optional<String> opt) {
int x = 5;
x++;
String result = opt.orElse(createDef<caret>aultString(x));
}
}
@@ -0,0 +1,9 @@
// "Use 'orElseGet' method with functional argument" "false"
import java.util.Optional;
class Test {
public void test(Optional<String> opt) {
String result = opt.orElse("f<caret>oo");
}
}