From c0da4b3165e62f88b3385cf3e0a32578af4fbccc Mon Sep 17 00:00:00 2001 From: Narendra Pathai Date: Tue, 16 Oct 2018 12:55:33 +0530 Subject: [PATCH] Removed sonar issue and converted Audio to eager singleton for simplicity --- .../java/com/iluwatar/event/queue/Audio.java | 23 ++++++++----------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/event-queue/src/main/java/com/iluwatar/event/queue/Audio.java b/event-queue/src/main/java/com/iluwatar/event/queue/Audio.java index d059ccac0..2379bd31e 100644 --- a/event-queue/src/main/java/com/iluwatar/event/queue/Audio.java +++ b/event-queue/src/main/java/com/iluwatar/event/queue/Audio.java @@ -23,6 +23,9 @@ package com.iluwatar.event.queue; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import java.io.File; import java.io.IOException; @@ -38,6 +41,8 @@ import javax.sound.sampled.UnsupportedAudioFileException; * */ public class Audio { + private static final Logger LOGGER = LoggerFactory.getLogger(Audio.class); + private static final Audio INSTANCE = new Audio(); private static final int MAX_PENDING = 16; @@ -55,7 +60,7 @@ public class Audio { } public static Audio getInstance() { - return SingletonHolder.getAudioInstance(); + return INSTANCE; } /** @@ -141,14 +146,11 @@ public class Audio { clip.open(audioStream); clip.start(); } catch (LineUnavailableException e) { - System.err.println("Error occoured while loading the audio: The line is unavailable"); - e.printStackTrace(); + LOGGER.trace("Error occoured while loading the audio: The line is unavailable", e); } catch (IOException e) { - System.err.println("Input/Output error while loading the audio"); - e.printStackTrace(); + LOGGER.trace("Input/Output error while loading the audio", e); } catch (IllegalArgumentException e) { - System.err.println("The system doesn't support the sound: " + e.getMessage()); - e.printStackTrace(); + LOGGER.trace("The system doesn't support the sound: " + e.getMessage(), e); } } @@ -172,11 +174,4 @@ public class Audio { return pendingAudio; } - private static class SingletonHolder { - private static final Audio INSTANCE = new Audio(); - - static Audio getAudioInstance() { - return INSTANCE; - } - } }