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-02-26 10:44:10 +00:00
|
|
|
public class Average extends Function
|
|
|
|
{
|
2017-03-16 00:27:45 +00:00
|
|
|
private final boolean isRange; // may affect how the count is done
|
2017-02-26 10:44:10 +00:00
|
|
|
|
2017-03-18 08:33:40 +00:00
|
|
|
public Average (Cell cell, String text)
|
2017-02-26 10:44:10 +00:00
|
|
|
{
|
2017-03-18 08:33:40 +00:00
|
|
|
super (cell, text);
|
2017-03-03 23:41:08 +00:00
|
|
|
|
2017-03-18 09:21:11 +00:00
|
|
|
assert text.startsWith ("@AVERAGE(") : text;
|
|
|
|
|
2017-03-18 21:23:44 +00:00
|
|
|
list = new ValueList (cell, functionText);
|
2017-03-16 00:27:45 +00:00
|
|
|
isRange = functionText.indexOf ("...") > 0;
|
2017-03-18 21:23:44 +00:00
|
|
|
|
|
|
|
for (Value v : list)
|
|
|
|
values.add (v);
|
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
|
|
|
}
|
|
|
|
}
|