From fceb63fbd8494b934268764bca36dc61419f54f0 Mon Sep 17 00:00:00 2001 From: elblasco <64553614+elblasco@users.noreply.github.com> Date: Wed, 7 Aug 2024 11:03:00 +0200 Subject: [PATCH] fix: formatting --- .../java/it/unitn/disi/ds1/qtop/Logger.java | 298 +++++++++--------- .../it/unitn/disi/ds1/qtop/PairsHistory.java | 39 ++- .../disi/ds1/qtop/SimulationCallback.java | 16 +- .../it/unitn/disi/ds1/qtop/VotersMap.java | 3 +- 4 files changed, 190 insertions(+), 166 deletions(-) diff --git a/src/main/java/it/unitn/disi/ds1/qtop/Logger.java b/src/main/java/it/unitn/disi/ds1/qtop/Logger.java index 4044aff..8ec3dfd 100644 --- a/src/main/java/it/unitn/disi/ds1/qtop/Logger.java +++ b/src/main/java/it/unitn/disi/ds1/qtop/Logger.java @@ -18,148 +18,158 @@ */ public class Logger { - private static Logger instance = null; - // Map to store PrintWriters for each entity - private final Map entityLogs = new HashMap<>(); - private LogLevel logLevel = LogLevel.INFO; - private PrintWriter info; - private PrintWriter debug; - - /** - * Private constructor to prevent instantiation from outside - */ - private Logger() { - ensureDirectoryExists("logs"); // Ensure logs directory exists - try { - info = new PrintWriter( - "logs" + File.separator + "simulation.log", - StandardCharsets.UTF_8 - ); - debug = new PrintWriter( - "logs" + File.separator + "debug.log", - StandardCharsets.UTF_8 - ); - } catch (IOException e) - { - e.printStackTrace(); - } - } - - /** - * Singleton pattern to get the single instance of Logger. - * - * @return the Logger instance - */ - public static Logger getInstance() { - if (instance == null) { - synchronized (Logger.class) { - if (instance == null) { - instance = new Logger(); - } - } - } - return instance; - } - - /** - * Set the log level. - * - * @param level level to set - */ - public void setLogLevel(LogLevel level) { - logLevel = level; - } - - /** - * Ensure the logs directory exists, create if it does not. - * - * @param path directory path - */ - private void ensureDirectoryExists(String path) { - File directory = new File(path); - if (!directory.exists()) { - directory.mkdirs(); - } - } - - /** - * Get the PrintWriter for the given entity, create if it does not exist. - * - * @param entity entity to get the log for - * - * @return PrintWriter for the entity - */ - private @Nullable PrintWriter getEntityLog(String entity) { - if (!entityLogs.containsKey(entity)) { - ensureDirectoryExists("logs"); - try { - PrintWriter writer = new PrintWriter( - "logs" + File.separator + entity + ".log", - StandardCharsets.UTF_8 - ); - entityLogs.put(entity, writer); - return writer; - } catch (IOException e) - { - e.printStackTrace(); - return null; - } - } - return entityLogs.get(entity); - } - - // Parse the entity (NODE or CLIENT) from the log message - - /** - * Parse the entity (NODE or CLIENT) from the log message. - * - * @param message log message - * - * @return the entity "NODE", "CLIENT" or "general" - */ - private String parseEntity(String message) { - // Regular expression to match NODE- or CLIENT- - Pattern pattern = Pattern.compile("(NODE-\\d+)|(CLIENT-\\d+)"); - Matcher matcher = pattern.matcher(message); - - if (matcher.find()) { - // Return the matched group (NODE or CLIENT) - return matcher.group(1) != null ? matcher.group(1) : matcher.group(2); - } else { - return "general"; // Default entity if none is found - } - } - - /** - * Log the message at the specified log level. - * - * @param level log level - * @param message log message - */ - public void log(LogLevel level, String message) { - String log = String.format( - "[%s] [%s] %s", - LocalTime.now(), - level, - message - ); - - // Log to global info logs if the level is appropriate - if (level.ordinal() >= logLevel.ordinal()) { - info.println(log); - info.flush(); - } - - // Log to debug logs - debug.println(log); - debug.flush(); - - // Log to entity-specific logs - String entity = parseEntity(message); - PrintWriter entityLog = getEntityLog(entity); - if (entityLog != null) { - entityLog.println(log); - entityLog.flush(); - } - } + private static Logger instance = null; + // Map to store PrintWriters for each entity + private final Map entityLogs = new HashMap<>(); + private LogLevel logLevel = LogLevel.INFO; + private PrintWriter info; + private PrintWriter debug; + + /** + * Private constructor to prevent instantiation from outside + */ + private Logger() { + ensureDirectoryExists("logs"); // Ensure logs directory exists + try + { + info = new PrintWriter( + "logs" + File.separator + "simulation.log", + StandardCharsets.UTF_8 + ); + debug = new PrintWriter( + "logs" + File.separator + "debug.log", + StandardCharsets.UTF_8 + ); + } catch (IOException e) + { + e.printStackTrace(); + } + } + + /** + * Singleton pattern to get the single instance of Logger. + * + * @return the Logger instance + */ + public static Logger getInstance() { + if (instance == null) + { + synchronized (Logger.class) + { + if (instance == null) + { + instance = new Logger(); + } + } + } + return instance; + } + + /** + * Set the log level. + * + * @param level level to set + */ + public void setLogLevel(LogLevel level) { + logLevel = level; + } + + /** + * Ensure the logs directory exists, create if it does not. + * + * @param path directory path + */ + private void ensureDirectoryExists(String path) { + File directory = new File(path); + if (! directory.exists()) + { + directory.mkdirs(); + } + } + + /** + * Get the PrintWriter for the given entity, create if it does not exist. + * + * @param entity entity to get the log for + * + * @return PrintWriter for the entity + */ + private @Nullable PrintWriter getEntityLog(String entity) { + if (! entityLogs.containsKey(entity)) + { + ensureDirectoryExists("logs"); + try + { + PrintWriter writer = new PrintWriter( + "logs" + File.separator + entity + ".log", + StandardCharsets.UTF_8 + ); + entityLogs.put( + entity, + writer + ); + return writer; + } catch (IOException e) + { + e.printStackTrace(); + return null; + } + } + return entityLogs.get(entity); + } + // Parse the entity (NODE or CLIENT) from the log message + + /** + * Parse the entity (NODE or CLIENT) from the log message. + * + * @param message log message + * + * @return the entity "NODE", "CLIENT" or "general" + */ + private String parseEntity(String message) { + // Regular expression to match NODE- or CLIENT- + Pattern pattern = Pattern.compile("(NODE-\\d+)|(CLIENT-\\d+)"); + Matcher matcher = pattern.matcher(message); + if (matcher.find()) + { + // Return the matched group (NODE or CLIENT) + return matcher.group(1) != null ? matcher.group(1) : matcher.group(2); + } + else + { + return "general"; // Default entity if none is found + } + } + + /** + * Log the message at the specified log level. + * + * @param level log level + * @param message log message + */ + public void log(LogLevel level, String message) { + String log = String.format( + "[%s] [%s] %s", + LocalTime.now(), + level, + message + ); + // Log to global info logs if the level is appropriate + if (level.ordinal() >= logLevel.ordinal()) + { + info.println(log); + info.flush(); + } + // Log to debug logs + debug.println(log); + debug.flush(); + // Log to entity-specific logs + String entity = parseEntity(message); + PrintWriter entityLog = getEntityLog(entity); + if (entityLog != null) + { + entityLog.println(log); + entityLog.flush(); + } + } } diff --git a/src/main/java/it/unitn/disi/ds1/qtop/PairsHistory.java b/src/main/java/it/unitn/disi/ds1/qtop/PairsHistory.java index 23dce86..f900c9b 100644 --- a/src/main/java/it/unitn/disi/ds1/qtop/PairsHistory.java +++ b/src/main/java/it/unitn/disi/ds1/qtop/PairsHistory.java @@ -28,12 +28,10 @@ public PairsHistory(@NotNull PairsHistory sourceObject) { ArrayList> newEpoch = new ArrayList<>(); for (Pair iteration : epoch) { - newEpoch.add( - new Pair<>( - iteration.first(), - iteration.second() - ) - ); + newEpoch.add(new Pair<>( + iteration.first(), + iteration.second() + )); } this.add(newEpoch); } @@ -47,7 +45,8 @@ public PairsHistory(@NotNull PairsHistory sourceObject) { * @param finalState final state to set */ public void setState(int e, int i, Utils.Decision finalState) { - this.get(e).set(i, + this.get(e).set( + i, new Pair<>( this.get(e).get(i).first(), finalState @@ -127,7 +126,10 @@ public Utils.EpochPair getLatest() { latestIteration ); } - return new Utils.EpochPair(-1, -1); + return new Utils.EpochPair( + - 1, + - 1 + ); } /** @@ -136,7 +138,7 @@ public Utils.EpochPair getLatest() { * * @return the latest epoch and iteration committed */ - public Utils.EpochPair getLatestCommitted(){ + public Utils.EpochPair getLatestCommitted() { if (! this.isEmpty()) { for (int i = this.size() - 1; i >= 0; i--) @@ -157,9 +159,12 @@ public Utils.EpochPair getLatestCommitted(){ } } } - return new Utils.EpochPair(-1, -1); + return new Utils.EpochPair( + - 1, + - 1 + ); } - + @Override public String toString() { StringBuilder sb = new StringBuilder(); @@ -170,10 +175,18 @@ public String toString() { { sb.append(iteration.first()).append(" ").append(iteration.second()).append(", "); } - sb.replace(sb.length() - 2, sb.length(), ""); + sb.replace( + sb.length() - 2, + sb.length(), + "" + ); sb.append(" ]\n"); } - sb.replace(sb.length() - 1, sb.length(), ""); + sb.replace( + sb.length() - 1, + sb.length(), + "" + ); return sb.toString(); } } \ No newline at end of file diff --git a/src/main/java/it/unitn/disi/ds1/qtop/SimulationCallback.java b/src/main/java/it/unitn/disi/ds1/qtop/SimulationCallback.java index 32a4ffc..aef1643 100644 --- a/src/main/java/it/unitn/disi/ds1/qtop/SimulationCallback.java +++ b/src/main/java/it/unitn/disi/ds1/qtop/SimulationCallback.java @@ -5,13 +5,13 @@ */ public interface SimulationCallback { - /** - * Start the simulation. - */ - void start(); + /** + * Start the simulation. + */ + void start(); - /** - * Display to the user the textual menu. - */ - void clientMenu(); + /** + * Display to the user the textual menu. + */ + void clientMenu(); } diff --git a/src/main/java/it/unitn/disi/ds1/qtop/VotersMap.java b/src/main/java/it/unitn/disi/ds1/qtop/VotersMap.java index e56fa7f..f6ffc36 100644 --- a/src/main/java/it/unitn/disi/ds1/qtop/VotersMap.java +++ b/src/main/java/it/unitn/disi/ds1/qtop/VotersMap.java @@ -61,7 +61,8 @@ public void insert(int e, int i, ActorRef actorRef, Utils.Vote vote) { * @param i the index */ public void setDecision(Utils.Decision d, int e, int i) { - this.get(e).set(i, + this.get(e).set( + i, new Utils.VotePair( this.get(e).get(i).votes(), d