diff --git a/mute-idiom/etc/mute-idiom.png b/mute-idiom/etc/mute-idiom.png index 626cec827..203bdafc4 100644 Binary files a/mute-idiom/etc/mute-idiom.png and b/mute-idiom/etc/mute-idiom.png differ diff --git a/mute-idiom/etc/mute-idiom.ucls b/mute-idiom/etc/mute-idiom.ucls index 25a7bfefb..7b4d2f916 100644 --- a/mute-idiom/etc/mute-idiom.ucls +++ b/mute-idiom/etc/mute-idiom.ucls @@ -3,7 +3,7 @@ realizations="true" associations="true" dependencies="true" nesting-relationships="true"> - + @@ -12,7 +12,7 @@ - + @@ -21,21 +21,47 @@ - + - + + + + + + + + + + + + + + + + + + + - + + + + + diff --git a/mute-idiom/src/main/java/com/iluwatar/mute/App.java b/mute-idiom/src/main/java/com/iluwatar/mute/App.java index a0eb815fd..8a2aca41e 100644 --- a/mute-idiom/src/main/java/com/iluwatar/mute/App.java +++ b/mute-idiom/src/main/java/com/iluwatar/mute/App.java @@ -23,13 +23,9 @@ package com.iluwatar.mute; -import static org.mockito.Mockito.doThrow; -import static org.mockito.Mockito.mock; - import java.io.ByteArrayOutputStream; -import java.sql.Connection; +import java.io.IOException; import java.sql.SQLException; -import java.sql.Statement; /** * Mute pattern is utilized when we need to suppress an exception due to an API flaw or in @@ -75,31 +71,33 @@ public class App { } private static void useOfLoggedMute() throws SQLException { - Connection connection = null; + Resource resource = null; try { - connection = openConnection(); - readStuff(connection); + resource = acquireResource(); + utilizeResource(resource); } finally { - closeConnection(connection); + closeResource(resource); } } /* - * All we can do while failed close of connection is to log it. + * All we can do while failed close of a resource is to log it. */ - private static void closeConnection(Connection connection) { - Mute.loggedMute(() -> connection.close()); + private static void closeResource(Resource resource) { + Mute.loggedMute(() -> resource.close()); } - private static void readStuff(Connection connection) throws SQLException { - try (Statement statement = connection.createStatement()) { - System.out.println("Read data from statement"); - } + private static void utilizeResource(Resource resource) throws SQLException { + System.out.println("Utilizing acquired resource: " + resource); } - private static Connection openConnection() throws SQLException { - Connection mockedConnection = mock(Connection.class); - doThrow(SQLException.class).when(mockedConnection).close(); - return mockedConnection; + private static Resource acquireResource() throws SQLException { + return new Resource() { + + @Override + public void close() throws IOException { + throw new IOException("Error in closing resource: " + this); + } + }; } } diff --git a/mute-idiom/src/main/java/com/iluwatar/mute/Resource.java b/mute-idiom/src/main/java/com/iluwatar/mute/Resource.java new file mode 100644 index 000000000..6970d06bc --- /dev/null +++ b/mute-idiom/src/main/java/com/iluwatar/mute/Resource.java @@ -0,0 +1,35 @@ +/** + * The MIT License + * Copyright (c) 2014 Ilkka Seppälä + * + * 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 com.iluwatar.mute; + +import java.io.Closeable; + +/** + * Represents any resource that the application might acquire and that must be closed + * after it is utilized. Example of such resources can be a database connection, open + * files, sockets. + */ +public interface Resource extends Closeable { + +}