ReplaceWithAverage.java
Created with JBuilder |
package lrs.visitor;
import lrs.*;
/**
* Assumes each element in the host is an LRStruct of Double.
* Replaces each LRStruct in the host with the average of the numbers
* it contains. In the end, the host only contains Doubles representing the
* average of the original list.
* @author D. X. Nguyen
* @version 1.0
*/
public class ReplaceWithAverage implements IAlgo {
public static final ReplaceWithAverage Singleton = new ReplaceWithAverage();
private ReplaceWithAverage() {
}
public Object emptyCase(LRStruct host, Object inp) {
return host;
}
public Object nonEmptyCase(LRStruct host, Object inp) {
LRStruct lrs = (LRStruct)host.getFirst();
Object average = lrs.execute(new Average(), null);
host.setFirst(average);
return host.getRest().execute(this, null);
}
}
ReplaceWithAverage.java
Created with JBuilder |