mirror of
https://bitbucket.org/Mattrixwv/javaclasses.git
synced 2025-12-07 07:23:57 -05:00
Added number generators
This commit is contained in:
153
src/main/java/com/mattrixwv/Stopwatch.java
Normal file
153
src/main/java/com/mattrixwv/Stopwatch.java
Normal file
@@ -0,0 +1,153 @@
|
||||
//JavaClasses/src/main/java/mattrixwv/Stopwatch.java
|
||||
//Matthew Ellison (Mattrixwv)
|
||||
// Created: 03-01-19
|
||||
//Modified: 06-26-22
|
||||
//This file contains a class that is used to time the execution time of other programs
|
||||
/*
|
||||
Copyright (C) 2022 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
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.mattrixwv;
|
||||
|
||||
|
||||
import com.mattrixwv.exceptions.InvalidResult;
|
||||
|
||||
|
||||
public class Stopwatch{
|
||||
private Long startTime;
|
||||
private Long stopTime;
|
||||
//Constructor makes sure all values are set to defaults
|
||||
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
|
||||
private Long getTime(){
|
||||
if(startTime == null){
|
||||
return 0L;
|
||||
}
|
||||
else if(stopTime == null){
|
||||
return System.nanoTime() - startTime;
|
||||
}
|
||||
else{
|
||||
return stopTime - startTime;
|
||||
}
|
||||
}
|
||||
//An enum that helps keep track of how many times the time has been reduced in the getStr function
|
||||
private enum TIME_RESOLUTION{ NANOSECOND, MICROSECOND, MILLISECOND, SECOND, MINUTE, HOUR, ERROR }
|
||||
//Simulates starting a stopwatch by saving the time
|
||||
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
|
||||
public void stop(){
|
||||
//Set the stopTime as close to call time as possible
|
||||
stopTime = System.nanoTime();
|
||||
//If the startTime has not been set then reset stopTime
|
||||
if(startTime == null){
|
||||
stopTime = null;
|
||||
}
|
||||
}
|
||||
//Resets all variables in the stopwatch
|
||||
public void reset(){
|
||||
//Make sure all variables are reset correctly
|
||||
startTime = null;
|
||||
stopTime = null;
|
||||
}
|
||||
//Returns the time in nanoseconds
|
||||
public double getNano(){
|
||||
return getTime().doubleValue();
|
||||
}
|
||||
//Returns the time in microseconds
|
||||
public double getMicro(){
|
||||
return getTime().doubleValue() / 1000D;
|
||||
}
|
||||
//Returns the time in milliseconds
|
||||
public double getMilli(){
|
||||
return getTime().doubleValue() / 1000000D;
|
||||
}
|
||||
//Returns the time in seconds
|
||||
public double getSecond(){
|
||||
return getTime().doubleValue() / 1000000000D;
|
||||
}
|
||||
//Returns the time in minutes
|
||||
public double getMinute(){
|
||||
return getTime().doubleValue() / 60000000000D;
|
||||
}
|
||||
//Returns the time in hours
|
||||
public double getHour(){
|
||||
return getTime().doubleValue() / 3600000000000D;
|
||||
}
|
||||
//Returns the time as a string at the 'best' resolution. (Goal is xxx.xxx)
|
||||
public String getStr() throws InvalidResult{
|
||||
//Get the current duration from time
|
||||
return getStr(getTime().doubleValue());
|
||||
}
|
||||
|
||||
public static String getStr(double nanoseconds) throws InvalidResult{
|
||||
Double duration = nanoseconds;
|
||||
//Reduce the number to the appropriate number of digits. (xxx.x).
|
||||
//This loop works down to seconds
|
||||
TIME_RESOLUTION resolution;
|
||||
for(resolution = TIME_RESOLUTION.NANOSECOND;(resolution.ordinal() < TIME_RESOLUTION.SECOND.ordinal()) && (duration >= 1000);resolution = TIME_RESOLUTION.values()[resolution.ordinal() + 1]){
|
||||
duration /= 1000;
|
||||
}
|
||||
//Check if the duration needs reduced to minutes
|
||||
if((duration >= 120) && (resolution == TIME_RESOLUTION.SECOND)){
|
||||
//Reduce to minutes
|
||||
duration /= 60;
|
||||
resolution = TIME_RESOLUTION.values()[resolution.ordinal() + 1];
|
||||
|
||||
//Check if the duration needs reduced to hours
|
||||
if(duration >= 60){
|
||||
//Reduce to hours
|
||||
duration /= 60;
|
||||
resolution = TIME_RESOLUTION.values()[resolution.ordinal() + 1];
|
||||
}
|
||||
}
|
||||
|
||||
//Turn the number into a string
|
||||
int durationFraction = (int)Math.round(((duration % 1) * 1000));
|
||||
String time = String.format("%d.%03d", duration.intValue(), durationFraction);
|
||||
|
||||
//Tack on the appropriate suffix for resolution
|
||||
switch(resolution){
|
||||
case NANOSECOND: time += " nanoseconds"; break;
|
||||
case MICROSECOND: time += " microseconds"; break;
|
||||
case MILLISECOND: time += " milliseconds"; break;
|
||||
case SECOND: time += " seconds"; break;
|
||||
case MINUTE: time += " minutes"; break;
|
||||
case HOUR: time += " hours"; break;
|
||||
case ERROR:
|
||||
default: throw new InvalidResult("timeResolution was invalid");
|
||||
}
|
||||
//Return the string
|
||||
return time;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString(){
|
||||
try{
|
||||
return getStr();
|
||||
}
|
||||
catch(InvalidResult error){
|
||||
return "There was an error in getStr(): " + error;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user