Skip to content

Commit 4d820b1

Browse files
committed
Changes after review. Added README
1 parent e543ee0 commit 4d820b1

File tree

4 files changed

+84
-25
lines changed

4 files changed

+84
-25
lines changed

mute-idiom/etc/mute-idiom.png

4.89 KB
Loading

mute-idiom/etc/mute-idiom.ucls

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
realizations="true" associations="true" dependencies="true" nesting-relationships="true">
44
<class id="1" language="java" name="com.iluwatar.mute.App" project="mute-idiom"
55
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"/>
6+
<position height="-1" width="-1" x="168" y="234"/>
77
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
88
sort-features="false" accessors="true" visibility="true">
99
<attributes public="true" package="true" protected="true" private="false" static="true"/>
@@ -12,7 +12,7 @@
1212
</class>
1313
<class id="2" language="java" name="com.iluwatar.mute.Mute" project="mute-idiom"
1414
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"/>
15+
<position height="115" width="203" x="70" y="375"/>
1616
<display autosize="false" stereotype="true" package="true" initial-value="false" signature="true"
1717
sort-features="false" accessors="true" visibility="true">
1818
<attributes public="true" package="true" protected="true" private="true" static="true"/>
@@ -21,21 +21,47 @@
2121
</class>
2222
<interface id="3" language="java" name="com.iluwatar.mute.CheckedRunnable" project="mute-idiom"
2323
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"/>
24+
<position height="-1" width="-1" x="407" y="432"/>
2525
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
2626
sort-features="false" accessors="true" visibility="true">
2727
<attributes public="true" package="true" protected="true" private="true" static="true"/>
2828
<operations public="true" package="true" protected="true" private="true" static="true"/>
2929
</display>
3030
</interface>
31-
<dependency id="4">
31+
<interface id="4" language="java" name="com.iluwatar.mute.Resource" project="mute-idiom"
32+
file="/mute-idiom/src/main/java/com/iluwatar/mute/Resource.java" binary="false" corner="BOTTOM_RIGHT">
33+
<position height="-1" width="-1" x="383" y="235"/>
34+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
35+
sort-features="false" accessors="true" visibility="true">
36+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
37+
<operations public="true" package="true" protected="true" private="true" static="true"/>
38+
</display>
39+
</interface>
40+
<interface id="5" language="java" name="java.io.Closeable" project="dao"
41+
file="/opt/Softwares/Eclipses/MARS/eclipse/jre/lib/rt.jar" binary="true" corner="BOTTOM_RIGHT">
42+
<position height="-1" width="-1" x="384" y="109"/>
43+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
44+
sort-features="false" accessors="true" visibility="true">
45+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
46+
<operations public="true" package="true" protected="true" private="true" static="true"/>
47+
</display>
48+
</interface>
49+
<dependency id="6">
50+
<end type="SOURCE" refId="1"/>
51+
<end type="TARGET" refId="4"/>
52+
</dependency>
53+
<dependency id="7">
3254
<end type="SOURCE" refId="1"/>
3355
<end type="TARGET" refId="2"/>
3456
</dependency>
35-
<dependency id="5">
57+
<dependency id="8">
3658
<end type="SOURCE" refId="2"/>
3759
<end type="TARGET" refId="3"/>
3860
</dependency>
61+
<generalization id="9">
62+
<end type="SOURCE" refId="4"/>
63+
<end type="TARGET" refId="5"/>
64+
</generalization>
3965
<classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
4066
sort-features="false" accessors="true" visibility="true">
4167
<attributes public="true" package="true" protected="true" private="true" static="true"/>

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

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,9 @@
2323

2424
package com.iluwatar.mute;
2525

26-
import static org.mockito.Mockito.doThrow;
27-
import static org.mockito.Mockito.mock;
28-
2926
import java.io.ByteArrayOutputStream;
30-
import java.sql.Connection;
27+
import java.io.IOException;
3128
import java.sql.SQLException;
32-
import java.sql.Statement;
3329

3430
/**
3531
* Mute pattern is utilized when we need to suppress an exception due to an API flaw or in
@@ -75,31 +71,33 @@ private static void useOfMute() {
7571
}
7672

7773
private static void useOfLoggedMute() throws SQLException {
78-
Connection connection = null;
74+
Resource resource = null;
7975
try {
80-
connection = openConnection();
81-
readStuff(connection);
76+
resource = acquireResource();
77+
utilizeResource(resource);
8278
} finally {
83-
closeConnection(connection);
79+
closeResource(resource);
8480
}
8581
}
8682

8783
/*
88-
* All we can do while failed close of connection is to log it.
84+
* All we can do while failed close of a resource is to log it.
8985
*/
90-
private static void closeConnection(Connection connection) {
91-
Mute.loggedMute(() -> connection.close());
86+
private static void closeResource(Resource resource) {
87+
Mute.loggedMute(() -> resource.close());
9288
}
9389

94-
private static void readStuff(Connection connection) throws SQLException {
95-
try (Statement statement = connection.createStatement()) {
96-
System.out.println("Read data from statement");
97-
}
90+
private static void utilizeResource(Resource resource) throws SQLException {
91+
System.out.println("Utilizing acquired resource: " + resource);
9892
}
9993

100-
private static Connection openConnection() throws SQLException {
101-
Connection mockedConnection = mock(Connection.class);
102-
doThrow(SQLException.class).when(mockedConnection).close();
103-
return mockedConnection;
94+
private static Resource acquireResource() throws SQLException {
95+
return new Resource() {
96+
97+
@Override
98+
public void close() throws IOException {
99+
throw new IOException("Error in closing resource: " + this);
100+
}
101+
};
104102
}
105103
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
24+
package com.iluwatar.mute;
25+
26+
import java.io.Closeable;
27+
28+
/**
29+
* Represents any resource that the application might acquire and that must be closed
30+
* after it is utilized. Example of such resources can be a database connection, open
31+
* files, sockets.
32+
*/
33+
public interface Resource extends Closeable {
34+
35+
}

0 commit comments

Comments
 (0)