diff --git a/CHANGELOG b/CHANGELOG index bdec8c3..09046a8 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,12 @@ CHANGELOG ========= +0.5.0 +----- + +* Add `thenThrow(Exception)` +* Add `otherwiseThrow(Exception)` + 0.4.0 ----- diff --git a/pom.xml b/pom.xml index c5b830e..0684d89 100644 --- a/pom.xml +++ b/pom.xml @@ -59,7 +59,6 @@ net.kemitix.tiles:all:${kemitix-tiles.version} - net.kemitix.tiles:pmd-strict:${kemitix-tiles.version} net.kemitix.checkstyle:tile:${kemitix-checkstyle.version} diff --git a/src/main/java/net/kemitix/conditional/Condition.java b/src/main/java/net/kemitix/conditional/Condition.java index c87a74f..aa4669b 100644 --- a/src/main/java/net/kemitix/conditional/Condition.java +++ b/src/main/java/net/kemitix/conditional/Condition.java @@ -28,6 +28,7 @@ import java.util.function.Supplier; * * @author Paul Campbell (pcampbell@kemitix.net). */ +@SuppressWarnings({"methodcount", "PMD.TooManyMethods"}) public interface Condition { /** @@ -123,4 +124,21 @@ public interface Condition { return where(clause.get()); } + /** + * Throw then exception if the {@code Condition} is {@code true}. + * + * @param exception the Exception to throw + * @throws Exception the exception + */ + @SuppressWarnings(SuppressHelper.CS_ILLEGALTHROWS) + void thenThrow(Exception exception) throws Exception; + + /** + * Throw then exception if the {@code Condition} is {@code false}. + * + * @param exception the Exception to throw + * @throws Exception the exception + */ + @SuppressWarnings(SuppressHelper.CS_ILLEGALTHROWS) + void otherwiseThrow(Exception exception) throws Exception; } diff --git a/src/main/java/net/kemitix/conditional/FalseCondition.java b/src/main/java/net/kemitix/conditional/FalseCondition.java index 3ffdbd2..f47d8a4 100644 --- a/src/main/java/net/kemitix/conditional/FalseCondition.java +++ b/src/main/java/net/kemitix/conditional/FalseCondition.java @@ -53,4 +53,13 @@ final class FalseCondition implements Condition { response.perform(); } + @Override + public void thenThrow(final Exception exception) { + // do nothing + } + + @Override + public void otherwiseThrow(final Exception exception) throws Exception { + throw exception; + } } diff --git a/src/main/java/net/kemitix/conditional/SuppressHelper.java b/src/main/java/net/kemitix/conditional/SuppressHelper.java new file mode 100644 index 0000000..0e28ff9 --- /dev/null +++ b/src/main/java/net/kemitix/conditional/SuppressHelper.java @@ -0,0 +1,38 @@ +/** + * 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; + +/** + * Holder for strings for suppressing Checkstyle and PMD warnings. + * + * @author Paul Campbell (pcampbell@kemitix.net). + */ +final class SuppressHelper { + public static final String CS_ILLEGALTHROWS = "illegalthrows"; + + /** + * Restricted constructor. + */ + protected SuppressHelper() { + throw new UnsupportedOperationException(); + } +} diff --git a/src/main/java/net/kemitix/conditional/TrueCondition.java b/src/main/java/net/kemitix/conditional/TrueCondition.java index 0691433..3597cb3 100644 --- a/src/main/java/net/kemitix/conditional/TrueCondition.java +++ b/src/main/java/net/kemitix/conditional/TrueCondition.java @@ -54,4 +54,14 @@ final class TrueCondition implements Condition { // do nothing } + @Override + public void thenThrow(final Exception exception) throws Exception { + throw exception; + } + + @Override + public void otherwiseThrow(final Exception exception) throws Exception { + // do nothing + } + } diff --git a/src/test/java/net/kemitix/conditional/ConditionalTest.java b/src/test/java/net/kemitix/conditional/ConditionalTest.java index 5124b7f..7cc8465 100644 --- a/src/test/java/net/kemitix/conditional/ConditionalTest.java +++ b/src/test/java/net/kemitix/conditional/ConditionalTest.java @@ -1,16 +1,16 @@ package net.kemitix.conditional; +import org.assertj.core.api.WithAssertions; import org.junit.Before; import org.junit.Test; +import java.io.IOException; import java.util.concurrent.atomic.AtomicInteger; -import static org.assertj.core.api.Assertions.assertThat; - /** * @author Paul Campbell (pcampbell@kemitix.net). */ -public class ConditionalTest { +public class ConditionalTest implements WithAssertions { private Action thenResponse; @@ -311,4 +311,36 @@ public class ConditionalTest { assertThatTheThenResponseDidNotRun(); assertThat(atomicInteger).hasValue(0); } + + @Test + public void whereTrueThenThrowException() { + //given + assertThatExceptionOfType(IOException.class) + .isThrownBy(() -> Condition.where(true) + .thenThrow(new IOException())); + } + + @Test + public void whereFalseThenDoNotThrowException() throws Exception { + assertThatCode(() -> + Condition.where(false) + .thenThrow(new IOException())) + .doesNotThrowAnyException(); + } + + @Test + public void whereFalseOtherwiseThenThrowException() { + //given + assertThatExceptionOfType(IOException.class) + .isThrownBy(() -> Condition.where(false) + .otherwiseThrow(new IOException())); + } + + @Test + public void whereTrueOtherwiseThenDoNotThrowException() throws Exception { + assertThatCode(() -> + Condition.where(true) + .otherwiseThrow(new IOException())) + .doesNotThrowAnyException(); + } } diff --git a/src/test/java/net/kemitix/conditional/SuppressHelperTest.java b/src/test/java/net/kemitix/conditional/SuppressHelperTest.java new file mode 100644 index 0000000..5a4e813 --- /dev/null +++ b/src/test/java/net/kemitix/conditional/SuppressHelperTest.java @@ -0,0 +1,18 @@ +package net.kemitix.conditional; + +import org.assertj.core.api.WithAssertions; +import org.junit.Test; + +import java.lang.reflect.Constructor; + +public class SuppressHelperTest implements WithAssertions { + + @Test + public void utilityClassCannotBeInstantiate() throws NoSuchMethodException { + final Constructor constructor = SuppressHelper.class.getDeclaredConstructor(); + constructor.setAccessible(true); + assertThatExceptionOfType(UnsupportedOperationException.class) + .isThrownBy(() -> new SuppressHelper()); + } + +} \ No newline at end of file