diff --git a/Core/src/ca/uqac/lif/cep/functions/CumulativeFunction.java b/Core/src/ca/uqac/lif/cep/functions/CumulativeFunction.java index b6558bce..f3469a30 100644 --- a/Core/src/ca/uqac/lif/cep/functions/CumulativeFunction.java +++ b/Core/src/ca/uqac/lif/cep/functions/CumulativeFunction.java @@ -22,97 +22,108 @@ /** * A function with memory. - * + * * @author Sylvain Hallé * @since 0.1 */ -public class CumulativeFunction extends UnaryFunction -{ - /** - * The last value returned by the function - */ - private T m_lastValue; - - /** - * The stateless binary function to apply on each call - */ - private BinaryFunction m_function; - - /** - * Instantiates a new cumulative function - * @param function The function to cumulate - */ - public CumulativeFunction(BinaryFunction function) - { - super(function.getInputTypeLeft(), function.getOutputType()); - m_function = function; - m_lastValue = m_function.getStartValue(); - } - - @Override - public T getValue(T x) - { - if (m_lastValue == null) - { - // If the function did not provide a start value, use the - // first given argument as the start value - m_lastValue = x; - return x; +public class CumulativeFunction extends UnaryFunction { + /** + * The last value returned by the function + */ + private T m_lastValue; + + /** + * The start value of the function + */ + + private T m_startValue; + + /** + * The stateless binary function to apply on each call + */ + private BinaryFunction m_function; + + /** + * Instantiates a new cumulative function + * + * @param function The function to cumulate + */ + public CumulativeFunction(BinaryFunction function) { + // If no start value is provided, we use the start value of the function that gived to the CumulativeFunction constructor + this(function, function.getStartValue()); } - T value = m_function.getValue(m_lastValue, x); - m_lastValue = value; - return value; - } - - @Override - public void reset() - { - m_lastValue = m_function.getStartValue(); - } - - @Override - public CumulativeFunction duplicate(boolean with_state) - { - CumulativeFunction cf = new CumulativeFunction(m_function.duplicate(with_state)); - if (with_state) - { - cf.m_lastValue = m_lastValue; + + /** + * Instantiates a new cumulative function with a start value + * + * @param function the function to cumulate + * @param startValue the start value of the function + */ + public CumulativeFunction(BinaryFunction function, T startValue) { + super(function.getInputTypeLeft(), function.getOutputType()); + m_function = function; + // We store the start value in case of reset + m_startValue = startValue; + m_lastValue = startValue; + } + + @Override + public T getValue(T x) { + if (m_lastValue == null) { + // If the function did not provide a start value, use the + // first given argument as the start value + m_lastValue = x; + return x; + } + T value = m_function.getValue(m_lastValue, x); + m_lastValue = value; + return value; + } + + @Override + public void reset() { + m_lastValue = this.m_startValue; + } + + @Override + public CumulativeFunction duplicate(boolean with_state) { + CumulativeFunction cf = new CumulativeFunction(m_function.duplicate(with_state)); + if (with_state) { + cf.m_lastValue = m_lastValue; + } + return cf; + } + + /** + * @since 0.10.2 + */ + @Override + public Object printState() { + List list = new ArrayList(2); + list.add(m_function); + list.add(m_lastValue); + return list; + } + + /** + * @since 0.10.2 + */ + @SuppressWarnings("unchecked") + public CumulativeFunction readState(Object o) { + List list = (List) o; + BinaryFunction func = (BinaryFunction) list.get(0); + CumulativeFunction cf = new CumulativeFunction(func); + cf.m_lastValue = (T) list.get(1); + return cf; + } + + /** + * @return The last value + * @since 0.11 + */ + /*@ pure @*/ + public T getLastValue() { + return m_lastValue; } - return cf; - } - - /** - * @since 0.10.2 - */ - @Override - public Object printState() - { - List list = new ArrayList(2); - list.add(m_function); - list.add(m_lastValue); - return list; - } - - /** - * @since 0.10.2 - */ - @SuppressWarnings("unchecked") - public CumulativeFunction readState(Object o) - { - List list = (List) o; - BinaryFunction func = (BinaryFunction) list.get(0); - CumulativeFunction cf = new CumulativeFunction(func); - cf.m_lastValue = (T) list.get(1); - return cf; - } - - /** - * @since 0.11 - * @return The last value - */ - /*@ pure @*/ public T getLastValue() - { - return m_lastValue; - } }