diff --git a/baseline-refaster-rules/src/main/java/com/palantir/baseline/refaster/SortedFirstNatural.java b/baseline-refaster-rules/src/main/java/com/palantir/baseline/refaster/SortedFirstNatural.java new file mode 100644 index 000000000..e53f9ea11 --- /dev/null +++ b/baseline-refaster-rules/src/main/java/com/palantir/baseline/refaster/SortedFirstNatural.java @@ -0,0 +1,40 @@ +/* + * (c) Copyright 2019 Palantir Technologies Inc. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.palantir.baseline.refaster; + +import com.google.errorprone.refaster.annotation.AfterTemplate; +import com.google.errorprone.refaster.annotation.BeforeTemplate; +import java.util.Comparator; +import java.util.Optional; +import java.util.stream.Stream; + +/** + * Based on {@link SortedFirst}, but handles {@link Stream#sorted()} without a comparator. + */ +public final class SortedFirstNatural> { + + @BeforeTemplate + Optional before(Stream stream) { + return stream.sorted().findFirst(); + } + + @AfterTemplate + Optional after(Stream stream) { + return stream.min(Comparator.naturalOrder()); + } + +} diff --git a/baseline-refaster-rules/src/test/java/com/palantir/baseline/refaster/SortedFirstNaturalTest.java b/baseline-refaster-rules/src/test/java/com/palantir/baseline/refaster/SortedFirstNaturalTest.java new file mode 100644 index 000000000..23e7ba6fb --- /dev/null +++ b/baseline-refaster-rules/src/test/java/com/palantir/baseline/refaster/SortedFirstNaturalTest.java @@ -0,0 +1,46 @@ +/* + * (c) Copyright 2019 Palantir Technologies Inc. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.palantir.baseline.refaster; + +import org.junit.Test; + +public class SortedFirstNaturalTest { + + @Test + public void test() { + RefasterTestHelper + .forRefactoring(SortedFirstNatural.class) + .withInputLines( + "Test", + "import java.util.*;", + "import java.util.stream.Stream;", + "public class Test {", + " Optional i = Arrays.asList(5, -10, 7, -18, 23).stream()", + " .sorted()", + " .findFirst();", + "}") + .hasOutputLines( + "import java.util.*;", + "import java.util.Comparator;", + "import java.util.stream.Stream;", + "public class Test {", + " Optional i = Arrays.asList(5, -10, 7, -18, 23).stream()" + + ".min(Comparator.naturalOrder());", + "}"); + } + +} diff --git a/changelog/@unreleased/pr-752.v2.yml b/changelog/@unreleased/pr-752.v2.yml new file mode 100644 index 000000000..972a093b2 --- /dev/null +++ b/changelog/@unreleased/pr-752.v2.yml @@ -0,0 +1,5 @@ +type: improvement +improvement: + description: Refaster `stream.sorted().findFirst()` into `stream.min(Comparator.naturalOrder())` + links: + - https://github.com/palantir/gradle-baseline/pull/752