ExtractSetFromComparisonChainAction improvements (IDEA-CR-19606)

1. Fixed modifiers for interfaces
2. Replacement wrapped with Collections.unmodifiableSet
3. Supported Java 1.4 and lower
4. Supported Guava ImmutableSet
5. Supported comparisons like s.equals("xyz"), Objects.equals(s, "xyz")
This commit is contained in:
Tagir Valeev
2017-03-27 12:35:02 +07:00
parent 22cfb54ff0
commit 93e35b7334
14 changed files with 228 additions and 47 deletions
@@ -1,10 +1,11 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
// "Extract Set from comparison chain" "true"
public class Test {
private static final Set<String> NAMES = new HashSet<>(Arrays.asList("foo", "bar", "baz"));
private static final Set<String> NAMES = Collections.unmodifiableSet(new HashSet<>(Arrays.asList("foo", "bar", "baz")));
void testOr(String name) {
if(name == null || NAMES.contains(name)) {
@@ -1,10 +1,11 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
// "Extract Set from comparison chain" "true"
public class Test {
private static final Set<String> NAMES = new HashSet<>(Arrays.asList("foo", "bar", "baz"));
private static final Set<String> NAMES = Collections.unmodifiableSet(new HashSet<>(Arrays.asList("foo", "bar", "baz")));
interface Person {
String getName();
@@ -0,0 +1,21 @@
// "Extract Set from comparison chain" "true"
package com.google.common.collect;
import java.util.Set;
class ImmutableSet<T> {
public static <T> ImmutableSet<T> of(T... elements) {
return null;
}
}
public class Test {
private static final Set<String> S = com.google.common.collect.ImmutableSet.of("foo", "bar", "baz");
void testOr(String s) {
if(S.contains(s)) {
System.out.println("foobarbaz");
}
}
}
@@ -0,0 +1,15 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
// "Extract Set from comparison chain" "true"
public interface Test {
Set<String> S = Collections.unmodifiableSet(new HashSet<>(Arrays.asList("foo", "bar", "baz")));
default void testOr(String s) {
if(S.contains(s)) {
System.out.println("foobarbaz");
}
}
}
@@ -0,0 +1,15 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
// "Extract Set from comparison chain" "true"
public class Test {
private static final Set S = Collections.unmodifiableSet(new HashSet(Arrays.asList(new String[]{"foo", "bar", "baz"})));
void testOr(String s) {
if(S.contains(s)) {
System.out.println("foobarbaz");
}
}
}
@@ -1,11 +1,12 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
// "Extract Set from comparison chain" "true"
public class Test {
public static final String BAR = "bar";
private static final Set<String> PROPERTIES = new HashSet<>(Arrays.asList("foo", BAR, "baz"));
private static final Set<String> PROPERTIES = Collections.unmodifiableSet(new HashSet<>(Arrays.asList("foo", BAR, "baz")));
void testOr(int i, String property) {
int PROPERTIES;
@@ -1,10 +1,11 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
// "Extract Set from comparison chain" "true"
public class Test {
private static final Set<String> S = new HashSet<>(Arrays.asList("foo", "bar", "baz"));
private static final Set<String> S = Collections.unmodifiableSet(new HashSet<>(Arrays.asList("foo", "bar", "baz")));
void testOr(String s) {
if(S.contains(s)) {
@@ -0,0 +1,12 @@
// "Extract Set from comparison chain" "true"
import java.util.*;
public class Test {
private static final Set<String> S = Collections.unmodifiableSet(new HashSet<>(Arrays.asList("foo", "bar", "baz", "quz")));
void testOr(String s) {
if(Objects.equals(s, null) || S.contains(s)) {
System.out.println("foobarbaz");
}
}
}
@@ -0,0 +1,17 @@
// "Extract Set from comparison chain" "true"
package com.google.common.collect;
class ImmutableSet<T> {
public static <T> ImmutableSet<T> of(T... elements) {
return null;
}
}
public class Test {
void testOr(String s) {
if("foo"<caret>.equals(s) || "bar".equals(s) || "baz".equals(s)) {
System.out.println("foobarbaz");
}
}
}
@@ -0,0 +1,8 @@
// "Extract Set from comparison chain" "true"
public interface Test {
default void testOr(String s) {
if("foo"<caret>.equals(s) || "bar".equals(s) || "baz".equals(s)) {
System.out.println("foobarbaz");
}
}
}
@@ -0,0 +1,8 @@
// "Extract Set from comparison chain" "true"
public class Test {
void testOr(String s) {
if("foo"<caret>.equals(s) || "bar".equals(s) || "baz".equals(s)) {
System.out.println("foobarbaz");
}
}
}
@@ -0,0 +1,10 @@
// "Extract Set from comparison chain" "true"
import java.util.Objects;
public class Test {
void testOr(String s) {
if(Objects.equals(s, null) || "foo"<caret>.equals(s) || s.equals("bar") || Objects.equals("baz", s) || Objects.equals(s, "quz")) {
System.out.println("foobarbaz");
}
}
}