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

55 lines
1.4 KiB
Java
Raw Permalink 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;
2020-02-10 11:05:40 +00:00
// -----------------------------------------------------------------------------------//
class Average extends ValueListFunction
// -----------------------------------------------------------------------------------//
2017-02-26 10:44:10 +00:00
{
2020-02-10 11:05:40 +00:00
// ---------------------------------------------------------------------------------//
Average (Cell cell, String text)
// ---------------------------------------------------------------------------------//
2017-02-26 10:44:10 +00:00
{
super (cell, text);
2017-03-23 13:30:41 +00:00
2017-03-18 09:21:11 +00:00
assert text.startsWith ("@AVERAGE(") : text;
2017-02-26 10:44:10 +00:00
}
2020-02-10 11:05:40 +00:00
// ---------------------------------------------------------------------------------//
2017-02-26 10:44:10 +00:00
@Override
2017-02-28 20:39:26 +00:00
public void calculate ()
2020-02-10 11:05:40 +00:00
// ---------------------------------------------------------------------------------//
2017-02-26 10:44:10 +00:00
{
double total = 0.0;
int totalChecked = 0;
2017-03-23 13:30:41 +00:00
valueResult = ValueResult.VALID;
2017-02-26 10:44:10 +00:00
2017-03-15 00:41:45 +00:00
for (Value v : list)
2017-02-26 10:44:10 +00:00
{
2021-09-19 07:09:34 +00:00
if (v instanceof Cell cell && cell.isCellType (CellType.EMPTY))
2017-03-16 00:27:45 +00:00
continue;
2017-03-15 00:41:45 +00:00
v.calculate ();
2017-03-08 09:18:59 +00:00
2017-03-23 13:30:41 +00:00
if (v.getValueResult () == ValueResult.NA)
2017-02-26 10:44:10 +00:00
continue;
2017-03-23 13:30:41 +00:00
if (!v.isValid ())
2017-02-26 10:44:10 +00:00
{
2017-03-23 13:30:41 +00:00
valueResult = v.getValueResult ();
2017-03-08 09:18:59 +00:00
return;
2017-02-26 10:44:10 +00:00
}
2017-03-23 13:30:41 +00:00
total += v.getDouble ();
2017-02-26 10:44:10 +00:00
totalChecked++;
}
if (totalChecked == 0)
{
2017-03-23 13:30:41 +00:00
valueResult = ValueResult.ERROR;
2017-03-08 09:18:59 +00:00
return;
2017-02-26 10:44:10 +00:00
}
2017-03-08 09:18:59 +00:00
value = total / totalChecked;
2017-02-26 10:44:10 +00:00
}
}