I calculate the moving averages of a specific measure. However, multiples threads can access this class in order to compute the average.
public class AvgUtils {
/** The alpha. */
private static final double ALPHA = 0.9;
private double avgOldValue = 1.0;
public synchronized double computeAvg(double pValue) {
if (avgOldValue == 0.0d) {
avgOldValue = pValue;
return pValue;
}
double lValue = avgOldValue + ALPHA * (pValue - avgOldValue);
avgOldValue = lValue;
return lValue;
}
}
In the other class, I'm obliged to create a new instance of this class in other every time to do the calculations.
Can my class be refactored or redesigned in a better way ?
delayOldValuebut you never declare or use it. \$\endgroup\$AvgOldValuenow? (no, it's not defined in your code) Please don't apply sloppy "hot fix" edits to the posted code that only introduce more errors. Post the real code you are using. Don't create some special version you derived to post here. Give us the real thing. This way, you get a real review. \$\endgroup\$