Skip to content
This repository has been archived by the owner on Jan 18, 2025. It is now read-only.

Commit

Permalink
comment utils folder
Browse files Browse the repository at this point in the history
  • Loading branch information
kennethchen416 committed Jan 15, 2025
1 parent 5f13b13 commit dc56ba5
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,6 @@ public enum ANSIColor {
* @return The colorized string.
*/
public static String colorize(final ANSIColor color, final String msg) {
return colorMap.get(color) + msg + colorMap.get(RESET);
return colorMap.get(color) + msg + colorMap.get(RESET); // Reset color after coloring the string
}
}
30 changes: 27 additions & 3 deletions culminating-mastermind/app/src/main/java/mastermind/utils/Log.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,34 @@ public class Log {
* addition to stdout, which is configured by {@link #logToStdout}.
*/
private static final Set<OutputStream> sinks = new HashSet<>();

/**
* The severity level of the logger.
* <p>
* Only messages with a severity level greater than or equal to this level
* will be logged.
*/
private static Severity level = Severity.INFO;

/**
* A flag indicating whether log messages should be written to stdout.
*/
private static boolean logToStdout = true;

/**
* Disable the constructor as {@Code Log} being a singleton class.
* Disable the constructor as {{@code @Code} Log} being a singleton class.
*/
private Log() {
throw new IllegalStateException("This singleton class should not be instantiated");
};
}

/**
* Adds sink to which log messages will be written.
*
* @param sink The output stream to add.
*/
public static void addSink(final OutputStream sink) {
// Add the provided output stream to the set of sinks
sinks.add(sink);
}

Expand All @@ -54,6 +57,7 @@ public static void addSink(final OutputStream sink) {
* @param level The severity level to set.
*/
public static void setLevel(final Severity level) {
// Set the logging level to the provided severity level
Log.level = level;
}

Expand All @@ -63,6 +67,7 @@ public static void setLevel(final Severity level) {
* @param logToStdout The flag to set.
*/
public static void setLogToStdout(final boolean logToStdout) {
// Set the flag indicating whether to log to stdout
Log.logToStdout = logToStdout;
}

Expand All @@ -72,6 +77,7 @@ public static void setLogToStdout(final boolean logToStdout) {
* @param msg The message to log.
*/
public static void trace(final String msg) {
// Log the message with TRACE severity
log(Severity.TRACE, msg);
}

Expand All @@ -81,6 +87,7 @@ public static void trace(final String msg) {
* @param msg The message to log.
*/
public static void debug(final String msg) {
// Log the message with DEBUG severity
log(Severity.DEBUG, msg);
}

Expand All @@ -90,6 +97,7 @@ public static void debug(final String msg) {
* @param msg The message to log.
*/
public static void info(final String msg) {
// Log the message with INFO severity
log(Severity.INFO, msg);
}

Expand All @@ -99,6 +107,7 @@ public static void info(final String msg) {
* @param msg The message to log.
*/
public static void warning(final String msg) {
// Log the message with WARNING severity
log(Severity.WARNING, msg);
}

Expand All @@ -108,6 +117,7 @@ public static void warning(final String msg) {
* @param msg The message to log.
*/
public static void error(final String msg) {
// Log the message with ERROR severity
log(Severity.ERROR, msg);
}

Expand All @@ -117,6 +127,7 @@ public static void error(final String msg) {
* @param msg The message to log.
*/
public static void fatal(final String msg) {
// Log the message with FATAL severity and exit the program
log(Severity.FATAL, msg);
System.exit(1);
}
Expand All @@ -131,28 +142,41 @@ public static void fatal(final String msg) {
* @param msg The message to log.
*/
private static void log(final Severity severity, final String msg) {
// Check if the severity level is less than the current logging level
if (severity.ordinal() < level.ordinal()) {
return;
}

// Get the current timestamp
final Instant now = Instant.now();

// Convert the severity level to a string
final String severityStr = severity.toString();

// Colorize the severity string using ANSI color codes
final String coloredSeverityStr = ANSIColor.colorize(Severity.COLOR_MAP.get(severity), severityStr);

// Define the format string for log messages
final String msgFormatString = "[%s][%s] %s%n";

// Format the full log message with the timestamp, severity, and message
final String fullMsg = String.format(msgFormatString, now.toString(), severityStr, msg);

// Format the colored log message with the timestamp, colored severity, and message
final String coloredFullMsg = String.format(msgFormatString, now, coloredSeverityStr, msg);

// If logging to stdout is enabled, print the colored log message to stdout
if (logToStdout) {
System.out.print(coloredFullMsg);
}

// Iterate over each output stream in the set of sinks
for (OutputStream sink : sinks) {
try {
// Write the log message to the output stream
sink.write(fullMsg.getBytes());
} catch (final IOException e) {
// Print an error message to stderr if writing to the sink fails
System.err.println("Failed to log to sink: " + e.getMessage());
}
}
Expand Down Expand Up @@ -203,4 +227,4 @@ public enum Severity {
ERROR, ANSIColor.RED,
FATAL, ANSIColor.RED_BACKGROUND);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class MathUtil {
* This class should not be instantiated.
*/
private MathUtil() {
// Throw an exception if this class is instantiated, as it is meant to be a utility class
throw new IllegalStateException("This class should not be instantiated");
}

Expand All @@ -26,20 +27,29 @@ private MathUtil() {
* ordered from the most significant digit to the least significant digit.
*/
public static ArrayList<Integer> digitsFromBase(int number, final int base, final int arrLength) {
// Initialize an ArrayList to store the digits, with an initial capacity of arrLength
ArrayList<Integer> digits = new ArrayList<>(arrLength);

// Extract digits from the number in the specified base
while (number > 0) {
// Get the least significant digit by taking the remainder of the number divided by the base
final int leastSignificantDigit = number % base;
// Add the least significant digit to the list
digits.add(leastSignificantDigit);
// Divide the number by the base to remove the least significant digit
number /= base;
}

// Pad the list with leading zeros if the number of extracted digits is less than arrLength
while (digits.size() < arrLength) {
// Add a zero to the list
digits.add(0);
}

// Reverse the list to have the most significant digit at the beginning
Collections.reverse(digits);

// Return the list of digits
return digits;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,20 @@ public record Tuple2<T, U>(T first, U second) {
*/
@Override
public boolean equals(Object obj) {
// Check if the current instance is the same as the object being compared
if (this == obj) {
return true;
}

// Check if the object being compared is null or not of the same class
if (obj == null || obj.getClass() != this.getClass()) {
return false;
}

// Cast the object to Tuple2 to compare the elements
@SuppressWarnings("unchecked") final Tuple2<T, U> otherTuple = (Tuple2<T, U>) obj;

// Compare the first and second elements of both tuples for equality
return this.first.equals(otherTuple.first) && this.second.equals(otherTuple.second);

}
}

0 comments on commit dc56ba5

Please sign in to comment.