From 9259081410ef53fa4c6ce905cd24d1f4e5298f78 Mon Sep 17 00:00:00 2001 From: Carter Kozak Date: Thu, 15 Aug 2019 12:16:23 -0400 Subject: [PATCH 1/3] SortedFirst refactor implementation for no-arg `sort()` --- .../baseline/refaster/SortedFirstNatural.java | 41 +++++++++++++++++ .../refaster/SortedFirstNaturalTest.java | 45 +++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 baseline-refaster-rules/src/main/java/com/palantir/baseline/refaster/SortedFirstNatural.java create mode 100644 baseline-refaster-rules/src/test/java/com/palantir/baseline/refaster/SortedFirstNaturalTest.java 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..d3bf7aa17 --- /dev/null +++ b/baseline-refaster-rules/src/main/java/com/palantir/baseline/refaster/SortedFirstNatural.java @@ -0,0 +1,41 @@ +/* + * (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..2b0088ff2 --- /dev/null +++ b/baseline-refaster-rules/src/test/java/com/palantir/baseline/refaster/SortedFirstNaturalTest.java @@ -0,0 +1,45 @@ +/* + * (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.stream.Stream;", + "public class Test {", + " Optional i = Arrays.asList(5, -10, 7, -18, 23).stream()" + + ".min(Comparator.naturalOrder());", + "}"); + } + +} From ed09ad29a16451d19247cdf1f5717f9081bc776c Mon Sep 17 00:00:00 2001 From: Carter Kozak Date: Thu, 15 Aug 2019 16:16:23 +0000 Subject: [PATCH 2/3] Add generated changelog entries --- changelog/@unreleased/pr-752.v2.yml | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 changelog/@unreleased/pr-752.v2.yml 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 From 805976f6033d8526caea16c93bcca7dfc6b1a140 Mon Sep 17 00:00:00 2001 From: Carter Kozak Date: Thu, 15 Aug 2019 12:37:16 -0400 Subject: [PATCH 3/3] style --- .../java/com/palantir/baseline/refaster/SortedFirstNatural.java | 1 - .../com/palantir/baseline/refaster/SortedFirstNaturalTest.java | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) 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 index d3bf7aa17..e53f9ea11 100644 --- 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 @@ -18,7 +18,6 @@ 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; 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 index 2b0088ff2..23e7ba6fb 100644 --- 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 @@ -35,6 +35,7 @@ public void test() { "}") .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()"