IDEA-187214 Stream to loop inspection: support Java 10 toUnmodifiableList/Set/Map

This commit is contained in:
Tagir Valeev
2018-02-27 10:57:08 +07:00
parent 2bc3c8b81b
commit 0da4fd17f3
4 changed files with 128 additions and 4 deletions
@@ -0,0 +1,47 @@
// "Fix all 'Stream API call chain can be replaced with loop' problems in file" "true"
package java.util.stream;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
class Collectors {
public static native <T> Collector<T, ?, List<T>> toUnmodifiableList();
public static native <T> Collector<T, ?, Set<T>> toUnmodifiableSet();
public static native <T, K, U> Collector<T, ?, Map<K,U>> toUnmodifiableMap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper);
}
class MyFile {
public static List<String> testList(String[] args) {
List<String> result = new ArrayList<>();
for (String arg : args) {
if (arg != null) {
result.add(arg);
}
}
List<String> list = Collections.unmodifiableList(result);
return list;
}
public static Set<String> testSet(String[] args) {
Set<String> set = new HashSet<>();
for (String arg : args) {
if (arg != null) {
set.add(arg);
}
}
return Collections.unmodifiableSet(set);
}
public static Map<String, String> testMap(String[] args) {
Map<String, String> map = new HashMap<>();
for (String arg : args) {
if (arg != null) {
if (map.put(arg.trim(), arg) != null) {
throw new IllegalStateException("Duplicate key");
}
}
}
return Collections.unmodifiableMap(map);
}
}
@@ -0,0 +1,30 @@
// "Fix all 'Stream API call chain can be replaced with loop' problems in file" "true"
package java.util.stream;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
class Collectors {
public static native <T> Collector<T, ?, List<T>> toUnmodifiableList();
public static native <T> Collector<T, ?, Set<T>> toUnmodifiableSet();
public static native <T, K, U> Collector<T, ?, Map<K,U>> toUnmodifiableMap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper);
}
class MyFile {
public static List<String> testList(String[] args) {
List<String> list = Arrays.stream(args).filter(Objects::nonNull)
.collect(Collectors.toUnmodifiab<caret>leList());
return list;
}
public static Set<String> testSet(String[] args) {
return Arrays.stream(args).filter(Objects::nonNull)
.collect(Collectors.toUnmodifiableSet());
}
public static Map<String, String> testMap(String[] args) {
return Arrays.stream(args).filter(Objects::nonNull)
.collect(Collectors.toUnmodifiableMap(String::trim, Function.identity()));
}
}