mirror of
https://github.com/dmolony/DiskBrowser.git
synced 2024-06-27 08:29:31 +00:00
call calculate()
This commit is contained in:
parent
b87609c9d0
commit
d037cc1d97
|
@ -18,6 +18,7 @@ class Cell implements Comparable<Cell>, Value
|
|||
|
||||
private String expressionText;
|
||||
private Value value;
|
||||
private ValueType valueType;
|
||||
|
||||
enum CellType
|
||||
{
|
||||
|
@ -181,6 +182,12 @@ class Cell implements Comparable<Cell>, Value
|
|||
return value.getValue ();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ValueType getValueType ()
|
||||
{
|
||||
return valueType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getText ()
|
||||
{
|
||||
|
@ -210,12 +217,15 @@ class Cell implements Comparable<Cell>, Value
|
|||
{
|
||||
System.out.printf ("%s null expression text %n", address);
|
||||
value = Function.getInstance (parent, "@ERROR");
|
||||
valueType = ValueType.ERROR;
|
||||
}
|
||||
else
|
||||
{
|
||||
// should use Number or Cell for simple Values
|
||||
value = new Expression (parent, expressionText);
|
||||
|
||||
value.calculate ();
|
||||
valueType = value.getValueType ();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -49,8 +49,14 @@ class Condition
|
|||
{
|
||||
conditionExpression = new Expression (parent, conditionText);
|
||||
valueExpression = new Expression (parent, valueText);
|
||||
|
||||
conditionExpression.calculate ();
|
||||
valueExpression.calculate ();
|
||||
}
|
||||
|
||||
if (conditionExpression.isError () || valueExpression.isError ())
|
||||
return false;
|
||||
|
||||
double conditionResult = conditionExpression.getValue ();
|
||||
double valueResult = valueExpression.getValue ();
|
||||
|
||||
|
|
|
@ -19,7 +19,7 @@ class Count extends Function
|
|||
Cell cell = parent.getCell (address);
|
||||
if (cell == null || cell.isError () || cell.isNaN ())
|
||||
{
|
||||
isError = true;
|
||||
valueType = ValueType.ERROR;
|
||||
break;
|
||||
}
|
||||
|
||||
|
|
|
@ -32,7 +32,8 @@ class Expression implements Value
|
|||
private final List<String> operators = new ArrayList<String> ();
|
||||
private final List<String> signs = new ArrayList<String> ();
|
||||
|
||||
protected boolean isError;
|
||||
// protected boolean isError;
|
||||
private ValueType valueType;
|
||||
private double value;
|
||||
|
||||
public Expression (Sheet parent, String text)
|
||||
|
@ -117,9 +118,17 @@ class Expression implements Value
|
|||
|
||||
@Override
|
||||
public void calculate ()
|
||||
{
|
||||
try
|
||||
{
|
||||
Value thisValue = values.get (0);
|
||||
value = thisValue == null ? 0 : values.get (0).getValue ();
|
||||
thisValue.calculate ();
|
||||
if (thisValue.isError ())
|
||||
{
|
||||
valueType = ValueType.ERROR;
|
||||
return;
|
||||
}
|
||||
value = thisValue.getValue ();
|
||||
|
||||
String sign = signs.get (0);
|
||||
if (sign.equals ("(-)"))
|
||||
|
@ -128,7 +137,13 @@ class Expression implements Value
|
|||
for (int i = 1; i < values.size (); i++)
|
||||
{
|
||||
thisValue = values.get (i);
|
||||
double nextValue = thisValue == null ? 0 : thisValue.getValue ();
|
||||
thisValue.calculate ();
|
||||
if (thisValue.isError ())
|
||||
{
|
||||
valueType = ValueType.ERROR;
|
||||
return;
|
||||
}
|
||||
double nextValue = thisValue.getValue ();
|
||||
|
||||
sign = signs.get (i);
|
||||
if (sign.equals ("(-)"))
|
||||
|
@ -146,6 +161,18 @@ class Expression implements Value
|
|||
else if (operator.equals ("^"))
|
||||
value = Math.pow (value, nextValue);
|
||||
}
|
||||
valueType = ValueType.VALUE;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
valueType = ValueType.ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public ValueType getValueType ()
|
||||
{
|
||||
return valueType;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -157,22 +184,19 @@ class Expression implements Value
|
|||
@Override
|
||||
public String getText ()
|
||||
{
|
||||
if (isNaN ())
|
||||
return "NaN";
|
||||
if (isError ())
|
||||
return "Error";
|
||||
return "";
|
||||
return isNaN () ? "NaN" : isError () ? "Error" : "";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isError ()
|
||||
{
|
||||
return isError;
|
||||
return valueType == ValueType.ERROR;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getValue ()
|
||||
{
|
||||
assert valueType == ValueType.VALUE : "Expression ValueType = " + valueType;
|
||||
return value;
|
||||
}
|
||||
|
||||
|
|
|
@ -33,7 +33,7 @@ abstract class Function implements Value
|
|||
protected final Sheet parent;
|
||||
protected String functionText;
|
||||
|
||||
protected boolean isError;
|
||||
protected ValueType valueType;
|
||||
protected double value;
|
||||
|
||||
static Function getInstance (Sheet parent, String text)
|
||||
|
@ -102,10 +102,16 @@ abstract class Function implements Value
|
|||
functionText = "";
|
||||
}
|
||||
|
||||
@Override
|
||||
public ValueType getValueType ()
|
||||
{
|
||||
return valueType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isError ()
|
||||
{
|
||||
return isError;
|
||||
return valueType == ValueType.ERROR;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -117,17 +123,14 @@ abstract class Function implements Value
|
|||
@Override
|
||||
public double getValue ()
|
||||
{
|
||||
assert valueType == ValueType.VALUE : "Function ValueType = " + valueType;
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getText ()
|
||||
{
|
||||
if (isNaN ())
|
||||
return "NaN";
|
||||
if (isError ())
|
||||
return "Error";
|
||||
return "";
|
||||
return isNaN () ? "NaN" : isError () ? "Error" : "";
|
||||
}
|
||||
|
||||
protected Range getRange (String text)
|
||||
|
|
|
@ -25,16 +25,30 @@ class If extends Function
|
|||
@Override
|
||||
public void calculate ()
|
||||
{
|
||||
valueType = ValueType.VALUE;
|
||||
|
||||
if (condition.getResult ())
|
||||
{
|
||||
if (expTrue == null)
|
||||
{
|
||||
expTrue = new Expression (parent, textTrue);
|
||||
expTrue.calculate ();
|
||||
}
|
||||
if (expTrue.isError () || expTrue.isNaN ())
|
||||
valueType = expTrue.getValueType ();
|
||||
else
|
||||
value = expTrue.getValue ();
|
||||
}
|
||||
else
|
||||
{
|
||||
if (expFalse == null)
|
||||
{
|
||||
expFalse = new Expression (parent, textFalse);
|
||||
expFalse.calculate ();
|
||||
}
|
||||
if (expFalse.isError () || expFalse.isNaN ())
|
||||
valueType = expFalse.getValueType ();
|
||||
else
|
||||
value = expFalse.getValue ();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,7 +13,9 @@ class IsError extends Function
|
|||
@Override
|
||||
public void calculate ()
|
||||
{
|
||||
value = expression.getValue ();
|
||||
isError = expression.isError ();
|
||||
expression.calculate ();
|
||||
// value = expression.getValue ();
|
||||
valueType = expression.getValueType ();
|
||||
value = isError () ? 1 : 0;
|
||||
}
|
||||
}
|
|
@ -14,6 +14,6 @@ public class IsNa extends Function
|
|||
public void calculate ()
|
||||
{
|
||||
value = expression.getValue ();
|
||||
isError = expression.isError ();
|
||||
valueType = expression.getValueType ();
|
||||
}
|
||||
}
|
|
@ -3,7 +3,6 @@ package com.bytezone.diskbrowser.visicalc;
|
|||
class Lookup extends Function
|
||||
{
|
||||
Range range;
|
||||
// boolean hasValue;
|
||||
String sourceText;
|
||||
String rangeText;
|
||||
Expression source;
|
||||
|
@ -20,15 +19,15 @@ class Lookup extends Function
|
|||
range = getRange (rangeText);
|
||||
}
|
||||
|
||||
// need a mechanism to return NA and ERROR
|
||||
// @Override
|
||||
// public boolean hasValue ()
|
||||
// {
|
||||
// return hasValue;
|
||||
// }
|
||||
|
||||
@Override
|
||||
public void calculate ()
|
||||
{
|
||||
source.calculate ();
|
||||
if (source.isError () || source.isNaN ())
|
||||
{
|
||||
valueType = source.getValueType ();
|
||||
return;
|
||||
}
|
||||
double sourceValue = source.getValue ();
|
||||
|
||||
Address target = null;
|
||||
|
@ -45,7 +44,5 @@ class Lookup extends Function
|
|||
value = parent.getCell (target.nextColumn ()).getValue ();
|
||||
else
|
||||
value = parent.getCell (target.nextRow ()).getValue ();
|
||||
|
||||
// return 0;
|
||||
}
|
||||
}
|
|
@ -14,7 +14,6 @@ class Max extends Function
|
|||
public void calculate ()
|
||||
{
|
||||
value = Double.MIN_VALUE;
|
||||
isError = false;
|
||||
int totalChecked = 0;
|
||||
|
||||
for (Address address : range)
|
||||
|
@ -25,8 +24,8 @@ class Max extends Function
|
|||
|
||||
if (cell.isError () || cell.isNaN ())
|
||||
{
|
||||
isError = true;
|
||||
break;
|
||||
valueType = ValueType.ERROR;
|
||||
return;
|
||||
}
|
||||
|
||||
double temp = cell.getValue ();
|
||||
|
@ -36,6 +35,8 @@ class Max extends Function
|
|||
}
|
||||
|
||||
if (totalChecked == 0)
|
||||
isError = true;
|
||||
valueType = ValueType.ERROR;
|
||||
else
|
||||
valueType = ValueType.VALUE;
|
||||
}
|
||||
}
|
|
@ -14,7 +14,6 @@ class Min extends Function
|
|||
public void calculate ()
|
||||
{
|
||||
value = Double.MAX_VALUE;
|
||||
isError = false;
|
||||
int totalChecked = 0;
|
||||
|
||||
for (Address address : range)
|
||||
|
@ -25,8 +24,8 @@ class Min extends Function
|
|||
|
||||
if (cell.isError () || cell.isNaN ())
|
||||
{
|
||||
isError = true;
|
||||
break;
|
||||
valueType = ValueType.ERROR;
|
||||
return;
|
||||
}
|
||||
|
||||
double temp = cell.getValue ();
|
||||
|
@ -36,6 +35,8 @@ class Min extends Function
|
|||
}
|
||||
|
||||
if (totalChecked == 0)
|
||||
isError = true;
|
||||
valueType = ValueType.ERROR;
|
||||
else
|
||||
valueType = ValueType.VALUE;
|
||||
}
|
||||
}
|
|
@ -24,6 +24,7 @@ public class Npv extends Function
|
|||
public void calculate ()
|
||||
{
|
||||
value = 0;
|
||||
valueType = ValueType.VALUE;
|
||||
|
||||
for (Address address : range)
|
||||
{
|
||||
|
@ -33,8 +34,8 @@ public class Npv extends Function
|
|||
|
||||
if (cell.isError () || cell.isNaN ())
|
||||
{
|
||||
isError = true;
|
||||
break;
|
||||
valueType = ValueType.VALUE;
|
||||
return;
|
||||
}
|
||||
|
||||
double temp = cell.getValue ();
|
||||
|
|
|
@ -2,8 +2,8 @@ package com.bytezone.diskbrowser.visicalc;
|
|||
|
||||
class Number implements Value
|
||||
{
|
||||
double value;
|
||||
boolean isError;
|
||||
private double value;
|
||||
private ValueType valueType;
|
||||
|
||||
public Number (String text)
|
||||
{
|
||||
|
@ -13,14 +13,14 @@ class Number implements Value
|
|||
}
|
||||
catch (NumberFormatException e)
|
||||
{
|
||||
isError = true;
|
||||
valueType = ValueType.ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isError ()
|
||||
{
|
||||
return isError;
|
||||
return valueType == ValueType.ERROR;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -51,4 +51,10 @@ class Number implements Value
|
|||
public void calculate ()
|
||||
{
|
||||
}
|
||||
|
||||
@Override
|
||||
public ValueType getValueType ()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
|
@ -14,6 +14,8 @@ class Sum extends Function
|
|||
public void calculate ()
|
||||
{
|
||||
value = 0;
|
||||
valueType = ValueType.VALUE;
|
||||
|
||||
for (Address address : range)
|
||||
{
|
||||
Cell cell = parent.getCell (address);
|
||||
|
@ -22,8 +24,8 @@ class Sum extends Function
|
|||
|
||||
if (cell.isError () || cell.isNaN ())
|
||||
{
|
||||
isError = true;
|
||||
break;
|
||||
valueType = ValueType.ERROR;
|
||||
return;
|
||||
}
|
||||
value += cell.getValue ();
|
||||
}
|
||||
|
|
|
@ -2,7 +2,12 @@ package com.bytezone.diskbrowser.visicalc;
|
|||
|
||||
interface Value
|
||||
{
|
||||
// public boolean hasValue ();
|
||||
enum ValueType
|
||||
{
|
||||
VALUE, ERROR, NA
|
||||
}
|
||||
|
||||
public ValueType getValueType ();
|
||||
|
||||
public double getValue ();
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user