|
| 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.ByteArrayOutputStream; |
| 27 | +import java.io.IOException; |
| 28 | +import java.sql.SQLException; |
| 29 | + |
| 30 | +/** |
| 31 | + * Mute pattern is utilized when we need to suppress an exception due to an API flaw or in |
| 32 | + * situation when all we can do to handle the exception is to log it. |
| 33 | + * This pattern should not be used everywhere. It is very important to logically handle the |
| 34 | + * exceptions in a system, but some situations like the ones described above require this pattern, |
| 35 | + * so that we don't need to repeat |
| 36 | + * <pre> |
| 37 | + * <code> |
| 38 | + * try { |
| 39 | + * // code that may throwing exception we need to ignore or may never be thrown |
| 40 | + * } catch (Exception ex) { |
| 41 | + * // ignore by logging or throw error if unexpected exception occurs |
| 42 | + * } |
| 43 | + * </code> |
| 44 | + * </pre> every time we need to ignore an exception. |
| 45 | + * |
| 46 | + */ |
| 47 | +public class App { |
| 48 | + |
| 49 | + /** |
| 50 | + * Program entry point. |
| 51 | + * |
| 52 | + * @param args command line args. |
| 53 | + * @throws Exception if any exception occurs |
| 54 | + */ |
| 55 | + public static void main(String[] args) throws Exception { |
| 56 | + |
| 57 | + useOfLoggedMute(); |
| 58 | + |
| 59 | + useOfMute(); |
| 60 | + } |
| 61 | + |
| 62 | + /* |
| 63 | + * Typically used when the API declares some exception but cannot do so. Usually a |
| 64 | + * signature mistake.In this example out is not supposed to throw exception as it is a |
| 65 | + * ByteArrayOutputStream. So we utilize mute, which will throw AssertionError if unexpected |
| 66 | + * exception occurs. |
| 67 | + */ |
| 68 | + private static void useOfMute() { |
| 69 | + ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 70 | + Mute.mute(() -> out.write("Hello".getBytes())); |
| 71 | + } |
| 72 | + |
| 73 | + private static void useOfLoggedMute() throws SQLException { |
| 74 | + Resource resource = null; |
| 75 | + try { |
| 76 | + resource = acquireResource(); |
| 77 | + utilizeResource(resource); |
| 78 | + } finally { |
| 79 | + closeResource(resource); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + /* |
| 84 | + * All we can do while failed close of a resource is to log it. |
| 85 | + */ |
| 86 | + private static void closeResource(Resource resource) { |
| 87 | + Mute.loggedMute(() -> resource.close()); |
| 88 | + } |
| 89 | + |
| 90 | + private static void utilizeResource(Resource resource) throws SQLException { |
| 91 | + System.out.println("Utilizing acquired resource: " + resource); |
| 92 | + } |
| 93 | + |
| 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 | + }; |
| 102 | + } |
| 103 | +} |
0 commit comments