Visitar URL original
Replace `Maps.newConcurrentMap`, `Sets.newTreeSet` and `Strings.nullToEmpty` in NoGuava by knutwannheden · Pull Request #1421 · openrewrite/rewrite-migrate-java · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.openrewrite.java.migrate.guava;

import com.google.common.base.Strings;
import com.google.common.collect.Collections2;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
Expand All @@ -35,10 +36,13 @@
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -324,6 +328,22 @@ TreeMap<K, V> after(SortedMap<K, ? extends V> map) {
}
}

@RecipeDescriptor(
name = "Prefer `new ConcurrentHashMap<>()`",
description = "Prefer the Java standard library over third-party usage of Guava in simple cases like this."
)
public static class MapsNewConcurrentMap<K, V> {
@BeforeTemplate
ConcurrentMap<K, V> before() {
return Maps.newConcurrentMap();
}

@AfterTemplate
ConcurrentMap<K, V> after() {
return new ConcurrentHashMap<>();
}
}

@RecipeDescriptor(
name = "Prefer `Collections.newSetFromMap(new ConcurrentHashMap<>())`",
description = "Prefer the Java standard library over third-party usage of Guava in simple cases like this."
Expand Down Expand Up @@ -388,6 +408,56 @@ LinkedHashSet<T> after(int expectedSize) {
}
}

@RecipeDescriptor(
name = "Prefer `new TreeSet<>()`",
description = "Prefer the Java standard library over third-party usage of Guava in simple cases like this."
)
public static class SetsNewTreeSet<T extends Comparable<?>> {
@BeforeTemplate
TreeSet<T> before() {
return Sets.newTreeSet();
}

@AfterTemplate
TreeSet<T> after() {
return new TreeSet<>();
}
}

// `Sets.newTreeSet(Iterable)` has no template. Given a `SortedSet`, `new TreeSet<>(..)` keeps that set's
// comparator, where Guava sorts by natural order.
@RecipeDescriptor(
name = "Prefer `new TreeSet<>(Comparator)`",
description = "Prefer the Java standard library over third-party usage of Guava in simple cases like this."
)
public static class SetsNewTreeSetWithComparator<T> {
@BeforeTemplate
TreeSet<T> before(Comparator<? super T> comparator) {
return Sets.newTreeSet(comparator);
}

@AfterTemplate
TreeSet<T> after(Comparator<? super T> comparator) {
return new TreeSet<>(comparator);
}
}

@RecipeDescriptor(
name = "Prefer `Objects.toString(String, \"\")`",
description = "Prefer `java.util.Objects.toString(string, \"\")` over Guava's `Strings.nullToEmpty(string)`."
)
public static class StringsNullToEmpty {
@BeforeTemplate
String before(String string) {
return Strings.nullToEmpty(string);
}

@AfterTemplate
String after(String string) {
return Objects.toString(string, "");
}
}

@RecipeDescriptor(
name = "Prefer `Collection.stream().allMatch(Predicate)`",
description = "Prefer `Collection.stream().allMatch(Predicate)` over `Iterables.all(Collection, Predicate)`."
Expand Down
Loading