checkstyle

This commit is contained in:
jendave
2011-10-26 20:05:11 +00:00
parent c8140b3c2e
commit 108609c6c1
9 changed files with 812 additions and 521 deletions

View File

@@ -1,45 +1,77 @@
package net.slightlymagic.braids.util.lambda;
import net.slightlymagic.braids.util.UtilFunctions;
import static net.slightlymagic.braids.util.UtilFunctions.checkNotNull;
import net.slightlymagic.braids.util.UtilFunctions;
/**
* This embodies a promise to invoke a certain method at a later time; the
* This embodies a promise to invoke a certain method at a later time; the
* FrozenCall remembers the arguments to use and the return type.
*
* @param <T> the return type of apply
*
* @param <T>
* the return type of apply
*
* @see Thunk
*/
public class FrozenCall<T> implements Thunk<T> {
private Lambda<T> proc;
private Object[] args;
private Lambda<T> proc;
private Object[] args;
public FrozenCall(Lambda<T> proc, Object[] args) {
checkNotNull("proc", proc);
checkNotNull("args", args);
this.proc = proc;
this.args = args;
}
/**
* Instantiates a new frozen call.
*
* @param proc
* the proc
* @param args
* the args
*/
public FrozenCall(final Lambda<T> proc, final Object[] args) {
checkNotNull("proc", proc);
checkNotNull("args", args);
public T apply() {
return proc.apply(args);
}
this.proc = proc;
this.args = args;
}
/*
* (non-Javadoc)
*
* @see net.slightlymagic.braids.util.lambda.Thunk#apply()
*/
/**
* @return <T>
*/
public final T apply() {
return proc.apply(args);
}
@Override
public boolean equals(Object obj) {
FrozenCall<T> that = UtilFunctions.checkNullOrNotInstance(this, obj);
if (that == null) return false;
else if (!this.proc.equals(that.proc)) return false;
else if (this.args.length != that.args.length) return false;
/*
* (non-Javadoc)
*
* @see java.lang.Object#equals(java.lang.Object)
*/
/**
* @return boolean
* @param obj Object
*/
@Override
public final boolean equals(final Object obj) {
FrozenCall<T> that = UtilFunctions.checkNullOrNotInstance(this, obj);
if (that == null) {
return false;
} else if (!this.proc.equals(that.proc)) {
return false;
} else if (this.args.length != that.args.length) {
return false;
}
for (int i = 0; i < args.length; i++) {
if (this.args[i] == null && that.args[i] != null) return false;
else if (!this.args[i].equals(that.args[i])) return false;
}
return true;
}
for (int i = 0; i < args.length; i++) {
if (this.args[i] == null && that.args[i] != null) {
return false;
} else if (!this.args[i].equals(that.args[i])) {
return false;
}
}
return true;
}
}

View File

@@ -1,5 +1,19 @@
package net.slightlymagic.braids.util.lambda;
/**
* The Interface Lambda.
*
* @param <R>
* the generic type
*/
public interface Lambda<R> {
public abstract R apply(Object[] args);
/**
* Apply.
*
* @param args
* the args
* @return the r
*/
R apply(Object[] args);
}

View File

