Added javadoc comments

This commit is contained in:
2024-08-11 21:31:00 -04:00
parent ae1346dbcd
commit 3feefdb7dd
12 changed files with 825 additions and 60 deletions

View File

@@ -1,10 +1,10 @@
//JavaClasses/src/main/java/mattrixwv/Stopwatch.java
//Matthew Ellison (Mattrixwv)
// Created: 03-01-19
//Modified: 04-13-23
//Modified: 08-11-24
//This file contains a class that is used to time the execution time of other programs
/*
Copyright (C) 2023 Matthew Ellison
Copyright (C) 2024 Matthew Ellison
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
@@ -25,16 +25,49 @@ package com.mattrixwv;
import com.mattrixwv.exceptions.InvalidResult;
/**
* A simple stopwatch class to measure elapsed time in various units of resolution.
*
* <p>
* This class provides methods to start, stop, and reset a stopwatch, as well as retrieve the
* elapsed time in nanoseconds, microseconds, milliseconds, seconds, minutes, and hours. It
* also includes functionality to get the elapsed time as a formatted string in the most
* appropriate time unit.
* </p>
*/
public class Stopwatch{
/**
* The start time of the stopwatch in nanoseconds.
*/
private Long startTime;
/**
* The stop time of the stopwatch in nanoseconds.
*/
private Long stopTime;
//Constructor makes sure all values are set to defaults
//?Constructor makes sure all values are set to defaults
/**
* Constructs a new Stopwatch and initializes the start and stop times to {@code null}.
* This ensures that incorrect function calling order can be detected easily.
*/
public Stopwatch(){
//Make sure both values are null so it is easier to detect incorrect function calling order
startTime = null;
stopTime = null;
}
//Returns a long with the elapsed time in nanoseconds. Used by other functions to get the time before converting it to the correct resolution
//?Returns a long with the elapsed time in nanoseconds. Used by other functions to get the time before converting it to the correct resolution
/**
* Returns the elapsed time in nanoseconds.
* <p>
* If the stopwatch has not been started, it returns {@code 0L}. If the stopwatch has
* been started but not stopped, it returns the time elapsed since it was started.
* </p>
*
* @return the elapsed time in nanoseconds
*/
private Long getTime(){
if(startTime == null){
return 0L;
@@ -46,16 +79,37 @@ public class Stopwatch{
return stopTime - startTime;
}
}
//An enum that helps keep track of how many times the time has been reduced in the getStr function
//?An enum that helps keep track of how many times the time has been reduced in the getStr function
/**
* Enum to represent the time resolution for formatting the time string.
*/
private enum TIME_RESOLUTION{ NANOSECOND, MICROSECOND, MILLISECOND, SECOND, MINUTE, HOUR, ERROR }
//Simulates starting a stopwatch by saving the time
//?Simulates starting a stopwatch by saving the time
/**
* Starts the stopwatch by recording the current time in nanoseconds.
* <p>
* If the stopwatch was already running, the previous start time is overwritten.
* </p>
*/
public void start(){
//Make sure the stop time is reset to 0
stopTime = null;
//Get the time as close to returning from the function as possible
startTime = System.nanoTime();
}
//Simulates stopping a stopwatch by saving the time
//?Simulates stopping a stopwatch by saving the time
/**
* Stops the stopwatch by recording the current time in nanoseconds.
* <p>
* If the stopwatch has not been started, this method has no effect.
* </p>
*/
public void stop(){
//Set the stopTime as close to call time as possible
stopTime = System.nanoTime();
@@ -64,42 +118,114 @@ public class Stopwatch{
stopTime = null;
}
}
//Resets all variables in the stopwatch
//?Resets all variables in the stopwatch
/**
* Resets the stopwatch, clearing the start and stop times.
* <p>
* After calling this method, the stopwatch needs to be started again before measuring
* elapsed time.
* </p>
*/
public void reset(){
//Make sure all variables are reset correctly
startTime = null;
stopTime = null;
}
//Returns the time in nanoseconds
//?Returns the time in nanoseconds
/**
* Returns the elapsed time in nanoseconds.
*
* @return the elapsed time in nanoseconds as a {@code double}
*/
public double getNano(){
return getTime().doubleValue();
}
//Returns the time in microseconds
//?Returns the time in microseconds
/**
* Returns the elapsed time in microseconds.
*
* @return the elapsed time in microseconds as a {@code double}
*/
public double getMicro(){
return getTime().doubleValue() / 1000D;
}
//Returns the time in milliseconds
//?Returns the time in milliseconds
/**
* Returns the elapsed time in milliseconds.
*
* @return the elapsed time in milliseconds as a {@code double}
*/
public double getMilli(){
return getTime().doubleValue() / 1000000D;
}
//Returns the time in seconds
//?Returns the time in seconds
/**
* Returns the elapsed time in seconds.
*
* @return the elapsed time in seconds as a {@code double}
*/
public double getSecond(){
return getTime().doubleValue() / 1000000000D;
}
//Returns the time in minutes
//?Returns the time in minutes
/**
* Returns the elapsed time in minutes.
*
* @return the elapsed time in minutes as a {@code double}
*/
public double getMinute(){
return getTime().doubleValue() / 60000000000D;
}
//Returns the time in hours
//?Returns the time in hours
/**
* Returns the elapsed time in hours.
*
* @return the elapsed time in hours as a {@code double}
*/
public double getHour(){
return getTime().doubleValue() / 3600000000000D;
}
//Returns the time as a string at the 'best' resolution. (Goal is xxx.xxx)
//?Returns the time as a string at the 'best' resolution. (Goal is xxx.xxx)
/**
* Returns the elapsed time as a formatted string with the most appropriate resolution.
* <p>
* The method automatically selects the best time resolution to display the time in a
* human-readable format.
* </p>
*
* @return a formatted string representing the elapsed time
* @throws InvalidResult if the time resolution is invalid
*/
public String getStr() throws InvalidResult{
//Get the current duration from time
return getStr(getTime().doubleValue());
}
/**
* Returns a formatted string representing the given time in nanoseconds with the most
* appropriate resolution.
*
* @param nanoseconds the time in nanoseconds to format
* @return a formatted string representing the given time
* @throws InvalidResult if the time resolution is invalid
*/
public static String getStr(double nanoseconds) throws InvalidResult{
Double duration = nanoseconds;
//Reduce the number to the appropriate number of digits. (xxx.x).
@@ -145,6 +271,12 @@ public class Stopwatch{
return time;
}
/**
* Returns a string representation of the elapsed time in the most appropriate resolution.
*
* @return a string representation of the elapsed time
*/
@Override
public String toString(){
return getStr();