-
Notifications
You must be signed in to change notification settings - Fork 306
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
17ab5d6
commit c583cf6
Showing
2 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package org.schemaspy.util; | ||
|
||
import java.util.function.Consumer; | ||
import java.util.function.Predicate; | ||
|
||
public class WhenIf<T> implements Predicate<T> { | ||
|
||
private final Predicate<T> origin; | ||
private final Consumer<T> onTrue; | ||
private final Consumer<T> onFalse; | ||
|
||
public WhenIf( | ||
final Predicate<T> origin, | ||
final Consumer<T> onTrue, | ||
final Consumer<T> onFalse | ||
) { | ||
this.origin = origin; | ||
this.onTrue = onTrue; | ||
this.onFalse = onFalse; | ||
} | ||
|
||
@Override | ||
public boolean test(final T t) { | ||
if (this.origin.test(t)) { | ||
this.onTrue.accept(t); | ||
return true; | ||
} else { | ||
this.onFalse.accept(t); | ||
return false; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package org.schemaspy.util; | ||
|
||
import java.util.concurrent.atomic.AtomicBoolean; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import org.junit.jupiter.api.Test; | ||
|
||
/** | ||
* Tests for {@link WhenIf}. | ||
*/ | ||
class WhenIfTest { | ||
|
||
@Test | ||
void actOnFalse() { | ||
AtomicBoolean onTrue = new AtomicBoolean(false); | ||
AtomicBoolean onFalse = new AtomicBoolean(false); | ||
new WhenIf<>( | ||
a -> false, | ||
b -> onTrue.set(true), | ||
c -> onFalse.set(true) | ||
).test(new Object()); | ||
assertThat(onTrue).isFalse(); | ||
assertThat(onFalse).isTrue(); | ||
} | ||
|
||
@Test | ||
void actOnTrue() { | ||
AtomicBoolean onTrue = new AtomicBoolean(false); | ||
AtomicBoolean onFalse = new AtomicBoolean(false); | ||
new WhenIf<>( | ||
a -> true, | ||
b -> onTrue.set(true), | ||
c -> onFalse.set(true) | ||
).test(new Object()); | ||
assertThat(onTrue).isTrue(); | ||
assertThat(onFalse).isFalse(); | ||
} | ||
} |