@@ -1,13 +1,39 @@
package net.slightlymagic.braids.util.lambda;
public abstract class Lambda1<R,A1> implements Lambda<R> {
/**
* The Class Lambda1.
*
* @param <R>
* the generic type
* @param <A1>
* the generic type
*/
public abstract class Lambda1<R, A1> implements Lambda<R> {
public abstract R apply(A1 arg1);
/**
* Apply.
*
* @param arg1
* the arg1
* @return the r
*/
public abstract R apply(A1 arg1);
@SuppressWarnings("unchecked")
//TODO @Override
public R apply(Object[] args) {
return apply((A1) args[0]);
}
/*
* (non-Javadoc)
*
* @see
* net.slightlymagic.braids.util.lambda.Lambda#apply(java.lang.Object[])
*/
// TODO @Override
/**
* @return R
* @param args Object[]
*/
@SuppressWarnings("unchecked")
public final R apply(final Object[] args) {
return apply((A1) args[0]);
}
}

View File

@@ -1,13 +1,43 @@
package net.slightlymagic.braids.util.lambda;
public abstract class Lambda2<R,A1,A2> implements Lambda<R> {
/**
* The Class Lambda2.
*
* @param <R>
* the generic type
* @param <A1>
* the generic type
* @param <A2>
* the generic type
*/
public abstract class Lambda2<R, A1, A2> implements Lambda<R> {
public abstract R apply(A1 arg1, A2 arg2);
@SuppressWarnings("unchecked")
//TODO @Override
public R apply(Object[] args) {
return apply((A1) args[0], (A2) args[1]);
}
/**
* Apply.
*
* @param arg1
* the arg1
* @param arg2
* the arg2
* @return the r
*/
public abstract R apply(A1 arg1, A2 arg2);
/*
* (non-Javadoc)
*
* @see
* net.slightlymagic.braids.util.lambda.Lambda#apply(java.lang.Object[])
*/
// TODO @Override
/**
* @return R
* @param args Object[]
*/
@SuppressWarnings("unchecked")
public final R apply(final Object[] args) {
return apply((A1) args[0], (A2) args[1]);
}
}

View File

@@ -1,13 +1,47 @@
package net.slightlymagic.braids.util.lambda;
public abstract class Lambda3<R,A1,A2,A3> implements Lambda<R> {
/**
* The Class Lambda3.
*
* @param <R>
* the generic type
* @param <A1>
* the generic type
* @param <A2>
* the generic type
* @param <A3>
* the generic type
*/
public abstract class Lambda3<R, A1, A2, A3> implements Lambda<R> {
public abstract R apply(A1 arg1, A2 arg2, A3 arg3);
@SuppressWarnings("unchecked")
//TODO @Override
public R apply(Object[] args) {
return apply((A1) args[0], (A2) args[1], (A3) args[2]);
}
/**
* Apply.
*
* @param arg1
* the arg1
* @param arg2
* the arg2
* @param arg3
* the arg3
* @return the r
*/
public abstract R apply(A1 arg1, A2 arg2, A3 arg3);
/*
* (non-Javadoc)
*
* @see
* net.slightlymagic.braids.util.lambda.Lambda#apply(java.lang.Object[])
*/
// TODO @Override
/**
* @return R
* @param args Object[]
*/
@SuppressWarnings("unchecked")
public final R apply(final Object[] args) {
return apply((A1) args[0], (A2) args[1], (A3) args[2]);
}
}

View File

@@ -1,5 +1,17 @@
package net.slightlymagic.braids.util.lambda;
/**
* The Interface Thunk.
*
* @param <T>
* the generic type
*/
public interface Thunk<T> {
public abstract T apply();
/**
* Apply.
*
* @return the t
*/
T apply();
}

View File

@@ -14,500 +14,580 @@ import com.esotericsoftware.minlog.Log;
*/
public class BaseProgressMonitor implements BraidsProgressMonitor {
private int numPhases;
private int currentPhase;
private long totalUnitsThisPhase;
private long unitsCompletedSoFarThisPhase;
private float minUIUpdateIntervalSec;
private long lastUIUpdateTime;
private long phaseOneStartTime;
private long currentPhaseStartTime;
private float currentPhaseExponent;
private long[] phaseDurationHistorySecList;
private float[] phaseWeights;
private int currentPhase;
private long totalUnitsThisPhase;
private long unitsCompletedSoFarThisPhase;
private float minUIUpdateIntervalSec;
private long lastUIUpdateTime;
private long phaseOneStartTime;
private long currentPhaseStartTime;
private float currentPhaseExponent;
private long[] phaseDurationHistorySecList;
private float[] phaseWeights;
public final int SECONDS_PER_MINUTE = 60;
public final int SECONDS_PER_HOUR = 60 * SECONDS_PER_MINUTE;
public final int SECONDS_PER_DAY = 24 * SECONDS_PER_HOUR;
/**
* Convenience for
* BaseProgressMonitor(1, 1, 2.0f, null).
/** The SECOND s_ pe r_ minute. */
public final int SECONDS_PER_MINUTE = 60;
/** The SECOND s_ pe r_ hour. */
public final int SECONDS_PER_HOUR = 60 * SECONDS_PER_MINUTE;
/** The SECOND s_ pe r_ day. */
public final int SECONDS_PER_DAY = 24 * SECONDS_PER_HOUR;
/**
* Convenience for BaseProgressMonitor(1, 1, 2.0f, null).
*
* @see #BaseProgressMonitor(int,long,float,float[])
*/
public BaseProgressMonitor() {
this(1, 1L, 2.0f, null);
}
/**
* Convenience for
* BaseProgressMonitor(numPhases, 1, 2.0f, null).
/**
* Convenience for BaseProgressMonitor(numPhases, 1, 2.0f, null).
*
* @param numPhases
* the num phases
* @see #BaseProgressMonitor(int,long,float,float[])
*/
public BaseProgressMonitor(int numPhases) {
public BaseProgressMonitor(final int numPhases) {
this(numPhases, 1L, 2.0f, null);
}
/**
* Convenience for
* BaseProgressMonitor(numPhases, totalUnitsFirstPhase, 2.0f, null).
*
* @see #BaseProgressMonitor(int,long,float,float[])
*/
public BaseProgressMonitor(int numPhases, long totalUnitsFirstPhase) {
this(numPhases, totalUnitsFirstPhase, 2.0f, null);
}
/**
* Convenience for
* BaseProgressMonitor(numPhases, totalUnitsFirstPhase,
* minUIUpdateIntervalSec, null).
*
* Convenience for BaseProgressMonitor(numPhases, totalUnitsFirstPhase,
* 2.0f, null).
*
* @param numPhases
* the num phases
* @param totalUnitsFirstPhase
* the total units first phase
* @see #BaseProgressMonitor(int,long,float,float[])
*/
public BaseProgressMonitor(int numPhases, long totalUnitsFirstPhase,
float minUIUpdateIntervalSec)
{
this(numPhases, totalUnitsFirstPhase, minUIUpdateIntervalSec, null);
public BaseProgressMonitor(final int numPhases, final long totalUnitsFirstPhase) {
this(numPhases, totalUnitsFirstPhase, 2.0f, null);
}
/**
* Convenience for BaseProgressMonitor(numPhases, totalUnitsFirstPhase,
* minUIUpdateIntervalSec, null).
*
* @param numPhases
* the num phases
* @param totalUnitsFirstPhase
* the total units first phase
* @param minUIUpdateIntervalSec
* the min ui update interval sec
* @see #BaseProgressMonitor(int,long,float,float[])
*/
public BaseProgressMonitor(final int numPhases,
final long totalUnitsFirstPhase, final float minUIUpdateIntervalSec) {
this(numPhases, totalUnitsFirstPhase, minUIUpdateIntervalSec, null);
}
/**
* Initializes fields and starts the timers.
*
* @param numPhases the total number of phases we will monitor
* @param numPhases
* the total number of phases we will monitor
*
* @param totalUnitsFirstPhase how many units to expect in phase 1
* @param totalUnitsFirstPhase
* how many units to expect in phase 1
*
* @param minUIUpdateIntervalSec the approximate interval at which we
* update the user interface, in seconds
* @param minUIUpdateIntervalSec
* the approximate interval at which we update the user
* interface, in seconds
*
* @param phaseWeights may be null; if not null, this indicates the
* relative weight of each phase in terms of time to complete all phases.
* Index 0 of this array indicates phase 1's weight, index 1 indicates
* the weight of phase 2, and so on. If null, all phases are considered to
* take an equal amount of time to complete, which is equivalent to setting
* all phase weights to 1.0f. For example, if there are two phases, and
* the phase weights are set to {2.0f, 1.0f}, then the methods that compute
* the final ETA (Estimated Time of Arrival or completion) will assume that
* phase 2 takes half as long as phase 1. In other words, the operation
* will spend 67% of its time in phase 1, and 33% of its time in phase 2.
* @param phaseWeights
* may be null; if not null, this indicates the relative weight
* of each phase in terms of time to complete all phases. Index 0
* of this array indicates phase 1's weight, index 1 indicates
* the weight of phase 2, and so on. If null, all phases are
* considered to take an equal amount of time to complete, which
* is equivalent to setting all phase weights to 1.0f. For
* example, if there are two phases, and the phase weights are
* set to {2.0f, 1.0f}, then the methods that compute the final
* ETA (Estimated Time of Arrival or completion) will assume that
* phase 2 takes half as long as phase 1. In other words, the
* operation will spend 67% of its time in phase 1, and 33% of
* its time in phase 2.
*/
public BaseProgressMonitor(int numPhases, long totalUnitsFirstPhase,
float minUIUpdateIntervalSec, float[] phaseWeights)
{
this.numPhases = numPhases;
this.currentPhase = 1;
this.unitsCompletedSoFarThisPhase = 0L;
this.minUIUpdateIntervalSec = minUIUpdateIntervalSec;
this.lastUIUpdateTime = 0L;
this.phaseOneStartTime = new Date().getTime()/1000;
this.currentPhaseStartTime = this.phaseOneStartTime;
this.currentPhaseExponent = 1;
this.phaseDurationHistorySecList = new long[numPhases];
public BaseProgressMonitor(final int numPhases, final long totalUnitsFirstPhase,
final float minUIUpdateIntervalSec, final float[] phaseWeights) {
this.numPhases = numPhases;
this.currentPhase = 1;
this.unitsCompletedSoFarThisPhase = 0L;
this.minUIUpdateIntervalSec = minUIUpdateIntervalSec;
this.lastUIUpdateTime = 0L;
this.phaseOneStartTime = new Date().getTime() / 1000;
this.currentPhaseStartTime = this.phaseOneStartTime;
this.currentPhaseExponent = 1;
this.phaseDurationHistorySecList = new long[numPhases];
if (phaseWeights == null) {
this.phaseWeights = new float[numPhases];
for (int ix = 0; ix < numPhases; ix++) {
this.phaseWeights[ix] = 1.0f;
}
}
else {
this.phaseWeights = phaseWeights;
}
setTotalUnitsThisPhase(totalUnitsFirstPhase);
if (phaseWeights == null) {
this.phaseWeights = new float[numPhases];
for (int ix = 0; ix < numPhases; ix++) {
this.phaseWeights[ix] = 1.0f;
}
} else {
this.phaseWeights = phaseWeights;
}
setTotalUnitsThisPhase(totalUnitsFirstPhase);
}
/**
* Does nothing.
*/
public void dispose() {
;
}
/**
* @see net.slightlymagic.braids.util.progress_monitor.BraidsProgressMonitor#getNumPhases()
*/
public int getNumPhases() {
* Gets the num phases.
*
* @return the num phases
* @see net.slightlymagic.braids.util.progress_monitor.BraidsProgressMonitor#getNumPhases()
*/
public final int getNumPhases() {
return this.numPhases;
}
/**
* @see net.slightlymagic.braids.util.progress_monitor.BraidsProgressMonitor#getMinUpdateIntervalSec()
*/
public float getMinUpdateIntervalSec() {
* Gets the min update interval sec.
*
* @return the min update interval sec
* @see net.slightlymagic.braids.util.progress_monitor.BraidsProgressMonitor#getMinUpdateIntervalSec()
*/
public final float getMinUpdateIntervalSec() {
return this.minUIUpdateIntervalSec;
}
/**
* @see net.slightlymagic.braids.util.progress_monitor.BraidsProgressMonitor#getCurrentPhase()
*/
public int getCurrentPhase() {
* Gets the current phase.
*
* @return the current phase
* @see net.slightlymagic.braids.util.progress_monitor.BraidsProgressMonitor#getCurrentPhase()
*/
public final int getCurrentPhase() {
return this.currentPhase;
}
/**
* @see net.slightlymagic.braids.util.progress_monitor.BraidsProgressMonitor#getUnitsCompletedSoFarThisPhase()
*/
public long getUnitsCompletedSoFarThisPhase() {
* Gets the units completed so far this phase.
*
* @return the units completed so far this phase
* @see net.slightlymagic.braids.util.progress_monitor.BraidsProgressMonitor#getUnitsCompletedSoFarThisPhase()
*/
public final long getUnitsCompletedSoFarThisPhase() {
return this.unitsCompletedSoFarThisPhase;
}
/**
* @see net.slightlymagic.braids.util.progress_monitor.BraidsProgressMonitor#getTotalUnitsThisPhase()
*/
public long getTotalUnitsThisPhase() {
* Gets the total units this phase.
*
* @return the total units this phase
* @see net.slightlymagic.braids.util.progress_monitor.BraidsProgressMonitor#getTotalUnitsThisPhase()
*/
public final long getTotalUnitsThisPhase() {
return this.totalUnitsThisPhase;
}
/**
* @see net.slightlymagic.braids.util.progress_monitor.BraidsProgressMonitor#getLastUIUpdateTime()
*/
public long getLastUIUpdateTime() {
* Gets the last ui update time.
*
* @return the last ui update time
* @see net.slightlymagic.braids.util.progress_monitor.BraidsProgressMonitor#getLastUIUpdateTime()
*/
public final long getLastUIUpdateTime() {
return this.lastUIUpdateTime;
}
/**
* @see net.slightlymagic.braids.util.progress_monitor.BraidsProgressMonitor#getPhaseOneStartTime()
*/
public long getPhaseOneStartTime() {
* Gets the phase one start time.
*
* @return the phase one start time
* @see net.slightlymagic.braids.util.progress_monitor.BraidsProgressMonitor#getPhaseOneStartTime()
*/
public final long getPhaseOneStartTime() {
return this.phaseOneStartTime;
}
/**
* @see net.slightlymagic.braids.util.progress_monitor.BraidsProgressMonitor#getCurrentPhaseStartTime()
*/
public long getCurrentPhaseStartTime() {
* Gets the current phase start time.
*
* @return the current phase start time
* @see net.slightlymagic.braids.util.progress_monitor.BraidsProgressMonitor#getCurrentPhaseStartTime()
*/
public final long getCurrentPhaseStartTime() {
return this.currentPhaseStartTime;
}
/**
* @see net.slightlymagic.braids.util.progress_monitor.BraidsProgressMonitor#setMinUpdateIntervalSec(float)
*/
public void setMinUpdateIntervalSec(float value) {
* Sets the min update interval sec.
*
* @param value
* the new min update interval sec
* @see net.slightlymagic.braids.util.progress_monitor.BraidsProgressMonitor#setMinUpdateIntervalSec(float)
*/
public final void setMinUpdateIntervalSec(final float value) {
this.minUIUpdateIntervalSec = value;
}
/**
* @see net.slightlymagic.braids.util.progress_monitor.BraidsProgressMonitor#setTotalUnitsThisPhase(long)
*/
public void setTotalUnitsThisPhase(long value) {
* Sets the total units this phase.
*
* @param value
* the new total units this phase
* @see net.slightlymagic.braids.util.progress_monitor.BraidsProgressMonitor#setTotalUnitsThisPhase(long)
*/
public void setTotalUnitsThisPhase(final long value) {
this.totalUnitsThisPhase = value;
}
/**
* @see net.slightlymagic.braids.util.progress_monitor.BraidsProgressMonitor#getPercentCompleteOfThisPhaseAsString()
*/
public String getPercentCompleteOfThisPhaseAsString() {
* Gets the percent complete of this phase as string.
*
* @return the percent complete of this phase as string
* @see net.slightlymagic.braids.util.progress_monitor.BraidsProgressMonitor#getPercentCompleteOfThisPhaseAsString()
*/
public final String getPercentCompleteOfThisPhaseAsString() {
Float percent = getPercentCompleteOfThisPhaseAsFloat();
if (percent != null) {
return Integer.toString((int) (float) percent);
}
else {
} else {
return "??";
}
}
/**
* @see net.slightlymagic.braids.util.progress_monitor.BraidsProgressMonitor#getTotalPercentCompleteAsString()
*/
public String getTotalPercentCompleteAsString() {
* Gets the total percent complete as string.
*
* @return the total percent complete as string
* @see net.slightlymagic.braids.util.progress_monitor.BraidsProgressMonitor#getTotalPercentCompleteAsString()
*/
public final String getTotalPercentCompleteAsString() {
Float percent = getTotalPercentCompleteAsFloat();
if (percent == null) {
return "??";
}
else {
} else {
return Integer.toString((int) (float) percent);
}
}
/**
* Convenience for getRelativeETAAsString(false), meaning to compute the
* value for the end of the last phase.
*
* @see #getRelativeETAAsString(boolean)
*/
public String getRelativeETAAsString() {
return getRelativeETAAsString(false);
* Convenience for getRelativeETAAsString(false), meaning to compute the
* value for the end of the last phase.
*
* @return the relative eta as string
* @see #getRelativeETAAsString(boolean)
*/
public final String getRelativeETAAsString() {
return getRelativeETAAsString(false);
}
/**
* @see net.slightlymagic.braids.util.progress_monitor.BraidsProgressMonitor#getRelativeETAAsString(boolean)
*/
public String getRelativeETAAsString(boolean thisPhaseOnly) {
* Gets the relative eta as string.
*
* @param thisPhaseOnly
* the this phase only
* @return the relative eta as string
* @see net.slightlymagic.braids.util.progress_monitor.BraidsProgressMonitor#getRelativeETAAsString(boolean)
*/
public final String getRelativeETAAsString(final boolean thisPhaseOnly) {
Integer etaSec = getRelativeETASec(thisPhaseOnly);
if (etaSec == null) {
return "unknown";
}
String result = "";
if (etaSec > SECONDS_PER_DAY) {
result += Integer.toString(etaSec / SECONDS_PER_DAY);
result += " da, ";
etaSec %= SECONDS_PER_DAY; // Shave off the portion recorded.
etaSec %= SECONDS_PER_DAY; // Shave off the portion recorded.
}
if (result.length() > 0 || etaSec > SECONDS_PER_HOUR) {
result += Integer.toString(etaSec / SECONDS_PER_HOUR);
result += " hr, ";
etaSec %= SECONDS_PER_HOUR; // Shave off the portion recorded.
}
result += " hr, ";
etaSec %= SECONDS_PER_HOUR; // Shave off the portion recorded.
}
if (result.length() > 0 || etaSec > SECONDS_PER_MINUTE) {
result += Integer.toString(etaSec / SECONDS_PER_MINUTE);
result += " min, ";
etaSec %= SECONDS_PER_MINUTE; // Shave off the portion recorded.
result += " min, ";
etaSec %= SECONDS_PER_MINUTE; // Shave off the portion recorded.
}
result += Integer.toString(etaSec);
result += " sec";
return result;
}
/**
* Convenience for getAbsoluteETAAsLocalTimeString(false), meaning to
* compute the value for the end of the last phase.
*
* @see #getAbsoluteETAAsLocalTimeString(boolean)
*/
public String getAbsoluteETAAsLocalTimeString() {
return getAbsoluteETAAsLocalTimeString(false);
return result;
}
/**
* @see net.slightlymagic.braids.util.progress_monitor.BraidsProgressMonitor#getAbsoluteETAAsLocalTimeString(boolean)
*/
public String getAbsoluteETAAsLocalTimeString(boolean thisPhaseOnly) {
* Convenience for getAbsoluteETAAsLocalTimeString(false), meaning to
* compute the value for the end of the last phase.
*
* @return the absolute eta as local time string
* @see #getAbsoluteETAAsLocalTimeString(boolean)
*/
public final String getAbsoluteETAAsLocalTimeString() {
return getAbsoluteETAAsLocalTimeString(false);
}
/**
* Gets the absolute eta as local time string.
*
* @param thisPhaseOnly
* the this phase only
* @return the absolute eta as local time string
* @see net.slightlymagic.braids.util.progress_monitor.BraidsProgressMonitor#getAbsoluteETAAsLocalTimeString(boolean)
*/
public final String getAbsoluteETAAsLocalTimeString(final boolean thisPhaseOnly) {
Long etaTime = getAbsoluteETATime(thisPhaseOnly);
if (etaTime == null) {
return "unknown";
}
return (new Date(etaTime*1000).toString());
return (new Date(etaTime * 1000).toString());
}
/**
* @see net.slightlymagic.braids.util.progress_monitor.BraidsProgressMonitor#incrementUnitsCompletedThisPhase(long)
*/
public void incrementUnitsCompletedThisPhase(long numUnits) {
* Increment units completed this phase.
*
* @param numUnits
* the num units
* @see net.slightlymagic.braids.util.progress_monitor.BraidsProgressMonitor#incrementUnitsCompletedThisPhase(long)
*/
public void incrementUnitsCompletedThisPhase(final long numUnits) {
this.unitsCompletedSoFarThisPhase += numUnits;
}
/**
* Subclasses must call this immediately after updating the UI, to
* preserve the integrity of the shouldUpdateUI method.
* Subclasses must call this immediately after updating the UI, to preserve
* the integrity of the shouldUpdateUI method.
*/
public void justUpdatedUI() {
this.lastUIUpdateTime = new Date().getTime()/1000;
public final void justUpdatedUI() {
this.lastUIUpdateTime = new Date().getTime() / 1000;
}
/**
* @see net.slightlymagic.braids.util.progress_monitor.BraidsProgressMonitor#shouldUpdateUI()
*/
public boolean shouldUpdateUI() {
* Should update ui.
*
* @return true, if successful
* @see net.slightlymagic.braids.util.progress_monitor.BraidsProgressMonitor#shouldUpdateUI()
*/
public final boolean shouldUpdateUI() {
doctorStartTimes();
long nowTime = (new Date().getTime()/1000);
if (nowTime - this.lastUIUpdateTime >= this.minUIUpdateIntervalSec ||
(this.getUnitsCompletedSoFarThisPhase() ==
this.getTotalUnitsThisPhase()))
{
long nowTime = (new Date().getTime() / 1000);
if (nowTime - this.lastUIUpdateTime >= this.minUIUpdateIntervalSec
|| (this.getUnitsCompletedSoFarThisPhase() == this.getTotalUnitsThisPhase())) {
return true;
}
else {
} else {
return false;
}
}
/**
* @see net.slightlymagic.braids.util.progress_monitor.BraidsProgressMonitor#markCurrentPhaseAsComplete(long)
*/
public void markCurrentPhaseAsComplete(long totalUnitsNextPhase) {
* Mark current phase as complete.
*
* @param totalUnitsNextPhase
* the total units next phase
* @see net.slightlymagic.braids.util.progress_monitor.BraidsProgressMonitor#markCurrentPhaseAsComplete(long)
*/
public final void markCurrentPhaseAsComplete(final long totalUnitsNextPhase) {
if ((this.currentPhase > this.numPhases)) {
String message = "The phase just completed (";
message += this.currentPhase;
message += ") is greater than the total number ";
message += "of anticipated phases (";
message += this.numPhases;
message += "); the latter is probably incorrect.";
message += this.numPhases;
message += "); the latter is probably incorrect.";
Log.warn(message);
}
this.currentPhase += 1;
this.unitsCompletedSoFarThisPhase = 0;
setTotalUnitsThisPhase(totalUnitsNextPhase);
this.currentPhaseExponent = 1;
long nowTime = (new Date().getTime()/1000);
long nowTime = (new Date().getTime() / 1000);
long durationOfThisPhaseSec = nowTime - this.currentPhaseStartTime;
if (durationOfThisPhaseSec < 0) {
durationOfThisPhaseSec = 0;
}
if (0 <= currentPhase-2 && currentPhase-2 < phaseDurationHistorySecList.length) {
this.phaseDurationHistorySecList[currentPhase-2] = durationOfThisPhaseSec;
if (0 <= currentPhase - 2 && currentPhase - 2 < phaseDurationHistorySecList.length) {
this.phaseDurationHistorySecList[currentPhase - 2] = durationOfThisPhaseSec;
}
this.currentPhaseStartTime = nowTime;
if (this.currentPhase >= this.numPhases) {
String message = "Actual individual phase durations: [";
for (int ix = 0 ; ix < phaseDurationHistorySecList.length ; ix++) {
message += phaseDurationHistorySecList[ix] + ", ";
}
Log.info(message + ']');
if (this.currentPhase >= this.numPhases) {
String message = "Actual individual phase durations: [";
for (int ix = 0; ix < phaseDurationHistorySecList.length; ix++) {
message += phaseDurationHistorySecList[ix] + ", ";
}
Log.info(message + ']');
}
}
/**
* @see net.slightlymagic.braids.util.progress_monitor.BraidsProgressMonitor#sendMessage(java.lang.String)
*/
public void sendMessage(String message) {
;
* Send message.
*
* @param message
* the message
* @see net.slightlymagic.braids.util.progress_monitor.BraidsProgressMonitor#sendMessage(java.lang.String)
*/
public final void sendMessage(final String message) {
}
/**
* @see net.slightlymagic.braids.util.progress_monitor.BraidsProgressMonitor#setCurrentPhaseAsExponential(float)
*/
public void setCurrentPhaseAsExponential(float value) {
* Sets the current phase as exponential.
*
* @param value
* the new current phase as exponential
* @see net.slightlymagic.braids.util.progress_monitor.BraidsProgressMonitor#setCurrentPhaseAsExponential(float)
*/
public final void setCurrentPhaseAsExponential(final float value) {
this.currentPhaseExponent = value;
}
/**
* @see net.slightlymagic.braids.util.progress_monitor.BraidsProgressMonitor#getCurrentPhaseExponent()
*/
public float getCurrentPhaseExponent() {
* Gets the current phase exponent.
*
* @return the current phase exponent
* @see net.slightlymagic.braids.util.progress_monitor.BraidsProgressMonitor#getCurrentPhaseExponent()
*/
public final float getCurrentPhaseExponent() {
return this.currentPhaseExponent;
}
/**
* Gets the percent complete of this phase as float.
*
* @return number in range [0.0, 100.0] or null.
*/
protected Float getPercentCompleteOfThisPhaseAsFloat() {
if (this.totalUnitsThisPhase < 1 ||
this.unitsCompletedSoFarThisPhase > this.totalUnitsThisPhase) {
protected final Float getPercentCompleteOfThisPhaseAsFloat() {
if (this.totalUnitsThisPhase < 1 || this.unitsCompletedSoFarThisPhase > this.totalUnitsThisPhase) {
return null;
}
else {
float ratio = ((float) (this.unitsCompletedSoFarThisPhase)) /
((float) this.totalUnitsThisPhase);
} else {
float ratio = ((float) (this.unitsCompletedSoFarThisPhase)) / ((float) this.totalUnitsThisPhase);
ratio = (float) Math.pow(ratio, this.getCurrentPhaseExponent());
return (ratio * 100.0f);
}
}
/**
* Returns number in range [0.0, 100.0] or null.
*
* @return the total percent complete as float
*/
protected Float getTotalPercentCompleteAsFloat() {
protected final Float getTotalPercentCompleteAsFloat() {
long totalPoints = 0;
for (float weight : this.phaseWeights) {
totalPoints += weight * 100;
}
Float percentThisPhase = getPercentCompleteOfThisPhaseAsFloat();
if (percentThisPhase == null) {
// If we can't know the percentage for this phase, use a
// conservative estimate.
percentThisPhase = 0.0f;
}
long pointsSoFar = 0;
for (int ix = 0; ix < this.currentPhase-1; ix++) {
// We get full points for (all the phases completed prior to this one.
for (int ix = 0; ix < this.currentPhase - 1; ix++) {
// We get full points for (all the phases completed prior to this
// one.
pointsSoFar += phaseWeights[ix] * 100;
}
pointsSoFar += percentThisPhase * this.phaseWeights[this.currentPhase-1];
pointsSoFar += percentThisPhase * this.phaseWeights[this.currentPhase - 1];
if (totalPoints <= 0.0 || pointsSoFar > totalPoints) {
return null;
}
else {
} else {
return (100.0f * pointsSoFar) / totalPoints;
}
}
/**
* Convenience for getRelativeETASec(false), meaning to compute the value
* for the end of the last phase.
* for the end of the last phase.
*
* @return the relative eta sec
* @see #getRelativeETASec(boolean)
*/
protected Integer getRelativeETASec() {
return getRelativeETASec(false);
protected final Integer getRelativeETASec() {
return getRelativeETASec(false);
}
/**
* @return estimated seconds until completion for either thisPhaseOnly
* or for the entire operation. May return null if unknown.
* Gets the relative eta sec.
*
* @param thisPhaseOnly
* the this phase only
* @return estimated seconds until completion for either thisPhaseOnly or
* for the entire operation. May return null if unknown.
*/
protected Integer getRelativeETASec(boolean thisPhaseOnly) {
protected final Integer getRelativeETASec(final boolean thisPhaseOnly) {
Long absoluteETATime = getAbsoluteETATime(thisPhaseOnly);
if (absoluteETATime == null) {
return null;
}
return (int) (absoluteETATime - (new Date().getTime()/1000));
return (int) (absoluteETATime - (new Date().getTime() / 1000));
}
/**
* Convenience for getAbsoluteETATime(false), meaning to compute the value
* for the end of all phases.
* for the end of all phases.
*
* @return the absolute eta time
* @see #getAbsoluteETATime(boolean)
*/
protected Long getAbsoluteETATime() {
return getAbsoluteETATime(false);
protected final Long getAbsoluteETATime() {
return getAbsoluteETATime(false);
}
/**
* Gets the absolute eta time.
*
* @param thisPhaseOnly
* the this phase only
* @return the estimated time (in absolute seconds) at which thisPhaseOnly
* or the entire operation will be completed. May return null if (unknown.
* or the entire operation will be completed. May return null if
* (unknown.
*/
protected Long getAbsoluteETATime(boolean thisPhaseOnly) {
protected final Long getAbsoluteETATime(boolean thisPhaseOnly) {
doctorStartTimes();
// If we're in the last phase, the overall ETA is the same as the ETA
// for (this particular phase.
if (this.getCurrentPhase() >= this.getNumPhases()) {
thisPhaseOnly = true;
}
Float percentDone = null;
long startTime = 0L;
if (thisPhaseOnly) {
percentDone = getPercentCompleteOfThisPhaseAsFloat();
startTime = this.currentPhaseStartTime;
}
else {
} else {
percentDone = getTotalPercentCompleteAsFloat();
startTime = this.phaseOneStartTime;
}
@@ -515,31 +595,29 @@ public class BaseProgressMonitor implements BraidsProgressMonitor {
if (percentDone == null || percentDone <= 0.001) {
return null;
}
// Elapsed time is to percent done as total time is to total done =>
// elapsed/percentDone == totalTime/100.0 =>
long totalTime = (long) (100.0f * ((new Date().getTime()/1000) - startTime) / percentDone);
long totalTime = (long) (100.0f * ((new Date().getTime() / 1000) - startTime) / percentDone);
return totalTime + startTime;
}
/**
* Repair the start times in case the system clock has been moved
* backwards.
* Repair the start times in case the system clock has been moved backwards.
*/
protected void doctorStartTimes() {
long nowTime = (new Date().getTime()/1000);
protected final void doctorStartTimes() {
long nowTime = (new Date().getTime() / 1000);
if (this.lastUIUpdateTime > nowTime) {
this.lastUIUpdateTime = 0;
}
if (this.phaseOneStartTime > nowTime) {
this.phaseOneStartTime = nowTime;
}
if (this.currentPhaseStartTime > nowTime) {
this.currentPhaseStartTime = nowTime;
}

View File

@@ -1,158 +1,194 @@
package net.slightlymagic.braids.util.progress_monitor;
/**
* Interface for a progress monitor that can have multiple phases
* and periodically update its UI.
* Interface for a progress monitor that can have multiple phases and
* periodically update its UI.
*
* All times must be in seconds; absolute times are measured in seconds since
* 01 Jan 1970 00:00:00 UTC (GMT) a la (new Date().getTime()/1000).
* All times must be in seconds; absolute times are measured in seconds since 01
* Jan 1970 00:00:00 UTC (GMT) a la (new Date().getTime()/1000).
*/
public interface BraidsProgressMonitor {
/**
* Destroy this progress monitor, making it no longer usable and/or
* visible.
*/
public void dispose();
/**
* @return the total number of phases monitored by this object.
*/
public abstract int getNumPhases();
/**
* @return the approximate minimum interval in seconds at which the UI
* should be updated.
*/
public abstract float getMinUpdateIntervalSec();
/**
* @return the current phase number; this is never less than 1 (one).
*/
public abstract int getCurrentPhase();
/**
* @return the number of units (an intentionally vague amount) completed
* so far in the current phase.
*/
public abstract long getUnitsCompletedSoFarThisPhase();
/**
* @return the total units we expect to process in this phase
*/
public abstract long getTotalUnitsThisPhase();
/**
* @return the time in absolute seconds since the UI was last updated
*/
public abstract long getLastUIUpdateTime();
/**
* @return the time in absolute seconds at which the first phase started
*/
public abstract long getPhaseOneStartTime();
/**
* @return the time in absolute seconds at which the current phase started
*/
public abstract long getCurrentPhaseStartTime();
/**
* @param value
* the approximate time in relative seconds at which the UI
* should be updated periodically
*/
public abstract void setMinUpdateIntervalSec(float value);
/**
* @param value the total number of units expected to processed in this
* phase
*/
public abstract void setTotalUnitsThisPhase(long value);
/**
* Resulting string does not contain a percent sign.
*
* @return the percentage completion of this phase as a String with no
* percent sign.
*/
public abstract String getPercentCompleteOfThisPhaseAsString();
/**
* Resulting string does not contain a percent sign.
*
* @return the percentage completion at this point, taking into account all
* phases and phase-weights, as a String with no percent sign.
*/
public abstract String getTotalPercentCompleteAsString();
/**
* May return "unknown"
*/
public abstract String getRelativeETAAsString(boolean thisPhaseOnly);
/**
* May return "unknown"
*/
public abstract String getAbsoluteETAAsLocalTimeString(boolean thisPhaseOnly);
/**
* Note this will NOT advance the phase.
* To do that, use markCurrentPhaseAsComplete().
*/
public abstract void incrementUnitsCompletedThisPhase(long numUnits);
/**
* Returns a boolean, whether or not to display the updated information.
* This throttles the update so it doesn't refresh so fast that it is
* unreadable. Implementers should call this method from their own
* incrementUnitsCompletedThisPhase method.
*
* If we have just reached 100% for (the current phase, we return true,
* even if it would otherwise be too soon to update the UI.
*/
public abstract boolean shouldUpdateUI();
/**
* Subclasses must call this immediately after updating the UI, to
* preserve the integrity of the shouldUpdateUI method.
/**
* Destroy this progress monitor, making it no longer usable and/or visible.
*/
public abstract void justUpdatedUI();
void dispose();
/**
* This is the only way to advance the phase number.
* It automatically "starts the clock" for the next phase.
*
* @param totalUnitsNextPhase if unknown, use zero (0), and be sure to call
* setTotalUnitsThisPhase() soon after.
*/
public abstract void markCurrentPhaseAsComplete(long totalUnitsNextPhase);
/**
* Gets the num phases.
*
* @return the total number of phases monitored by this object.
*/
int getNumPhases();
/**
* Attempt to display a message to the user; not all implementations
* support this.
*
* If they do not, they may silently ignore this call.
*
* @param message the message to display
*/
public abstract void sendMessage(String message);
/**
* Gets the min update interval sec.
*
* @return the approximate minimum interval in seconds at which the UI
* should be updated.
*/
float getMinUpdateIntervalSec();
/**
* Mark the current phase as having an exponential rate; such phases
* reach their totalUnits slower and slower as they process more units.
*
* By default, a phase is considered to be linear, meaning this value is
* 1.0f.
*
* @param value usually less than 1.0f; often determined empirically.
*/
public abstract void setCurrentPhaseAsExponential(float value);
/**
* Gets the current phase.
*
* @return the current phase number; this is never less than 1 (one).
*/
int getCurrentPhase();
/**
* @return the exponent for this phase
*/
public abstract float getCurrentPhaseExponent();
/**
* Gets the units completed so far this phase.
*
* @return the number of units (an intentionally vague amount) completed so
* far in the current phase.
*/
long getUnitsCompletedSoFarThisPhase();
/**
* Gets the total units this phase.
*
* @return the total units we expect to process in this phase
*/
long getTotalUnitsThisPhase();
/**
* Gets the last ui update time.
*
* @return the time in absolute seconds since the UI was last updated
*/
long getLastUIUpdateTime();
/**
* Gets the phase one start time.
*
* @return the time in absolute seconds at which the first phase started
*/
long getPhaseOneStartTime();
/**
* Gets the current phase start time.
*
* @return the time in absolute seconds at which the current phase started
*/
long getCurrentPhaseStartTime();
/**
* Sets the min update interval sec.
*
* @param value
* the approximate time in relative seconds at which the UI
* should be updated periodically
*/
void setMinUpdateIntervalSec(float value);
/**
* Sets the total units this phase.
*
* @param value
* the total number of units expected to processed in this phase
*/
void setTotalUnitsThisPhase(long value);
/**
* Resulting string does not contain a percent sign.
*
* @return the percentage completion of this phase as a String with no
* percent sign.
*/
String getPercentCompleteOfThisPhaseAsString();
/**
* Resulting string does not contain a percent sign.
*
* @return the percentage completion at this point, taking into account all
* phases and phase-weights, as a String with no percent sign.
*/
String getTotalPercentCompleteAsString();
/**
* May return "unknown".
*
* @param thisPhaseOnly
* the this phase only
* @return the relative eta as string
*/
String getRelativeETAAsString(boolean thisPhaseOnly);
/**
* May return "unknown".
*
* @param thisPhaseOnly
* the this phase only
* @return the absolute eta as local time string
*/
String getAbsoluteETAAsLocalTimeString(boolean thisPhaseOnly);
/**
* Note this will NOT advance the phase. To do that, use
* markCurrentPhaseAsComplete().
*
* @param numUnits
* the num units
*/
void incrementUnitsCompletedThisPhase(long numUnits);
/**
* Returns a boolean, whether or not to display the updated information.
* This throttles the update so it doesn't refresh so fast that it is
* unreadable. Implementers should call this method from their own
* incrementUnitsCompletedThisPhase method.
*
* If we have just reached 100% for (the current phase, we return true, even
* if it would otherwise be too soon to update the UI.
*
* @return true, if successful
*/
boolean shouldUpdateUI();
/**
* Subclasses must call this immediately after updating the UI, to preserve
* the integrity of the shouldUpdateUI method.
*/
void justUpdatedUI();
/**
* This is the only way to advance the phase number. It automatically
* "starts the clock" for the next phase.
*
* @param totalUnitsNextPhase
* if unknown, use zero (0), and be sure to call
* setTotalUnitsThisPhase() soon after.
*/
void markCurrentPhaseAsComplete(long totalUnitsNextPhase);
/**
* Attempt to display a message to the user; not all implementations support
* this.
*
* If they do not, they may silently ignore this call.
*
* @param message
* the message to display
*/
void sendMessage(String message);
/**
* Mark the current phase as having an exponential rate; such phases reach
* their totalUnits slower and slower as they process more units.
*
* By default, a phase is considered to be linear, meaning this value is
* 1.0f.
*
* @param value
* usually less than 1.0f; often determined empirically.
*/
void setCurrentPhaseAsExponential(float value);
/**
* Gets the current phase exponent.
*
* @return the exponent for this phase
*/
float getCurrentPhaseExponent();
}

View File

@@ -1,78 +1,107 @@
package net.slightlymagic.braids.util.progress_monitor;
/**
* The Class StderrProgressMonitor.
*/
public class StderrProgressMonitor extends BaseProgressMonitor {
/**
* @see net.slightlymagic.braids.util.progress_monitor.BaseProgressMonitor#BaseProgressMonitor(int, long)
*/
public StderrProgressMonitor(int numPhases, long totalUnitsFirstPhase) {
this(numPhases, totalUnitsFirstPhase, 2.0f, null);
/**
* Instantiates a new stderr progress monitor.
*
* @param numPhases
* the num phases
* @param totalUnitsFirstPhase
* the total units first phase
* @see net.slightlymagic.braids.util.progress_monitor.BaseProgressMonitor#BaseProgressMonitor(int,
* long)
*/
public StderrProgressMonitor(final int numPhases, final long totalUnitsFirstPhase) {
this(numPhases, totalUnitsFirstPhase, 2.0f, null);
}
/**
* @see net.slightlymagic.braids.util.progress_monitor.BaseProgressMonitor#BaseProgressMonitor(int, long, float)
*/
public StderrProgressMonitor(int numPhases, long totalUnitsFirstPhase,
float minUpdateIntervalSec)
{
this(numPhases, totalUnitsFirstPhase, minUpdateIntervalSec, null);
}
/**
* @see net.slightlymagic.braids.util.progress_monitor.BaseProgressMonitor#BaseProgressMonitor(int, long, float, float[])
*/
public StderrProgressMonitor(int numPhases, long totalUnitsFirstPhase,
float minUpdateIntervalSec, float[] phaseWeights)
/**
* Instantiates a new stderr progress monitor.
*
* @param numPhases
* the num phases
* @param totalUnitsFirstPhase
* the total units first phase
* @param minUpdateIntervalSec
* the min update interval sec
* @see net.slightlymagic.braids.util.progress_monitor.BaseProgressMonitor#BaseProgressMonitor(int,
* long, float)
*/
public StderrProgressMonitor(final int numPhases,
final long totalUnitsFirstPhase, final float minUpdateIntervalSec) {
this(numPhases, totalUnitsFirstPhase, minUpdateIntervalSec, null);
}
/**
* Instantiates a new stderr progress monitor.
*
* @param numPhases
* the num phases
* @param totalUnitsFirstPhase
* the total units first phase
* @param minUpdateIntervalSec
* the min update interval sec
* @param phaseWeights
* the phase weights
* @see net.slightlymagic.braids.util.progress_monitor.BaseProgressMonitor#BaseProgressMonitor(int,
* long, float, float[])
*/
public StderrProgressMonitor(final int numPhases, final long totalUnitsFirstPhase,
final float minUpdateIntervalSec, final float[] phaseWeights)
{
super(numPhases, totalUnitsFirstPhase, minUpdateIntervalSec, phaseWeights);
}
/*
* (non-Javadoc)
*
* @see net.slightlymagic.braids.util.progress_monitor.BaseProgressMonitor#
* incrementUnitsCompletedThisPhase(long)
*/
/**
* @see net.slightlymagic.braids.util.progress_monitor.BaseProgressMonitor#BaseProgressMonitor(int)
*/
@Override
/**
* @see net.slightlymagic.braids.util.progress_monitor.BaseProgressMonitor#BaseProgressMonitor(int)
*/
public void incrementUnitsCompletedThisPhase(long numUnits) {
public final void incrementUnitsCompletedThisPhase(final long numUnits) {
super.incrementUnitsCompletedThisPhase(numUnits);
if (shouldUpdateUI()) {
if ((getNumPhases() > 1)) {
printUpdate(
"Phase " + getCurrentPhase() + ": " +
getUnitsCompletedSoFarThisPhase() + " units processed. " +
"Overall: " + getTotalPercentCompleteAsString() + "% complete, " +
"ETA in " + getRelativeETAAsString() + "."
);
}
else {
printUpdate(
"Overall: " +
getUnitsCompletedSoFarThisPhase() + " units processed " +
"(" + getTotalPercentCompleteAsString() + "%); " +
"ETA in " + getRelativeETAAsString() + "."
);
if ((getNumPhases() > 1)) {
printUpdate("Phase " + getCurrentPhase() + ": " + getUnitsCompletedSoFarThisPhase()
+ " units processed. " + "Overall: " + getTotalPercentCompleteAsString() + "% complete, "
+ "ETA in " + getRelativeETAAsString() + ".");
} else {
printUpdate("Overall: " + getUnitsCompletedSoFarThisPhase() + " units processed " + "("
+ getTotalPercentCompleteAsString() + "%); " + "ETA in " + getRelativeETAAsString() + ".");
}
}
}
}
/**
* Displays a message to stderr, overwriting the current estimate; calls
* from outside this class should provide a newline character at the
* end of the messsage.
* from outside this class should provide a newline character at the end of
* the messsage.
*
* @param message the message to display
* @param message
* the message to display
*/
public void printUpdate(String message) {
while (message.length() < 79) {
message += ' ';
}
public final void printUpdate(String message) {
while (message.length() < 79) {
message += ' ';
}
System.err.print("\r");
System.err.print(message);
if (message.length() > 79) {
System.err.print("\n");
System.err.print("\n");
}
justUpdatedUI();