Skip to content

Instantly share code, notes, and snippets.

@pfichtner
Created December 6, 2021 15:49
Show Gist options
  • Select an option

  • Save pfichtner/ab3507ca2b6dafef9967cfa658a20d36 to your computer and use it in GitHub Desktop.

Select an option

Save pfichtner/ab3507ca2b6dafef9967cfa658a20d36 to your computer and use it in GitHub Desktop.
SonarSweep
package sonarsweep;
import static java.lang.Integer.MAX_VALUE;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.IntPredicate;
import java.util.stream.IntStream;
import org.junit.jupiter.api.Test;
class SonarSweepTest {
@Test
void test() {
int[] values = { 199, 200, 208, 210, 200, 207, 240, 269, 260, 263 };
assertThat(countIncreased(values), is(7L));
}
long countIncreased(int[] values) {
return IntStream.of(values).filter(isIncreased()).count();
}
static IntPredicate isIncreased() {
AtomicInteger previous = new AtomicInteger(MAX_VALUE);
return i -> i > previous.getAndSet(i);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment