From f7e22a1cf6f7664e8790cc5c76285adbcc6ab19d Mon Sep 17 00:00:00 2001 From: kanwarpreet25 <39183641+kanwarpreet25@users.noreply.github.com> Date: Sun, 28 Jul 2019 18:12:03 +0530 Subject: [PATCH] 508 : Sonar qube critical Issue Fix (#854) * 508 : Sonar qube critical Issue Fix Refactor this method to reduce its Cognitive Complexity from 30 to the 15 allowed. * 508: Sonar Qube Issue fxes Define a constant instead of duplicating this literal " does not exist." 3 times. * 508: sonar qube issue fixes Define a constant instead of duplicating this literal "Some external api for only realtime execution could be called here." 3 times. --- .../com/iluwatar/event/asynchronous/App.java | 111 ++++++++++-------- .../event/asynchronous/EventManager.java | 8 +- .../event/sourcing/domain/Account.java | 8 +- 3 files changed, 72 insertions(+), 55 deletions(-) diff --git a/event-asynchronous/src/main/java/com/iluwatar/event/asynchronous/App.java b/event-asynchronous/src/main/java/com/iluwatar/event/asynchronous/App.java index 6e9138d3c..820279bc0 100644 --- a/event-asynchronous/src/main/java/com/iluwatar/event/asynchronous/App.java +++ b/event-asynchronous/src/main/java/com/iluwatar/event/asynchronous/App.java @@ -60,7 +60,8 @@ public class App { /** * Program entry point. * - * @param args command line args + * @param args + * command line args */ public static void main(String[] args) { App app = new App(); @@ -150,56 +151,11 @@ public class App { option = s.nextInt(); if (option == 1) { - s.nextLine(); - LOGGER.info("Boil multiple eggs at once (A) or boil them one-by-one (S)?: "); - String eventType = s.nextLine(); - LOGGER.info("How long should this egg be boiled for (in seconds)?: "); - int eventTime = s.nextInt(); - if (eventType.equalsIgnoreCase("A")) { - try { - int eventId = eventManager.createAsync(eventTime); - eventManager.start(eventId); - LOGGER.info("Egg [{}] is being boiled.", eventId); - } catch (MaxNumOfEventsAllowedException | LongRunningEventException | EventDoesNotExistException e) { - LOGGER.error(e.getMessage()); - } - } else if (eventType.equalsIgnoreCase("S")) { - try { - int eventId = eventManager.create(eventTime); - eventManager.start(eventId); - LOGGER.info("Egg [{}] is being boiled.", eventId); - } catch (MaxNumOfEventsAllowedException | InvalidOperationException | LongRunningEventException - | EventDoesNotExistException e) { - LOGGER.error(e.getMessage()); - } - } else { - LOGGER.info("Unknown event type."); - } + processOption1(eventManager, s); } else if (option == 2) { - LOGGER.info("Which egg?: "); - int eventId = s.nextInt(); - try { - eventManager.cancel(eventId); - LOGGER.info("Egg [{}] is removed from boiler.", eventId); - } catch (EventDoesNotExistException e) { - LOGGER.error(e.getMessage()); - } + processOption2(eventManager, s); } else if (option == 3) { - s.nextLine(); - LOGGER.info("Just one egg (O) OR all of them (A) ?: "); - String eggChoice = s.nextLine(); - - if (eggChoice.equalsIgnoreCase("O")) { - LOGGER.info("Which egg?: "); - int eventId = s.nextInt(); - try { - eventManager.status(eventId); - } catch (EventDoesNotExistException e) { - LOGGER.error(e.getMessage()); - } - } else if (eggChoice.equalsIgnoreCase("A")) { - eventManager.statusOfAllEvents(); - } + processOption3(eventManager, s); } else if (option == 4) { eventManager.shutdown(); } @@ -208,4 +164,61 @@ public class App { s.close(); } + private void processOption3(EventManager eventManager, Scanner s) { + s.nextLine(); + LOGGER.info("Just one egg (O) OR all of them (A) ?: "); + String eggChoice = s.nextLine(); + + if (eggChoice.equalsIgnoreCase("O")) { + LOGGER.info("Which egg?: "); + int eventId = s.nextInt(); + try { + eventManager.status(eventId); + } catch (EventDoesNotExistException e) { + LOGGER.error(e.getMessage()); + } + } else if (eggChoice.equalsIgnoreCase("A")) { + eventManager.statusOfAllEvents(); + } + } + + private void processOption2(EventManager eventManager, Scanner s) { + LOGGER.info("Which egg?: "); + int eventId = s.nextInt(); + try { + eventManager.cancel(eventId); + LOGGER.info("Egg [{}] is removed from boiler.", eventId); + } catch (EventDoesNotExistException e) { + LOGGER.error(e.getMessage()); + } + } + + private void processOption1(EventManager eventManager, Scanner s) { + s.nextLine(); + LOGGER.info("Boil multiple eggs at once (A) or boil them one-by-one (S)?: "); + String eventType = s.nextLine(); + LOGGER.info("How long should this egg be boiled for (in seconds)?: "); + int eventTime = s.nextInt(); + if (eventType.equalsIgnoreCase("A")) { + try { + int eventId = eventManager.createAsync(eventTime); + eventManager.start(eventId); + LOGGER.info("Egg [{}] is being boiled.", eventId); + } catch (MaxNumOfEventsAllowedException | LongRunningEventException | EventDoesNotExistException e) { + LOGGER.error(e.getMessage()); + } + } else if (eventType.equalsIgnoreCase("S")) { + try { + int eventId = eventManager.create(eventTime); + eventManager.start(eventId); + LOGGER.info("Egg [{}] is being boiled.", eventId); + } catch (MaxNumOfEventsAllowedException | InvalidOperationException | LongRunningEventException + | EventDoesNotExistException e) { + LOGGER.error(e.getMessage()); + } + } else { + LOGGER.info("Unknown event type."); + } + } + } diff --git a/event-asynchronous/src/main/java/com/iluwatar/event/asynchronous/EventManager.java b/event-asynchronous/src/main/java/com/iluwatar/event/asynchronous/EventManager.java index 729900620..d54665bb2 100644 --- a/event-asynchronous/src/main/java/com/iluwatar/event/asynchronous/EventManager.java +++ b/event-asynchronous/src/main/java/com/iluwatar/event/asynchronous/EventManager.java @@ -39,6 +39,8 @@ public class EventManager implements ThreadCompleteListener { private int currentlyRunningSyncEvent = -1; private Random rand; private Map eventPool; + + private static final String DOES_NOT_EXIST = " does not exist."; /** * EventManager constructor. @@ -112,7 +114,7 @@ public class EventManager implements ThreadCompleteListener { */ public void start(int eventId) throws EventDoesNotExistException { if (!eventPool.containsKey(eventId)) { - throw new EventDoesNotExistException(eventId + " does not exist."); + throw new EventDoesNotExistException(eventId + DOES_NOT_EXIST); } eventPool.get(eventId).start(); @@ -126,7 +128,7 @@ public class EventManager implements ThreadCompleteListener { */ public void cancel(int eventId) throws EventDoesNotExistException { if (!eventPool.containsKey(eventId)) { - throw new EventDoesNotExistException(eventId + " does not exist."); + throw new EventDoesNotExistException(eventId + DOES_NOT_EXIST); } if (eventId == currentlyRunningSyncEvent) { @@ -145,7 +147,7 @@ public class EventManager implements ThreadCompleteListener { */ public void status(int eventId) throws EventDoesNotExistException { if (!eventPool.containsKey(eventId)) { - throw new EventDoesNotExistException(eventId + " does not exist."); + throw new EventDoesNotExistException(eventId + DOES_NOT_EXIST); } eventPool.get(eventId).status(); diff --git a/event-sourcing/src/main/java/com/iluwatar/event/sourcing/domain/Account.java b/event-sourcing/src/main/java/com/iluwatar/event/sourcing/domain/Account.java index d2e5f429b..a8ba649eb 100644 --- a/event-sourcing/src/main/java/com/iluwatar/event/sourcing/domain/Account.java +++ b/event-sourcing/src/main/java/com/iluwatar/event/sourcing/domain/Account.java @@ -44,6 +44,8 @@ public class Account { private final int accountNo; private final String owner; private BigDecimal money; + + private static final String MSG = "Some external api for only realtime execution could be called here."; /** * Instantiates a new Account. @@ -126,7 +128,7 @@ public class Account { depositMoney(money); AccountAggregate.putAccount(this); if (realTime) { - LOGGER.info("Some external api for only realtime execution could be called here."); + LOGGER.info(MSG); } } @@ -138,7 +140,7 @@ public class Account { withdrawMoney(money); AccountAggregate.putAccount(this); if (realTime) { - LOGGER.info("Some external api for only realtime execution could be called here."); + LOGGER.info(MSG); } } @@ -160,7 +162,7 @@ public class Account { public void handleEvent(AccountCreateEvent accountCreateEvent) { AccountAggregate.putAccount(this); if (accountCreateEvent.isRealTime()) { - LOGGER.info("Some external api for only realtime execution could be called here."); + LOGGER.info(MSG); } }