Skip to content

Commit c78dd26

Browse files
committed
Work on iluwatar#385, added documentation and class diagram. Made refactoring changes to pass checkstyle and PMD checks
1 parent 7aff77a commit c78dd26

File tree

8 files changed

+106
-18
lines changed

8 files changed

+106
-18
lines changed

mute-idiom/etc/mute-idiom.png

13.2 KB
Loading

mute-idiom/etc/mute-idiom.ucls

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<class-diagram version="1.1.8" icons="true" automaticImage="PNG" always-add-relationships="false" generalizations="true"
3+
realizations="true" associations="true" dependencies="true" nesting-relationships="true">
4+
<class id="1" language="java" name="com.iluwatar.mute.App" project="mute-idiom"
5+
file="/mute-idiom/src/main/java/com/iluwatar/mute/App.java" binary="false" corner="BOTTOM_RIGHT">
6+
<position height="-1" width="-1" x="519" y="122"/>
7+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
8+
sort-features="false" accessors="true" visibility="true">
9+
<attributes public="true" package="true" protected="true" private="false" static="true"/>
10+
<operations public="true" package="true" protected="true" private="false" static="true"/>
11+
</display>
12+
</class>
13+
<class id="2" language="java" name="com.iluwatar.mute.Mute" project="mute-idiom"
14+
file="/mute-idiom/src/main/java/com/iluwatar/mute/Mute.java" binary="false" corner="BOTTOM_RIGHT">
15+
<position height="115" width="203" x="291" y="267"/>
16+
<display autosize="false" stereotype="true" package="true" initial-value="false" signature="true"
17+
sort-features="false" accessors="true" visibility="true">
18+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
19+
<operations public="true" package="true" protected="true" private="false" static="true"/>
20+
</display>
21+
</class>
22+
<interface id="3" language="java" name="com.iluwatar.mute.CheckedRunnable" project="mute-idiom"
23+
file="/mute-idiom/src/main/java/com/iluwatar/mute/CheckedRunnable.java" binary="false" corner="BOTTOM_RIGHT">
24+
<position height="-1" width="-1" x="723" y="322"/>
25+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
26+
sort-features="false" accessors="true" visibility="true">
27+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
28+
<operations public="true" package="true" protected="true" private="true" static="true"/>
29+
</display>
30+
</interface>
31+
<dependency id="4">
32+
<end type="SOURCE" refId="1"/>
33+
<end type="TARGET" refId="2"/>
34+
</dependency>
35+
<dependency id="5">
36+
<end type="SOURCE" refId="2"/>
37+
<end type="TARGET" refId="3"/>
38+
</dependency>
39+
<classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
40+
sort-features="false" accessors="true" visibility="true">
41+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
42+
<operations public="true" package="true" protected="true" private="true" static="true"/>
43+
</classifier-display>
44+
<association-display labels="true" multiplicity="true"/>
45+
</class-diagram>

mute-idiom/src/main/java/com/iluwatar/mute/App.java

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2121
* THE SOFTWARE.
2222
*/
23+
2324
package com.iluwatar.mute;
2425

2526
import static org.mockito.Mockito.doThrow;
@@ -28,33 +29,56 @@
2829
import java.io.ByteArrayOutputStream;
2930
import java.sql.Connection;
3031
import java.sql.SQLException;
32+
import java.sql.Statement;
3133

