Skip to content

Commit

Permalink
SortedFirst refactor implementation for no-arg sort() (#752)
Browse files Browse the repository at this point in the history
Refaster `stream.sorted().findFirst()` into `stream.min(Comparator.naturalOrder())`
  • Loading branch information
carterkozak authored and bulldozer-bot[bot] committed Aug 15, 2019
1 parent cc49f6e commit 3c42c39
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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<T extends Comparable<T>> {

@BeforeTemplate
Optional<T> before(Stream<T> stream) {
return stream.sorted().findFirst();
}

@AfterTemplate
Optional<T> after(Stream<T> stream) {
return stream.min(Comparator.naturalOrder());
}

}
Original file line number Diff line number Diff line change
@@ -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<Integer> 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<Integer> i = Arrays.asList(5, -10, 7, -18, 23).stream()"
+ ".min(Comparator.naturalOrder());",
"}");
}

}
5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-752.v2.yml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 3c42c39

Please sign in to comment.