Skip to content

Commit

Permalink
Change usages of <stream>.toList() to `<stream>.collect(Collectors.…
Browse files Browse the repository at this point in the history
…toList())`

Signed-off-by: Thomas Farr <[email protected]>
  • Loading branch information
Xtansia committed Jun 7, 2024
1 parent 779b09f commit 4d54e70
Show file tree
Hide file tree
Showing 12 changed files with 51 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,13 @@ public class Field {
@Nullable
private final Deprecation deprecation;

public Field(@Nonnull String wireName, @Nonnull Type type, boolean required, @Nullable String description, @Nullable Deprecation deprecation) {
public Field(
@Nonnull String wireName,
@Nonnull Type type,
boolean required,
@Nullable String description,
@Nullable Deprecation deprecation
) {
this.wireName = Strings.requireNonBlank(wireName, "wireName must not be null");
this.type = Objects.requireNonNull(type, "type must not be null");
this.required = required;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import javax.annotation.Nullable;
import org.opensearch.client.codegen.openapi.OpenApiOperation;
import org.opensearch.client.codegen.utils.Either;
import org.opensearch.client.codegen.utils.Lists;

public class HttpPath {
@Nonnull
Expand Down Expand Up @@ -60,7 +61,7 @@ private HttpPath(@Nonnull List<Part> parts, @Nullable Deprecation deprecation, @
}

public List<Field> getParams() {
return parts.stream().filter(Part::isParameter).map(Part::getParameter).toList();
return Lists.filterMap(parts, Part::isParameter, Part::getParameter);
}

public Set<String> getParamNameSet() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import javax.annotation.Nullable;
import org.opensearch.client.codegen.JavaFormatter;
import org.opensearch.client.codegen.exceptions.RenderException;
import org.opensearch.client.codegen.utils.Lists;
import org.opensearch.client.codegen.utils.Strings;

public class Namespace extends Shape {
Expand Down Expand Up @@ -99,7 +100,7 @@ public String getName() {
}

public Collection<Client> getChildren() {
return parent.children.values().stream().filter(n -> !n.operations.isEmpty()).map(n -> new Client(n, async)).toList();
return Lists.filterMap(parent.children.values(), n -> !n.operations.isEmpty(), n -> new Client(n, async));
}

public Collection<RequestShape> getOperations() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.opensearch.client.codegen.openapi.OpenApiSchemaFormat;
import org.opensearch.client.codegen.openapi.OpenApiSchemaType;
import org.opensearch.client.codegen.openapi.OpenApiSpecification;
import org.opensearch.client.codegen.utils.Lists;

public class SpecTransformer {
private static final Logger LOGGER = LogManager.getLogger();
Expand Down Expand Up @@ -236,12 +237,7 @@ private void visit(Namespace parent, String className, String typedefName, OpenA
visitInto(schema, objShape);
shape = objShape;
} else if (schema.isString() && schema.hasEnums()) {
shape = new EnumShape(
parent,
className,
schema.getEnums().orElseThrow().stream().map(EnumShape.Variant::new).toList(),
typedefName
);
shape = new EnumShape(parent, className, Lists.map(schema.getEnums().orElseThrow(), EnumShape.Variant::new), typedefName);
} else if (schema.hasOneOf()) {
var taggedUnion = new TaggedUnionShape(parent, className, typedefName);
schema.getOneOf().orElseThrow().forEach(s -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.opensearch.client.codegen.utils.Lists;

public class TaggedUnionShape extends ObjectShape {
private final List<Variant> variants = new ArrayList<>();
Expand All @@ -28,7 +29,7 @@ public Collection<Variant> getVariants() {
}

public Collection<Variant> getPrimitiveVariants() {
return variants.stream().filter(v -> v.getType().isPrimitive()).toList();
return Lists.filter(variants, v -> v.getType().isPrimitive());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ public static HttpMethod from(@Nonnull PathItem.HttpMethod httpMethod) {
return from(httpMethod.name().toLowerCase());
}


@Override
public String toString() {
return name().toLowerCase();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ <TIn, TOut> List<TOut> children(@Nonnull String key, @Nullable List<TIn> childre
Objects.requireNonNull(factory, "factory must not be null");
var basePtr = childPtr(key);
var self = self();
return Lists.transform(children, (i, v) -> factory.create(self, basePtr.append(String.valueOf(i)), v));
return Lists.map(children, (i, v) -> factory.create(self, basePtr.append(String.valueOf(i)), v));
}

@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ protected OpenApiParameter(@Nullable OpenApiElement<?> parent, @Nonnull JsonPoin
var extensions = parameter.getExtensions();
this.versionDeprecated = Maps.tryGet(extensions, "x-version-deprecated").map(String::valueOf).orElse(null);
this.deprecationMessage = Maps.tryGet(extensions, "x-deprecation-message").map(String::valueOf).orElse(null);
this.isGlobal = (Boolean) Maps.tryGet(extensions, "x-global")
.orElse(null);
this.isGlobal = (Boolean) Maps.tryGet(extensions, "x-global").orElse(null);
}

@Nonnull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ protected OpenApiSchema(@Nullable OpenApiElement<?> parent, @Nonnull JsonPointer
allOf = children("allOf", schema.getAllOf(), OpenApiSchema::new);
oneOf = children("oneOf", schema.getOneOf(), OpenApiSchema::new);

enums = ifNonnull(schema.getEnum(), e -> Lists.transform(e, String::valueOf));
enums = ifNonnull(schema.getEnum(), e -> Lists.map(e, String::valueOf));

items = child("items", schema.getItems(), OpenApiSchema::new);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@

package org.opensearch.client.codegen.utils;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import javax.annotation.Nonnull;
Expand All @@ -27,22 +29,41 @@ public static <T> Optional<List<T>> unmodifiableOpt(@Nullable List<T> list) {
}

@Nonnull
public static <TOld, TNew> List<TNew> transform(@Nonnull List<TOld> list, @Nonnull Function<TOld, TNew> function) {
public static <T> List<T> filter(@Nonnull Collection<T> list, @Nonnull Predicate<T> predicate) {
Objects.requireNonNull(list, "list must not be null");
Objects.requireNonNull(function, "function must not be null");
return list.stream().map(function).collect(Collectors.toList());
Objects.requireNonNull(predicate, "predicate must not be null");
return list.stream().filter(predicate).collect(Collectors.toList());
}

@Nonnull
public static <TOld, TNew> List<TNew> transform(@Nonnull List<TOld> list, @Nonnull ItemMapper<TOld, TNew> function) {
public static <TIn, TOut> List<TOut> map(@Nonnull Collection<TIn> list, @Nonnull Function<TIn, TOut> mapper) {
Objects.requireNonNull(list, "list must not be null");
Objects.requireNonNull(function, "function must not be null");
return IntStream.range(0, list.size()).mapToObj(i -> function.map(i, list.get(i))).toList();
Objects.requireNonNull(mapper, "mapper must not be null");
return list.stream().map(mapper).collect(Collectors.toList());
}

@Nonnull
public static <TIn, TOut> List<TOut> map(@Nonnull List<TIn> list, @Nonnull ItemMapper<TIn, TOut> mapper) {
Objects.requireNonNull(list, "list must not be null");
Objects.requireNonNull(mapper, "mapper must not be null");
return IntStream.range(0, list.size()).mapToObj(i -> mapper.map(i, list.get(i))).collect(Collectors.toList());
}

@FunctionalInterface
public interface ItemMapper<TIn, TOut> {
@Nonnull
TOut map(int index, @Nonnull TIn item);
}

@Nonnull
public static <TIn, TOut> List<TOut> filterMap(
@Nonnull Collection<TIn> list,
@Nonnull Predicate<TIn> predicate,
@Nonnull Function<TIn, TOut> mapper
) {
Objects.requireNonNull(list, "list must not be null");
Objects.requireNonNull(predicate, "predicate must not be null");
Objects.requireNonNull(mapper, "mapper must not be null");
return list.stream().filter(predicate).map(mapper).collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,10 @@ public MavenArtifactResolver(
public Set<File> resolve(boolean withTransitives, @Nonnull Collection<String> mavenCoordinates) {
Objects.requireNonNull(mavenCoordinates, "mavenCoordinates must not be null");

var dependencies = mavenCoordinates.stream()
.map(coord -> new Dependency(new DefaultArtifact(coord), null, null, withTransitives ? null : EXCLUDE_ALL_TRANSITIVES))
.toList();
var dependencies = Lists.map(
mavenCoordinates,
coord -> new Dependency(new DefaultArtifact(coord), null, null, withTransitives ? null : EXCLUDE_ALL_TRANSITIVES)
);

var request = new DependencyRequest(new CollectRequest(dependencies, null, repositories), null);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ public void unmodifiableOpt_withNonNullList_shouldReturnUnmodifiableList() {
}

@Test
public void transform_shouldTransformIntoNewList() {
public void map_shouldMapIntoNewList() {
var input = new ArrayList<>() {
{
add("foobar");
add("hello world");
}
};

var output = Lists.transform(input, (i, v) -> i + "-" + v);
var output = Lists.map(input, (i, v) -> i + "-" + v);

assertEquals(List.of("foobar", "hello world"), input);
assertEquals(List.of("0-foobar", "1-hello world"), output);
Expand Down

0 comments on commit 4d54e70

Please sign in to comment.