diff --git a/CHANGELOG b/CHANGELOG index 46303f5..09046a8 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -5,6 +5,7 @@ CHANGELOG ----- * 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 1f7437c..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 { /** @@ -129,6 +130,15 @@ public interface Condition { * @param exception the Exception to throw * @throws Exception the exception */ - @SuppressWarnings({"illegalthrows", "PMD.SignatureDeclareThrowsException"}) + @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 5ec9f5e..f47d8a4 100644 --- a/src/main/java/net/kemitix/conditional/FalseCondition.java +++ b/src/main/java/net/kemitix/conditional/FalseCondition.java @@ -58,4 +58,8 @@ final class FalseCondition implements Condition { // 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 5001c5b..3597cb3 100644 --- a/src/main/java/net/kemitix/conditional/TrueCondition.java +++ b/src/main/java/net/kemitix/conditional/TrueCondition.java @@ -59,4 +59,9 @@ final class TrueCondition implements Condition { 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 9590438..6c07660 100644 --- a/src/test/java/net/kemitix/conditional/ConditionalTest.java +++ b/src/test/java/net/kemitix/conditional/ConditionalTest.java @@ -325,4 +325,18 @@ public class ConditionalTest implements WithAssertions { Condition.where(false) .thenThrow(new IOException()); } + + @Test + public void whereFalse_otherwiseThenThrowException() { + //given + assertThatExceptionOfType(IOException.class) + .isThrownBy(() -> Condition.where(false) + .otherwiseThrow(new IOException())); + } + + @Test + public void whereTrue_otherwiseThenDoNotThrowException() throws Exception { + Condition.where(true) + .otherwiseThrow(new IOException()); + } } 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..91ae946 --- /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 utilityClass_canNotInstantiate() throws NoSuchMethodException { + final Constructor constructor = SuppressHelper.class.getDeclaredConstructor(); + constructor.setAccessible(true); + assertThatExceptionOfType(UnsupportedOperationException.class) + .isThrownBy(() -> new SuppressHelper()); + } + +} \ No newline at end of file