// file: Scaler.java // author: Robert M. Keller // purpose: Example of a Function1 object /** * A Scaler is a Function1 object that can be used to scale a list of * Numbers by a scale factor. If both the scale factor and the function * argument are Integer or Long, a Long will be returned. Otherwise a * Double will be returned. */ class Scaler implements Function1 { private Number factor; /** * Create a Scaler with a specific scale factor. * @param _factor the factor to be used */ Scaler(Number _factor) { factor = _factor; } /** * Apply this Scaler object to an Object x, which should be a Number. * If x is not a number, a ClassCastException will be thrown. * * @param x the Object to which this function is applied * @return a Double containing this Scaler's factor times the argument's value */ public Object apply(Object x) { return Multiplier.multiply(factor, x); } }