final long beginTime_ms = System.currentTimeMillis(); // begin time
long elapsedTime_ms = 0; // total elapsed time
final long checkTimeInterval_ms = 100; // check every 100 ms
if (!(value instanceof Comparable)) {
throw new QTasteDataException("Value is not an instance of Comparable");
}
do {
Object lastValue;
try {
Data lastData = getLast(name);
lastValue = lastData.getValue();
int comparisonResult = ((Comparable) value).compareTo(lastValue);
boolean comparisonSuccessful = false;
switch (comparator) {
case COMPARATOR_EQ:
comparisonSuccessful = comparisonResult == 0;
break;
case COMPARATOR_NEQ:
comparisonSuccessful = comparisonResult != 0;
break;
case COMPARATOR_LT:
comparisonSuccessful = comparisonResult > 0;
break;
case COMPARATOR_GT:
comparisonSuccessful = comparisonResult < 0;
break;
case COMPARATOR_LEQ:
comparisonSuccessful = comparisonResult >= 0;
break;
case COMPARATOR_GEQ:
comparisonSuccessful = comparisonResult <= 0;
break;
}
if (comparisonSuccessful) {
return lastData;
}
} catch (QTasteTestFailException e) {
// variable not in cache
lastValue = null;
}
if (elapsedTime_ms >= timeout) {
String lastValueStr = (lastValue == null ? "null" : lastValue.toString());
String comparatorStr = "";
switch (comparator) {
case COMPARATOR_EQ:
comparatorStr = "==";
break;
case COMPARATOR_NEQ:
comparatorStr = "!=";
break;
case COMPARATOR_LT:
comparatorStr = "<";
break;
case COMPARATOR_GT:
comparatorStr = ">";
break;
case COMPARATOR_LEQ:
comparatorStr = "<=";
break;
case COMPARATOR_GEQ:
comparatorStr = ">=";
break;
}
throw new QTasteTestFailException("Variable " + name + " value (" + lastValueStr + ") didn't reach expected value (" + comparatorStr + " " + value + ")");
}
// wait
try {
Thread.sleep(checkTimeInterval_ms);
elapsedTime_ms = System.currentTimeMillis() - beginTime_ms;
} catch (InterruptedException e) {
throw new QTasteDataException("Sleep interrupted");
}
} while (true);
}