dmolony-DiskBrowser/src/com/bytezone/diskbrowser/visicalc/Average.java

48 lines
907 B
Java
Raw Normal View History

2017-02-26 10:44:10 +00:00
package com.bytezone.diskbrowser.visicalc;
2017-03-16 00:27:45 +00:00
import com.bytezone.diskbrowser.visicalc.Cell.CellType;
2017-03-19 23:33:23 +00:00
public class Average extends ValueListFunction
2017-02-26 10:44:10 +00:00
{
public Average (Cell cell, String text)
2017-02-26 10:44:10 +00:00
{
super (cell, text);
2017-03-18 09:21:11 +00:00
assert text.startsWith ("@AVERAGE(") : text;
2017-02-26 10:44:10 +00:00
}
@Override
2017-02-28 20:39:26 +00:00
public void calculate ()
2017-02-26 10:44:10 +00:00
{
double total = 0.0;
int totalChecked = 0;
2017-03-15 00:41:45 +00:00
for (Value v : list)
2017-02-26 10:44:10 +00:00
{
2017-03-16 00:27:45 +00:00
if (v instanceof Cell && ((Cell) v).isCellType (CellType.EMPTY))
continue;
2017-03-15 00:41:45 +00:00
v.calculate ();
2017-03-08 09:18:59 +00:00
2017-03-15 00:41:45 +00:00
if (v.isValueType (ValueType.NA))
2017-02-26 10:44:10 +00:00
continue;
2017-03-15 00:41:45 +00:00
if (!v.isValueType (ValueType.VALUE))
2017-02-26 10:44:10 +00:00
{
2017-03-15 00:41:45 +00:00
valueType = v.getValueType ();
2017-03-08 09:18:59 +00:00
return;
2017-02-26 10:44:10 +00:00
}
2017-03-15 00:41:45 +00:00
total += v.getValue ();
2017-02-26 10:44:10 +00:00
totalChecked++;
}
if (totalChecked == 0)
{
2017-03-08 09:18:59 +00:00
valueType = ValueType.ERROR;
return;
2017-02-26 10:44:10 +00:00
}
2017-03-08 09:18:59 +00:00
value = total / totalChecked;
valueType = ValueType.VALUE;
2017-02-26 10:44:10 +00:00
}
}