Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace UnaryOperator with Function in ListUtils methods #4720

Merged
merged 2 commits into from
Nov 26, 2024
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
27 changes: 15 additions & 12 deletions rewrite-core/src/main/java/org/openrewrite/internal/ListUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
*/
package org.openrewrite.internal;

import org.jetbrains.annotations.Contract;
import org.jspecify.annotations.Nullable;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.UnaryOperator;

import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;
Expand Down Expand Up @@ -117,7 +117,8 @@ public static <T> List<T> insert(@Nullable List<T> ls, @Nullable T t, int index)
* @param <T> The type of elements in the list.
* @return A new list with the modified last element, or the original list if unchanged.
*/
public static <T> List<T> mapLast(@Nullable List<T> ls, UnaryOperator<@Nullable T> mapLast) {
@Contract("null, _ -> null; !null, _ -> !null")
public static <T> @Nullable List<T> mapLast(@Nullable List<T> ls, Function<T, @Nullable T> mapLast) {
if (ls == null || ls.isEmpty()) {
//noinspection ConstantConditions
return ls;
Expand Down Expand Up @@ -145,7 +146,8 @@ public static <T> List<T> mapLast(@Nullable List<T> ls, UnaryOperator<@Nullable
* @param <T> The type of elements in the list.
* @return A new list with the modified first element, or the original list if unchanged.
*/
public static <T> List<T> mapFirst(@Nullable List<T> ls, UnaryOperator<@Nullable T> mapFirst) {
@Contract("null, _ -> null; !null, _ -> !null")
public static <T> @Nullable List<T> mapFirst(@Nullable List<T> ls, Function<T, @Nullable T> mapFirst) {
if (ls == null || ls.isEmpty()) {
//noinspection ConstantConditions
return ls;
Expand Down Expand Up @@ -173,7 +175,8 @@ public static <T> List<T> mapFirst(@Nullable List<T> ls, UnaryOperator<@Nullable
* @param <T> The type of elements in the list.
* @return A new list with modified elements, or the original list if unchanged.
*/
public static <T> List<T> map(@Nullable List<T> ls, BiFunction<Integer, T, @Nullable T> map) {
@Contract("null, _ -> null; !null, _ -> !null")
public static <T> @Nullable List<T> map(@Nullable List<T> ls, BiFunction<Integer, T, @Nullable T> map) {
if (ls == null || ls.isEmpty()) {
//noinspection ConstantConditions
return ls;
Expand All @@ -198,7 +201,6 @@ public static <T> List<T> map(@Nullable List<T> ls, BiFunction<Integer, T, @Null
while (newLs.remove(null)) ;
}

//noinspection NullableProblems
return newLs;
}

Expand All @@ -211,7 +213,8 @@ public static <T> List<T> map(@Nullable List<T> ls, BiFunction<Integer, T, @Null
* @return A new list with modified elements, or the original list if unchanged.
*/
// inlined version of `map(List, BiFunction)` for memory efficiency (no overhead for lambda)
public static <T> List<T> map(@Nullable List<T> ls, UnaryOperator<@Nullable T> map) {
@Contract("null, _ -> null; !null, _ -> !null")
public static <T> @Nullable List<T> map(@Nullable List<T> ls, Function<T, @Nullable T> map) {
if (ls == null || ls.isEmpty()) {
//noinspection ConstantConditions
return ls;
Expand All @@ -236,7 +239,6 @@ public static <T> List<T> map(@Nullable List<T> ls, UnaryOperator<@Nullable T> m
while (newLs.remove(null)) ;
}

//noinspection NullableProblems
return newLs;
}

Expand All @@ -250,7 +252,8 @@ public static <T> List<T> map(@Nullable List<T> ls, UnaryOperator<@Nullable T> m
* @param <T> The type of elements in the list.
* @return A new list with expanded or modified elements, or the original list if unchanged.
*/
public static <T> List<T> flatMap(@Nullable List<T> ls, BiFunction<Integer, T, @Nullable Object> flatMap) {
@Contract("null, _ -> null; !null, _ -> !null")
public static <T> @Nullable List<T> flatMap(@Nullable List<T> ls, BiFunction<Integer, T, @Nullable Object> flatMap) {
if (ls == null || ls.isEmpty()) {
//noinspection ConstantConditions
return ls;
Expand Down Expand Up @@ -310,7 +313,6 @@ public static <T> List<T> flatMap(@Nullable List<T> ls, BiFunction<Integer, T, @
}
}

//noinspection NullableProblems
return newLs;
}

Expand All @@ -322,7 +324,8 @@ public static <T> List<T> flatMap(@Nullable List<T> ls, BiFunction<Integer, T, @
* @param <T> The type of elements in the list.
* @return A new list with expanded or modified elements, or the original list if unchanged.
*/
public static <T> List<T> flatMap(@Nullable List<T> ls, Function<T, Object> flatMap) {
@Contract("null, _ -> null; !null, _ -> !null")
public static <T> @Nullable List<T> flatMap(@Nullable List<T> ls, Function<T, Object> flatMap) {
return flatMap(ls, (i, t) -> flatMap.apply(t));
}

Expand Down Expand Up @@ -355,6 +358,7 @@ public static <T> List<T> concat(@Nullable List<T> ls, @Nullable T t) {
* @return A new list with the added element at the start, the original list if the element is null or a null
* object if both element and list are null.
*/
@Contract("null, null -> null; !null, _ -> !null; _, !null -> !null")
public static <T> @Nullable List<T> concat(@Nullable T t, @Nullable List<T> ls) {
jevanlingen marked this conversation as resolved.
Show resolved Hide resolved
if (t == null && ls == null) {
//noinspection ConstantConditions
Expand All @@ -378,12 +382,11 @@ public static <T> List<T> concat(@Nullable List<T> ls, @Nullable T t) {
* @param <T> The type of elements in the lists.
* @return A new list containing both lists, or one of the lists if the other is null.
*/
@Contract("null, null -> null; !null, _ -> !null; _, !null -> !null")
public static <T> @Nullable List<T> concatAll(@Nullable List<T> ls, @Nullable List<? extends T> t) {
jevanlingen marked this conversation as resolved.
Show resolved Hide resolved
if (ls == null && t == null) {
//noinspection ConstantConditions
return null;
} else if (t == null || t.isEmpty()) {
//noinspection ConstantConditions
return ls;
} else if (ls == null || ls.isEmpty()) {
//noinspection unchecked
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public <H extends Hcl> H apply(Cursor scope, HclCoordinates coordinates, Object.
Space.Location loc = coordinates.getSpaceLocation();

//noinspection unchecked
H h = (H) new HclVisitor<Integer>() {
return (H) new HclVisitor<Integer>() {
@Override
public Hcl visitConfigFile(Hcl.ConfigFile configFile, Integer p) {
Hcl.ConfigFile c = (Hcl.ConfigFile) super.visitConfigFile(configFile, p);
Expand Down Expand Up @@ -166,10 +166,7 @@ public Hcl visitExpression(Expression expression, Integer p) {

return e;
}
}.visit(scope.getValue(), 0, scope.getParentOrThrow());

assert h != null;
return h;
}.visitNonNull(scope.getValue(), 0, scope.getParentOrThrow());
}

public static Builder builder(String code) {
Expand Down