Fix API issues (#43)

* Deprecate Condition.*Throw(Exception) and add Supplier versions

* Added: Condition.and(boolean) and Condition.or(boolean)
This commit is contained in:
Paul Campbell 2019-01-27 12:43:58 +00:00 committed by GitHub
parent 1fa6a3c05a
commit d9f1b22b2f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 309 additions and 30 deletions

View file

@ -5,12 +5,29 @@ All notable changes to this project will be documented in this file.
The format is based on [[https://keepachangelog.com/en/1.0.0/][Keep a Changelog]], and this project adheres to The format is based on [[https://keepachangelog.com/en/1.0.0/][Keep a Changelog]], and this project adheres to
[[https://semver.org/spec/v2.0.0.html][Semantic Versioning]]. [[https://semver.org/spec/v2.0.0.html][Semantic Versioning]].
* 0.7.1 * 1.0.0
** Added
- Bump assertj-core from 3.11.0 to 3.11.1 (#38) - ~Condition.thenThrow(Supplier<Exception>)~
- Bump kemitix-maven-tiles from 0.9.0 to 1.3.1 (#40) - ~Condition.otherwiseThrow(Supplier<Exception>)~
- [jenkins] Compatibility build against java 11 (#40) - ~Condition.and(boolean)~
- Bump kemitix-parent from 5.1.1 to 5.2.0 (#39) - ~Condition.or(boolean)~
** Changed
- [jenkins] Compatibility build against java 11 (#40)
** Deprecated
- ~Condition.thenThrow(Exception)~
- ~Condition.otherwiseThrow(Exception)~
** Dependencies
- Bump assertj-core from 3.11.0 to 3.11.1 (#38)
- Bump kemitix-maven-tiles from 0.9.0 to 1.3.1 (#40)
- Bump kemitix-parent from 5.1.1 to 5.2.0 (#39)
* 0.7.0 * 0.7.0

View file

@ -77,7 +77,7 @@ public interface Condition {
Condition and(Supplier<Boolean> clause); Condition and(Supplier<Boolean> clause);
/** /**
* Logicaly OR current {@code Condition} with the other {@code Condition}. * Logically AND current {@code Condition} with the other {@code Condition}.
* *
* @param other the other Condition * @param other the other Condition
* @return true if both Conditions are true * @return true if both Conditions are true
@ -86,6 +86,16 @@ public interface Condition {
return Condition.where(isTrue()).and(other::isTrue); return Condition.where(isTrue()).and(other::isTrue);
} }
/**
* Logically AND current {@code Condition} with the other boolean.
*
* @param other the other boolean
* @return true if both Conditions are true
*/
default Condition and(boolean other) {
return Condition.where(isTrue()).and(Condition.where(other));
}
/** /**
* Logically OR combine the current {@code Condition} with the clause. * Logically OR combine the current {@code Condition} with the clause.
* *
@ -105,6 +115,16 @@ public interface Condition {
return where(isTrue()).or(other::isTrue); return where(isTrue()).or(other::isTrue);
} }
/**
* Logically OR the current {@code Condition} with the other boolean.
*
* @param other the other boolean
* @return true if either Condition is true
*/
default Condition or(boolean other) {
return where(isTrue()).or(Condition.where(other));
}
/** /**
* Perform this response if the {@code Condition} is {@code true}. * Perform this response if the {@code Condition} is {@code true}.
* *
@ -131,23 +151,45 @@ public interface Condition {
} }
/** /**
* Throw then exception if the {@code Condition} is {@code true}. * Throw the exception if the {@code Condition} is {@code true}.
* *
* @param exception the Exception to throw * @param exception the Exception to throw
* @throws Exception the exception * @throws Exception the exception
* @deprecated use {@link #thenThrow(Supplier)}
*/ */
@Deprecated
@SuppressWarnings(SuppressHelper.CS_ILLEGALTHROWS) @SuppressWarnings(SuppressHelper.CS_ILLEGALTHROWS)
void thenThrow(Exception exception) throws Exception; void thenThrow(Exception exception) throws Exception;
/**
* Throw the exception supplied if the {@code Condition} is {@code true}.
*
* @param exceptionSupplier the supplier of the Exception to throw
* @throws Exception the exception
*/
@SuppressWarnings(SuppressHelper.CS_ILLEGALTHROWS)
void thenThrow(Supplier<Exception> exceptionSupplier) throws Exception;
/** /**
* Throw then exception if the {@code Condition} is {@code false}. * Throw then exception if the {@code Condition} is {@code false}.
* *
* @param exception the Exception to throw * @param exception the Exception to throw
* @throws Exception the exception * @throws Exception the exception
* @deprecated use {@link #otherwiseThrow(Supplier)}
*/ */
@Deprecated
@SuppressWarnings(SuppressHelper.CS_ILLEGALTHROWS) @SuppressWarnings(SuppressHelper.CS_ILLEGALTHROWS)
void otherwiseThrow(Exception exception) throws Exception; void otherwiseThrow(Exception exception) throws Exception;
/**
* Throw then exception if the {@code Condition} is {@code false}.
*
* @param exceptionSupplier the supplier of the Exception to throw
* @throws Exception the exception
*/
@SuppressWarnings(SuppressHelper.CS_ILLEGALTHROWS)
void otherwiseThrow(Supplier<Exception> exceptionSupplier) throws Exception;
/** /**
* Apply the function to the Condtion, resulting an another Condition. * Apply the function to the Condtion, resulting an another Condition.
* *

View file

@ -58,11 +58,21 @@ final class FalseCondition implements Condition {
// do nothing // do nothing
} }
@Override
public void thenThrow(final Supplier<Exception> exceptionSupplier) throws Exception {
// do nothing
}
@Override @Override
public void otherwiseThrow(final Exception exception) throws Exception { public void otherwiseThrow(final Exception exception) throws Exception {
throw exception; throw exception;
} }
@Override
public void otherwiseThrow(final Supplier<Exception> exceptionSupplier) throws Exception {
throw exceptionSupplier.get();
}
@Override @Override
public boolean isTrue() { public boolean isTrue() {
return false; return false;

View file

@ -59,11 +59,21 @@ final class TrueCondition implements Condition {
throw exception; throw exception;
} }
@Override
public void thenThrow(final Supplier<Exception> exceptionSupplier) throws Exception {
throw exceptionSupplier.get();
}
@Override @Override
public void otherwiseThrow(final Exception exception) throws Exception { public void otherwiseThrow(final Exception exception) throws Exception {
// do nothing // do nothing
} }
@Override
public void otherwiseThrow(final Supplier<Exception> exceptionSupplier) throws Exception {
// do nothing
}
@Override @Override
public boolean isTrue() { public boolean isTrue() {
return true; return true;

View file

@ -30,7 +30,7 @@ public class ConditionalTest implements WithAssertions {
@Test @Test
public void whereTrueThenRuns() { public void whereTrueThenRuns() {
//when //when
when(true); whenAndCondition(true);
//then //then
assertThatTheThenResponseRuns(); assertThatTheThenResponseRuns();
} }
@ -38,39 +38,103 @@ public class ConditionalTest implements WithAssertions {
@Test @Test
public void whereFalseOtherwiseRuns() { public void whereFalseOtherwiseRuns() {
//when //when
when(false); whenAndCondition(false);
//then //then
assertThatTheOtherwiseResponseRuns(); assertThatTheOtherwiseResponseRuns();
} }
@Test @Test
public void whereTrueAndTrueThenRuns() { public void whereTrueAndSupplierTrueThenRuns() {
//when //when
when(true, true); whenAndSupplier(true, true);
//then //then
assertThatTheThenResponseRuns(); assertThatTheThenResponseRuns();
} }
@Test @Test
public void whereTrueAndFalseThenOtherwiseRuns() { public void whereTrueAndSupplierFalseThenOtherwiseRuns() {
//when //when
when(true, false); whenAndSupplier(true, false);
//then //then
assertThatTheOtherwiseResponseRuns(); assertThatTheOtherwiseResponseRuns();
} }
@Test @Test
public void whereFalseAndTrueThenOtherwiseRuns() { public void whereFalseAndSupplierTrueThenOtherwiseRuns() {
//when //when
when(false, true); whenAndSupplier(false, true);
//then //then
assertThatTheOtherwiseResponseRuns(); assertThatTheOtherwiseResponseRuns();
} }
@Test @Test
public void whereFalseAndFalseThenOtherwiseRuns() { public void whereFalseAndSupplierFalseThenOtherwiseRuns() {
//when //when
when(false, false); whenAndSupplier(false, false);
//then
assertThatTheOtherwiseResponseRuns();
}
@Test
public void whereTrueAndConditionTrueThenRuns() {
//when
whenAndCondition(true, true);
//then
assertThatTheThenResponseRuns();
}
@Test
public void whereTrueAndConditionFalseThenOtherwiseRuns() {
//when
whenAndCondition(true, false);
//then
assertThatTheOtherwiseResponseRuns();
}
@Test
public void whereFalseAndTrueConditionThenOtherwiseRuns() {
//when
whenAndCondition(false, true);
//then
assertThatTheOtherwiseResponseRuns();
}
@Test
public void whereFalseAndConditionFalseThenOtherwiseRuns() {
//when
whenAndCondition(false, false);
//then
assertThatTheOtherwiseResponseRuns();
}
@Test
public void whereTrueAndBooleanTrueThenRuns() {
//when
whenAndBoolean(true, true);
//then
assertThatTheThenResponseRuns();
}
@Test
public void whereTrueAndBooleanFalseThenOtherwiseRuns() {
//when
whenAndBoolean(true, false);
//then
assertThatTheOtherwiseResponseRuns();
}
@Test
public void whereFalseAndBooleanTrueThenOtherwiseRuns() {
//when
whenAndBoolean(false, true);
//then
assertThatTheOtherwiseResponseRuns();
}
@Test
public void whereFalseAndBooleanFalseThenOtherwiseRuns() {
//when
whenAndBoolean(false, false);
//then //then
assertThatTheOtherwiseResponseRuns(); assertThatTheOtherwiseResponseRuns();
} }
@ -87,33 +151,97 @@ public class ConditionalTest implements WithAssertions {
} }
@Test @Test
public void whereTrueOrTrueThenDoSomething() { public void whereTrueOrSupplierTrueThenDoSomething() {
//when //when
whenOr(true, true); whenOrSupplier(true, true);
//then //then
assertThatTheThenResponseRuns(); assertThatTheThenResponseRuns();
} }
@Test @Test
public void whereTrueOrFalseThenDoSomething() { public void whereTrueOrSupplierFalseThenDoSomething() {
//when //when
whenOr(true, false); whenOrSupplier(true, false);
//then //then
assertThatTheThenResponseRuns(); assertThatTheThenResponseRuns();
} }
@Test @Test
public void whereFalseOrTrueThenDoSomething() { public void whereFalseOrSupplierTrueThenDoSomething() {
//when //when
whenOr(false, true); whenOrSupplier(false, true);
//then //then
assertThatTheThenResponseRuns(); assertThatTheThenResponseRuns();
} }
@Test @Test
public void whereFalseOrFalseThenDoSomethingElse() { public void whereFalseOrSupplierFalseThenDoSomethingElse() {
//when //when
whenOr(false, false); whenOrSupplier(false, false);
//then
assertThatTheOtherwiseResponseRuns();
}
@Test
public void whereTrueOrConditionTrueThenDoSomething() {
//when
whenOrCondition(true, true);
//then
assertThatTheThenResponseRuns();
}
@Test
public void whereTrueOrConditionFalseThenDoSomething() {
//when
whenOrCondition(true, false);
//then
assertThatTheThenResponseRuns();
}
@Test
public void whereFalseOrConditionTrueThenDoSomething() {
//when
whenOrCondition(false, true);
//then
assertThatTheThenResponseRuns();
}
@Test
public void whereFalseOrConditionFalseThenDoSomethingElse() {
//when
whenOrCondition(false, false);
//then
assertThatTheOtherwiseResponseRuns();
}
@Test
public void whereTrueOrBooleanTrueThenDoSomething() {
//when
whenOrBoolean(true, true);
//then
assertThatTheThenResponseRuns();
}
@Test
public void whereTrueOrBooleanFalseThenDoSomething() {
//when
whenOrBoolean(true, false);
//then
assertThatTheThenResponseRuns();
}
@Test
public void whereFalseOrBooleanTrueThenDoSomething() {
//when
whenOrBoolean(false, true);
//then
assertThatTheThenResponseRuns();
}
@Test
public void whereFalseOrBooleanFalseThenDoSomethingElse() {
//when
whenOrBoolean(false, false);
//then //then
assertThatTheOtherwiseResponseRuns(); assertThatTheOtherwiseResponseRuns();
} }
@ -261,13 +389,23 @@ public class ConditionalTest implements WithAssertions {
assertThatTheOtherwiseResponseDidNotRun(); assertThatTheOtherwiseResponseDidNotRun();
} }
private void when(final boolean clause) { private void whenAndCondition(final boolean clause) {
Condition.where(clause) Condition.where(clause)
.then(thenResponse) .then(thenResponse)
.otherwise(otherwiseResponse); .otherwise(otherwiseResponse);
} }
private void when( private void whenAndCondition(
final boolean firstClause,
final boolean secondClause
) {
Condition.where(firstClause)
.and(Condition.where(secondClause))
.then(thenResponse)
.otherwise(otherwiseResponse);
}
private void whenAndSupplier(
final boolean firstClause, final boolean firstClause,
final boolean secondClause final boolean secondClause
) { ) {
@ -277,7 +415,27 @@ public class ConditionalTest implements WithAssertions {
.otherwise(otherwiseResponse); .otherwise(otherwiseResponse);
} }
private void whenOr( private void whenAndBoolean(
final boolean firstClause,
final boolean secondClause
) {
Condition.where(firstClause)
.and(secondClause)
.then(thenResponse)
.otherwise(otherwiseResponse);
}
private void whenOrCondition(
final boolean firstClause,
final boolean secondClause
) {
Condition.where(firstClause)
.or(Condition.where(secondClause))
.then(thenResponse)
.otherwise(otherwiseResponse);
}
private void whenOrSupplier(
final boolean firstClause, final boolean firstClause,
final boolean secondClause final boolean secondClause
) { ) {
@ -287,8 +445,18 @@ public class ConditionalTest implements WithAssertions {
.otherwise(otherwiseResponse); .otherwise(otherwiseResponse);
} }
private void whenOrBoolean(
final boolean firstClause,
final boolean secondClause
) {
Condition.where(firstClause)
.or(secondClause)
.then(thenResponse)
.otherwise(otherwiseResponse);
}
@Test @Test
public void shortCurcuitOr() { public void shortCircuitOr() {
//given //given
final AtomicInteger atomicInteger = new AtomicInteger(); final AtomicInteger atomicInteger = new AtomicInteger();
//when //when
@ -301,7 +469,7 @@ public class ConditionalTest implements WithAssertions {
} }
@Test @Test
public void shortCurcuitAnd() { public void shortCircuitAnd() {
//given //given
final AtomicInteger atomicInteger = new AtomicInteger(); final AtomicInteger atomicInteger = new AtomicInteger();
//when //when
@ -315,6 +483,14 @@ public class ConditionalTest implements WithAssertions {
@Test @Test
public void whereTrueThenThrowException() { public void whereTrueThenThrowException() {
//given
assertThatExceptionOfType(IOException.class)
.isThrownBy(() -> Condition.where(true)
.thenThrow(IOException::new));
}
@Test
public void whereTrueThenThrowExceptionDeprecated() {
//given //given
assertThatExceptionOfType(IOException.class) assertThatExceptionOfType(IOException.class)
.isThrownBy(() -> Condition.where(true) .isThrownBy(() -> Condition.where(true)
@ -323,6 +499,14 @@ public class ConditionalTest implements WithAssertions {
@Test @Test
public void whereFalseThenDoNotThrowException() throws Exception { public void whereFalseThenDoNotThrowException() throws Exception {
assertThatCode(() ->
Condition.where(false)
.thenThrow(IOException::new))
.doesNotThrowAnyException();
}
@Test
public void whereFalseThenDoNotThrowExceptionDeprecated() throws Exception {
assertThatCode(() -> assertThatCode(() ->
Condition.where(false) Condition.where(false)
.thenThrow(new IOException())) .thenThrow(new IOException()))
@ -331,6 +515,14 @@ public class ConditionalTest implements WithAssertions {
@Test @Test
public void whereFalseOtherwiseThenThrowException() { public void whereFalseOtherwiseThenThrowException() {
//given
assertThatExceptionOfType(IOException.class)
.isThrownBy(() -> Condition.where(false)
.otherwiseThrow(IOException::new));
}
@Test
public void whereFalseOtherwiseThenThrowExceptionDeprecated() {
//given //given
assertThatExceptionOfType(IOException.class) assertThatExceptionOfType(IOException.class)
.isThrownBy(() -> Condition.where(false) .isThrownBy(() -> Condition.where(false)
@ -339,6 +531,14 @@ public class ConditionalTest implements WithAssertions {
@Test @Test
public void whereTrueOtherwiseThenDoNotThrowException() throws Exception { public void whereTrueOtherwiseThenDoNotThrowException() throws Exception {
assertThatCode(() ->
Condition.where(true)
.otherwiseThrow(IOException::new))
.doesNotThrowAnyException();
}
@Test
public void whereTrueOtherwiseThenDoNotThrowExceptionDeprecated() throws Exception {
assertThatCode(() -> assertThatCode(() ->
Condition.where(true) Condition.where(true)
.otherwiseThrow(new IOException())) .otherwiseThrow(new IOException()))