34+
/**
35+
* Mute pattern is utilized when we need to suppress an exception due to an API flaw or in
36+
* situation when all we can do to handle the exception is to log it.
37+
* This pattern should not be used everywhere. It is very important to logically handle the
38+
* exceptions in a system, but some situations like the ones described above require this pattern,
39+
* so that we don't need to repeat
40+
* <pre>
41+
* <code>
42+
* try {
43+
* // code that may throwing exception we need to ignore or may never be thrown
44+
* } catch (Exception ex) {
45+
* // ignore by logging or throw error if unexpected exception occurs
46+
* }
47+
* </code>
48+
* </pre> every time we need to ignore an exception.
49+
*
50+
*/
3251
public class App {
3352

34-
public static void main(String[] args) {
35-
53+
/**
54+
* Program entry point.
55+
*
56+
* @param args command line args.
57+
* @throws Exception if any exception occurs
58+
*/
59+
public static void main(String[] args) throws Exception {
60+
3661
useOfLoggedMute();
37-
62+
3863
useOfMute();
3964
}
4065

4166
/*
42-
* Typically used when the API declares some exception but cannot do so. Usually a signature mistake.
43-
* In this example out is not supposed to throw exception as it is a ByteArrayOutputStream. So we
44-
* utilize mute, which will throw AssertionError if unexpected exception occurs.
67+
* Typically used when the API declares some exception but cannot do so. Usually a
68+
* signature mistake.In this example out is not supposed to throw exception as it is a
69+
* ByteArrayOutputStream. So we utilize mute, which will throw AssertionError if unexpected
70+
* exception occurs.
4571
*/
4672
private static void useOfMute() {
4773
ByteArrayOutputStream out = new ByteArrayOutputStream();
4874
Mute.mute(() -> out.write("Hello".getBytes()));
4975
}
5076

51-
private static void useOfLoggedMute() {
77+
private static void useOfLoggedMute() throws SQLException {
5278
Connection connection = null;
5379
try {
5480
connection = openConnection();
5581
readStuff(connection);
56-
} catch (SQLException ex) {
57-
ex.printStackTrace();
5882
} finally {
5983
closeConnection(connection);
6084
}
@@ -64,14 +88,12 @@ private static void useOfLoggedMute() {
6488
* All we can do while failed close of connection is to log it.
6589
*/
6690
private static void closeConnection(Connection connection) {
67-
if (connection != null) {
68-
Mute.loggedMute(() -> connection.close());
69-
}
91+
Mute.loggedMute(() -> connection.close());
7092
}
7193

7294
private static void readStuff(Connection connection) throws SQLException {
73-
if (connection != null) {
74-
connection.createStatement();
95+
try (Statement statement = connection.createStatement()) {
96+
System.out.println("Read data from statement");
7597
}
7698
}
7799

mute-idiom/src/main/java/com/iluwatar/mute/CheckedRunnable.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2121
* THE SOFTWARE.
2222
*/
23+
2324
package com.iluwatar.mute;
2425

2526
/**
@@ -32,5 +33,5 @@ public interface CheckedRunnable {
3233
* Same as {@link Runnable#run()} with a possibility of exception in execution.
3334
* @throws Exception if any exception occurs.
3435
*/
35-
public void run() throws Exception;
36+
void run() throws Exception;
3637
}

mute-idiom/src/main/java/com/iluwatar/mute/Mute.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
* A utility class that allows you to utilize mute idiom.
3030
*/
3131
public final class Mute {
32+
33+
// The constructor is never meant to be called.
34+
private Mute() {}
3235

3336
/**
3437
* Executes the <code>runnable</code> and throws the exception occurred within a {@link AssertionError}.

mute-idiom/src/test/java/com/iluwatar/mute/AppTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
public class AppTest {
1010

1111
@Test
12-
public void test() {
12+
public void test() throws Exception {
1313
App.main(null);
1414
}
1515
}

mute-idiom/src/test/java/com/iluwatar/mute/MuteTest.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2121
* THE SOFTWARE.
2222
*/
23+
2324
package com.iluwatar.mute;
2425

2526
import static org.junit.Assert.assertTrue;
@@ -38,6 +39,11 @@ public class MuteTest {
3839

3940
@Rule public ExpectedException exception = ExpectedException.none();
4041

42+
@Test
43+
public void muteShouldRunTheCheckedRunnableAndNotThrowAnyExceptionIfCheckedRunnableDoesNotThrowAnyException() {
44+
Mute.mute(() -> methodNotThrowingAnyException());
45+
}
46+
4147
@Test
4248
public void muteShouldRethrowUnexpectedExceptionAsAssertionError() throws Exception {
4349
exception.expect(AssertionError.class);
@@ -46,8 +52,9 @@ public void muteShouldRethrowUnexpectedExceptionAsAssertionError() throws Except
4652
Mute.mute(() -> methodThrowingException());
4753
}
4854

49-
private void methodThrowingException() throws Exception {
50-
throw new Exception(MESSAGE);
55+
@Test
56+
public void loggedMuteShouldRunTheCheckedRunnableAndNotThrowAnyExceptionIfCheckedRunnableDoesNotThrowAnyException() {
57+
Mute.loggedMute(() -> methodNotThrowingAnyException());
5158
}
5259

5360
@Test
@@ -59,4 +66,13 @@ public void loggedMuteShouldLogExceptionTraceBeforeSwallowingIt() throws IOExcep
5966

6067
assertTrue(new String(stream.toByteArray()).contains(MESSAGE));
6168
}
69+
70+
71+
private void methodNotThrowingAnyException() {
72+
System.out.println("Executed successfully");
73+
}
74+
75+
private void methodThrowingException() throws Exception {
76+
throw new Exception(MESSAGE);
77+
}
6278
}

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@
123123
<module>feature-toggle</module>
124124
<module>value-object</module>
125125
<module>monad</module>
126+
<module>mute-idiom</module>
126127
</modules>
127128

128129
<dependencyManagement>

0 commit comments

Comments
 (0)