mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-08-26 15:27:45 +07:00
IDEA-187214 Stream to loop inspection: support Java 10 toUnmodifiableList/Set/Map
This commit is contained in:
+47
@@ -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);
|
||||
}
|
||||
}
|
||||
+30
@@ -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()));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user