I have an Util class that i use for Stats computation. An Exponential moving average is calculated among multiple threads. Also this threads pass differents values some times double and others long. I want to make generics and also be sure that when applying synchronized to the method's signature.
public class StatUtils {
public static class WMA {
// MMA
}
public static class EMA {
/** The alpha. */
private static double staticAlpha = 0.9;
/** The old value. */
private static double staticOldValue = 1.0;
/**
* Compute.
*
* @param pValue the value
* @return the double
*/
public static synchronized double compute(double pValue) {
if (staticOldValue == 0.0) {
staticOldValue = pValue;
return pValue;
}
double lValue = staticOldValue + staticAlpha * (pValue - staticOldValue);
staticOldValue = lValue;
return lValue;
}
}
}
Is the compute method thread safe ? If Yes is that possible to make this static class generic ?