Skip to content

Instantly share code, notes, and snippets.

@rabestro
Last active June 15, 2023 15:44

Revisions

  1. Jegors Čemisovs revised this gist Jun 15, 2023. 1 changed file with 21 additions and 0 deletions.
    21 changes: 21 additions & 0 deletions FlipFlopSpec.groovy
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    class FlipFlopPredicateSpec extends Specification {

    def 'the flip-flop predicate returns false until the first condition is met'() {

    given: 'the flip-flop left and right side predicates'
    def leftSide = 'ON'::equals
    def rightSide = 'OFF'::equals

    when: 'we create a flip-flop predicate'
    def flipFlop = FlipFlopPredicate.flipFlop(leftSide, rightSide)

    then: 'the result is false while the left side is not met'
    flipFlop.test('A') == false

    then: 'the result is true when the left side is met'
    flipFlop.test('ON') == true

    then: 'the result is true until the right side is met'
    flipFlop.test('A') == true
    }
    }
  2. Jegors Čemisovs created this gist Jun 15, 2023.
    21 changes: 21 additions & 0 deletions CopilotTest.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    package com.epam.flipflop;

    import org.junit.jupiter.api.Test;

    import static org.junit.jupiter.api.Assertions.assertFalse;
    import static org.junit.jupiter.api.Assertions.assertTrue;
    import java.util.function.Predicate;

    class CopilotTest {
    @Test
    void testFlipFlop() {
    var flipFlop = new FlipFlop<Integer>(x -> x % 2 == 0, x -> x % 3 == 0);

    assertTrue(flipFlop.test(2));
    assertTrue(flipFlop.test(3));
    assertTrue(flipFlop.test(4));
    assertTrue(flipFlop.test(6));
    assertFalse(flipFlop.test(9));
    assertTrue(flipFlop.test(10));
    }
    }
    21 changes: 21 additions & 0 deletions FlipFlopPredicate.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    package org.epam.flipflop;

    import java.util.function.Predicate;

    public final class FlipFlopPredicate<T> implements Predicate<T> {
    private boolean state;
    private final Predicate<T> lhs;
    private final Predicate<T> rhs;

    public FlipFlopPredicate(Predicate<T> leftHandSide, Predicate<T> rightHandSide) {
    this.lhs = leftHandSide;
    this.rhs = rightHandSide;
    }

    @Override
    public boolean test(T value) {
    var result = state || lhs.test(value);
    state = result && !rhs.test(value);
    return result;
    }
    }
    54 changes: 54 additions & 0 deletions FlipFlopPredicateTest.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,54 @@
    package com.epam.flipflop;

    import org.junit.jupiter.api.DisplayNameGeneration;
    import org.junit.jupiter.api.DisplayNameGenerator;
    import org.junit.jupiter.api.Test;

    import java.util.function.Predicate;
    import java.util.stream.Stream;

    import static com.epam.flipflop.FlipFlopPredicate.flipFlop;
    import static org.assertj.core.api.Assertions.assertThat;


    @DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
    class FlipFlopPredicate2Test {

    @Test
    void extract_all_javadoc_by_flip_flop_predicate() {
    // sample test data
    var sampleSourceCode = """
    package sample;
    /**
    * Multiline javadoc
    */
    void someMethod() {}
    // single-line comment
    /** single-line javadoc */
    /*
    * Multiline comment
    */
    """;

    // given
    Predicate<String> javaDocOpen = line -> line.startsWith("/**");
    Predicate<String> javaDocClose = line -> line.endsWith("*/");

    // when
    var javaDocsLines = sampleSourceCode
    .lines()
    .filter(flipFlop(javaDocOpen, javaDocClose));

    // then
    assertThat(javaDocsLines)
    .as("JavaDoc comments extracted from the source code")
    .containsExactly(
    "/**",
    " * Multiline javadoc",
    " */",
    "/** single-line javadoc */");
    }
    }