Botre Posted March 6, 2015 Share Posted March 6, 2015 (edited) LAST UPDATE: MARCH 6, 2015 package org.bjornkrols.timing; /** * A base for basic timer subclasses. * * @author Bjorn Krols (Botre) * @version 0.1 * @since March 6, 2015 */ public abstract class BasicAbstractTimer { protected long st; protected int mtups; protected BasicAbstractTimer() { start(); } public void start() { st = fetchCurrentTime(); } public void restart() { start(); } protected abstract long fetchCurrentTime(); public double getElapsedDays(){ return getElapsedSeconds() / 86400; } public double getElapsedHours(){ return getElapsedSeconds() / 86400; } public double getElapsedSeconds() { return (fetchCurrentTime() - st) / mtups; } public double getElapsedMilliSeconds() { return getElapsedSeconds() / 1000; } public double getElapsedNanoSeconds() { return getElapsedSeconds() / 1000000000; } public int[] distribute() { int seconds = (int) getElapsedSeconds(); int d[] = new int[4]; d[0] = (seconds / 86400); // Days. d[1] = ((seconds % 86400) / 3600); // Hours. d[2] = (((seconds % 86400) % 3600) / 60); // Minutes. d[3] = (((seconds % 86400) % 3600) % 60); // Seconds. return d; } @Override public String toString() { StringBuilder sb = new StringBuilder(); int[] d = distribute(); for (int i = 0; i < d.length; i++) { sb.append(String.format("%02d", d[(int) i])); if (i != d.length - 1) sb.append(":"); } return sb.toString(); } } package org.bjornkrols.timing; /** * A millisecond precision timer. * * @author Bjorn Krols (Botre) * @version 0.0 * @since March 6, 2015 */ public final class BasicMilliTimer extends BasicAbstractTimer { public BasicMilliTimer() { super(); mtups = 1000; } @Override protected long fetchCurrentTime() { return System.currentTimeMillis(); } @Override public double getElapsedMilliSeconds() { return st - fetchCurrentTime(); } } package org.bjornkrols.timing; /** * A nanosecond precision timer. * * @author Bjorn Krols (Botre) * @version 0.0 * @since March 6, 2015 */ public final class BasicNanoTimer extends BasicAbstractTimer { public BasicNanoTimer() { super(); mtups = 1000000000; } @Override protected long fetchCurrentTime() { return System.nanoTime(); } @Override public double getElapsedNanoSeconds() { return st - fetchCurrentTime(); } } Edited March 6, 2015 by Botre Quote Link to comment Share on other sites More sharing options...
Joseph Posted March 6, 2015 Share Posted March 6, 2015 Why did you make the basic timer class abstract? Quote Link to comment Share on other sites More sharing options...
Botre Posted March 6, 2015 Author Share Posted March 6, 2015 Why did you make the basic timer class abstract? Because it doesn't have a way to the fetch the current time or a valid mtups value to convert between time units and therefore shouldn't be instantiated, only extended. Quote Link to comment Share on other sites More sharing options...
Joseph Posted March 6, 2015 Share Posted March 6, 2015 Because it doesn't have a way to the fetch the current time or a valid mtups value to convert between time units and therefore shouldn't be instantiated, only extended.then remove the abstract and add in an argument to the constructor to allow the input of the current time. Quote Link to comment Share on other sites More sharing options...
Botre Posted March 6, 2015 Author Share Posted March 6, 2015 then remove the abstract and add in an argument to the constructor to allow the input of the current time. Passing a method as a parameter is tedious imho. I could make fetchCurrentTime() non abstract and have it take a long argument for current time and an int for converting to other time units but the current implementation is more efficient (I could also get rid of fetchCurrentTime() and make the getElaspsed...() methods take those parameters but again, no need for that). Perhaps I misunderstood, if I did feel free to post some pseudocode :p Quote Link to comment Share on other sites More sharing options...
Joseph Posted March 6, 2015 Share Posted March 6, 2015 (edited) Passing a method as a parameter is tedious imho. I could make fetchCurrentTime() non abstract and have it take a long argument for current time and an int for converting to other time units but the current implementation is more efficient (I could also get rid of fetchCurrentTime() and make the getElaspsed...() methods take those parameters but again, no need for that). Perhaps I misunderstood, if I did feel free to post some pseudocode ya you miss understood me a little, but i understand where your coming from. You have the abstract class there as the basic timer. Then you have the other classes extending the basic class so you can easily convert the timer into mili second and nano seconds and stuff. Personally i would just keep one class. Have the constructor with an argument. Which allow the user to set the current time (in mil secs ofc). All I need is this basic class. (i posted the code for the noobies, trust me your code might seem a bit confusing to them at first.) public class Timer { private long period; private long start; public Timer(long period) { this.period = period; this.start = System.currentTimeMillis(); } public long getElapsed() { return System.currentTimeMillis() - this.start; } public long getRemaining() { return this.period - this.getElapsed(); } public boolean isRunning() { return this.getElapsed() <= this.period; } public void reset() { this.start = System.currentTimeMillis(); } public void stop() { this.period = 0; } public static String format(long milliSeconds) { long secs = milliSeconds / 1000L; return String.format("%02d:%02d:%02d", new Object[] { Long.valueOf(secs / 3600L), Long.valueOf(secs % 3600L/ 60L), Long.valueOf(secs % 60L) }); } } Edited March 6, 2015 by josedpay Quote Link to comment Share on other sites More sharing options...