Replace Runnable with new Action interface
This commit is contained in:
parent
5101768f57
commit
b6801cf89e
5 changed files with 46 additions and 10 deletions
36
src/main/java/net/kemitix/conditional/Action.java
Normal file
36
src/main/java/net/kemitix/conditional/Action.java
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
/**
|
||||||
|
* The MIT License (MIT)
|
||||||
|
*
|
||||||
|
* Copyright (c) 2017 Paul Campbell
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
|
||||||
|
* and associated documentation files (the "Software"), to deal in the Software without restriction,
|
||||||
|
* including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||||
|
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
|
||||||
|
* subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all copies
|
||||||
|
* or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
||||||
|
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
|
||||||
|
* AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
||||||
|
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package net.kemitix.conditional;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An action to perform in a clause when a {@link Condition} is met.
|
||||||
|
*
|
||||||
|
* @author Paul Campbell (pcampbell@kemitix.net)
|
||||||
|
*/
|
||||||
|
@FunctionalInterface
|
||||||
|
public interface Action {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The action to perform.
|
||||||
|
*/
|
||||||
|
void perform();
|
||||||
|
}
|
|
@ -98,14 +98,14 @@ public interface Condition {
|
||||||
*
|
*
|
||||||
* @return the Condition
|
* @return the Condition
|
||||||
*/
|
*/
|
||||||
Condition then(Runnable response);
|
Condition then(Action response);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Perform this response if the {@code Condition} is {@code false}.
|
* Perform this response if the {@code Condition} is {@code false}.
|
||||||
*
|
*
|
||||||
* @param response the response to perform
|
* @param response the response to perform
|
||||||
*/
|
*/
|
||||||
void otherwise(Runnable response);
|
void otherwise(Action response);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new {@code Condition} for the clause as a continuation to an existing {@code Condition}.
|
* Create a new {@code Condition} for the clause as a continuation to an existing {@code Condition}.
|
||||||
|
|
|
@ -41,13 +41,13 @@ final class FalseCondition implements Condition {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Condition then(final Runnable response) {
|
public Condition then(final Action response) {
|
||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void otherwise(final Runnable response) {
|
public void otherwise(final Action response) {
|
||||||
response.run();
|
response.perform();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -41,13 +41,13 @@ final class TrueCondition implements Condition {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Condition then(final Runnable response) {
|
public Condition then(final Action response) {
|
||||||
response.run();
|
response.perform();
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void otherwise(final Runnable response) {
|
public void otherwise(final Action response) {
|
||||||
// do nothing
|
// do nothing
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -10,9 +10,9 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||||
*/
|
*/
|
||||||
public class ConditionalTest {
|
public class ConditionalTest {
|
||||||
|
|
||||||
private Runnable thenResponse;
|
private Action thenResponse;
|
||||||
|
|
||||||
private Runnable otherwiseResponse;
|
private Action otherwiseResponse;
|
||||||
|
|
||||||
private boolean thenFlag;
|
private boolean thenFlag;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue