mirror of
https://github.com/dmolony/DiskBrowser.git
synced 2024-06-17 16:29:31 +00:00
changed Value interface
This commit is contained in:
parent
d037cc1d97
commit
a569a8aee7
|
@ -8,9 +8,10 @@ public class Abs extends Function
|
|||
}
|
||||
|
||||
@Override
|
||||
public void calculate ()
|
||||
public Value calculate ()
|
||||
{
|
||||
Expression exp = new Expression (parent, functionText);
|
||||
value = Math.abs (exp.getValue ());
|
||||
return this;
|
||||
}
|
||||
}
|
|
@ -9,8 +9,9 @@ class And extends Function
|
|||
}
|
||||
|
||||
@Override
|
||||
public void calculate ()
|
||||
public Value calculate ()
|
||||
{
|
||||
value = 0;
|
||||
return this;
|
||||
}
|
||||
}
|
|
@ -32,6 +32,7 @@ class Cell implements Comparable<Cell>, Value
|
|||
type = CellType.VALUE; // default to VALUE, formatting may change it
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isValue ()
|
||||
{
|
||||
return type == CellType.VALUE;
|
||||
|
@ -124,7 +125,7 @@ class Cell implements Comparable<Cell>, Value
|
|||
return justify (repeat, colWidth);
|
||||
|
||||
case VALUE:
|
||||
if (value.isError () || value.isNaN ())
|
||||
if (value.isError () || value.isNotAvailable ())
|
||||
return justify (value.getText (), colWidth);
|
||||
|
||||
Double thisValue = value.getValue ();
|
||||
|
@ -203,14 +204,14 @@ class Cell implements Comparable<Cell>, Value
|
|||
}
|
||||
|
||||
@Override
|
||||
public boolean isNaN ()
|
||||
public boolean isNotAvailable ()
|
||||
{
|
||||
assert type == CellType.VALUE;
|
||||
return value.isNaN ();
|
||||
return value.isNotAvailable ();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void calculate ()
|
||||
public Value calculate ()
|
||||
{
|
||||
assert type == CellType.VALUE;
|
||||
if (expressionText == null)
|
||||
|
@ -221,11 +222,12 @@ class Cell implements Comparable<Cell>, Value
|
|||
}
|
||||
else
|
||||
{
|
||||
// should use Number or Cell for simple Values
|
||||
// should use Number or Cell or Function for simple Values
|
||||
value = new Expression (parent, expressionText);
|
||||
value.calculate ();
|
||||
valueType = value.getValueType ();
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -11,13 +11,18 @@ class Count extends Function
|
|||
}
|
||||
|
||||
@Override
|
||||
public void calculate ()
|
||||
public Value calculate ()
|
||||
{
|
||||
value = 0;
|
||||
valueType = ValueType.VALUE;
|
||||
|
||||
for (Address address : range)
|
||||
{
|
||||
Cell cell = parent.getCell (address);
|
||||
if (cell == null || cell.isError () || cell.isNaN ())
|
||||
if (cell == null || cell.isNotAvailable ())
|
||||
continue;
|
||||
|
||||
if (cell.isError ())
|
||||
{
|
||||
valueType = ValueType.ERROR;
|
||||
break;
|
||||
|
@ -26,5 +31,6 @@ class Count extends Function
|
|||
if (cell.getValue () != 0.0)
|
||||
value++;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
}
|
|
@ -14,9 +14,9 @@ class Error extends Function
|
|||
}
|
||||
|
||||
@Override
|
||||
public boolean isNaN ()
|
||||
public boolean isNotAvailable ()
|
||||
{
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -26,7 +26,8 @@ class Error extends Function
|
|||
}
|
||||
|
||||
@Override
|
||||
public void calculate ()
|
||||
public Value calculate ()
|
||||
{
|
||||
return this;
|
||||
}
|
||||
}
|
|
@ -90,7 +90,9 @@ class Expression implements Value
|
|||
{
|
||||
String addressText = getAddressText (line.substring (ptr));
|
||||
ptr += addressText.length ();
|
||||
values.add (parent.getCell (addressText));
|
||||
Cell cell = parent.getCell (addressText);
|
||||
if (cell != null)
|
||||
values.add (parent.getCell (addressText));
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -117,18 +119,19 @@ class Expression implements Value
|
|||
}
|
||||
|
||||
@Override
|
||||
public void calculate ()
|
||||
public Value calculate ()
|
||||
{
|
||||
// System.out.println (this);
|
||||
try
|
||||
{
|
||||
Value thisValue = values.get (0);
|
||||
thisValue.calculate ();
|
||||
if (thisValue.isError ())
|
||||
{
|
||||
valueType = ValueType.ERROR;
|
||||
return;
|
||||
valueType = thisValue.getValueType ();
|
||||
return this;
|
||||
}
|
||||
value = thisValue.getValue ();
|
||||
value = thisValue.isNotAvailable () ? 0 : thisValue.getValue ();
|
||||
|
||||
String sign = signs.get (0);
|
||||
if (sign.equals ("(-)"))
|
||||
|
@ -140,10 +143,11 @@ class Expression implements Value
|
|||
thisValue.calculate ();
|
||||
if (thisValue.isError ())
|
||||
{
|
||||
valueType = ValueType.ERROR;
|
||||
return;
|
||||
valueType = thisValue.getValueType ();
|
||||
return this;
|
||||
}
|
||||
double nextValue = thisValue.getValue ();
|
||||
|
||||
double nextValue = thisValue.isNotAvailable () ? 0 : thisValue.getValue ();
|
||||
|
||||
sign = signs.get (i);
|
||||
if (sign.equals ("(-)"))
|
||||
|
@ -167,6 +171,7 @@ class Expression implements Value
|
|||
{
|
||||
valueType = ValueType.ERROR;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -176,15 +181,15 @@ class Expression implements Value
|
|||
}
|
||||
|
||||
@Override
|
||||
public boolean isNaN ()
|
||||
public boolean isValue ()
|
||||
{
|
||||
return Double.isNaN (value);
|
||||
return valueType == ValueType.VALUE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getText ()
|
||||
public boolean isNotAvailable ()
|
||||
{
|
||||
return isNaN () ? "NaN" : isError () ? "Error" : "";
|
||||
return valueType == ValueType.NA;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -200,6 +205,12 @@ class Expression implements Value
|
|||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getText ()
|
||||
{
|
||||
return isNotAvailable () ? "NA" : isError () ? "Error" : "";
|
||||
}
|
||||
|
||||
private String checkBrackets (String input)
|
||||
{
|
||||
String line = input.trim ();
|
||||
|
@ -220,7 +231,7 @@ class Expression implements Value
|
|||
System.out.printf ("**** Unbalanced brackets: left:%d, right:%d ****%n",
|
||||
leftBracket, rightBracket);
|
||||
System.out.println (input);
|
||||
return "@ERROR()";
|
||||
return "@ERROR";
|
||||
}
|
||||
// System.out.printf ("Old expression:[%s]%n", line);
|
||||
while (rightBracket < leftBracket)
|
||||
|
@ -286,11 +297,18 @@ class Expression implements Value
|
|||
int ptr = 0;
|
||||
for (Value value : values)
|
||||
{
|
||||
assert value != null;
|
||||
text.append (signs.get (ptr));
|
||||
text.append (value.getValue ());
|
||||
// value.calculate ();
|
||||
// if (value.isValue ())
|
||||
// text.append (value.getValue ());
|
||||
if (ptr < operators.size ())
|
||||
{
|
||||
// System.out.println (operators.get (ptr));
|
||||
text.append (operators.get (ptr++));
|
||||
}
|
||||
}
|
||||
// System.out.println ("finished building");
|
||||
|
||||
return text.toString ();
|
||||
}
|
||||
|
|
|
@ -31,6 +31,7 @@ abstract class Function implements Value
|
|||
private static final Pattern addressList = Pattern.compile ("\\(([^,]+(,[^,]+)*)\\)");
|
||||
|
||||
protected final Sheet parent;
|
||||
protected String functionName;
|
||||
protected String functionText;
|
||||
|
||||
protected ValueType valueType;
|
||||
|
@ -97,9 +98,15 @@ abstract class Function implements Value
|
|||
// get function's parameter string
|
||||
int pos = text.indexOf ('(');
|
||||
if (pos >= 0)
|
||||
{
|
||||
functionName = text.substring (0, pos);
|
||||
functionText = text.substring (pos + 1, text.length () - 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
functionName = "";
|
||||
functionText = "";
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -108,6 +115,12 @@ abstract class Function implements Value
|
|||
return valueType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isValue ()
|
||||
{
|
||||
return valueType == ValueType.VALUE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isError ()
|
||||
{
|
||||
|
@ -115,9 +128,9 @@ abstract class Function implements Value
|
|||
}
|
||||
|
||||
@Override
|
||||
public boolean isNaN ()
|
||||
public boolean isNotAvailable ()
|
||||
{
|
||||
return Double.isNaN (value);
|
||||
return valueType == ValueType.NA;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -130,7 +143,7 @@ abstract class Function implements Value
|
|||
@Override
|
||||
public String getText ()
|
||||
{
|
||||
return isNaN () ? "NaN" : isError () ? "Error" : "";
|
||||
return isNotAvailable () ? "" : isError () ? "Error" : "";
|
||||
}
|
||||
|
||||
protected Range getRange (String text)
|
||||
|
@ -176,6 +189,6 @@ abstract class Function implements Value
|
|||
@Override
|
||||
public String toString ()
|
||||
{
|
||||
return String.format ("Function: %s", functionText);
|
||||
return String.format ("Function: %s %s", functionName, functionText);
|
||||
}
|
||||
}
|
|
@ -23,7 +23,7 @@ class If extends Function
|
|||
}
|
||||
|
||||
@Override
|
||||
public void calculate ()
|
||||
public Value calculate ()
|
||||
{
|
||||
valueType = ValueType.VALUE;
|
||||
|
||||
|
@ -34,7 +34,7 @@ class If extends Function
|
|||
expTrue = new Expression (parent, textTrue);
|
||||
expTrue.calculate ();
|
||||
}
|
||||
if (expTrue.isError () || expTrue.isNaN ())
|
||||
if (expTrue.isError () || expTrue.isNotAvailable ())
|
||||
valueType = expTrue.getValueType ();
|
||||
else
|
||||
value = expTrue.getValue ();
|
||||
|
@ -46,11 +46,12 @@ class If extends Function
|
|||
expFalse = new Expression (parent, textFalse);
|
||||
expFalse.calculate ();
|
||||
}
|
||||
if (expFalse.isError () || expFalse.isNaN ())
|
||||
if (expFalse.isError () || expFalse.isNotAvailable ())
|
||||
valueType = expFalse.getValueType ();
|
||||
else
|
||||
value = expFalse.getValue ();
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -9,9 +9,10 @@ public class Int extends Function
|
|||
}
|
||||
|
||||
@Override
|
||||
public void calculate ()
|
||||
public Value calculate ()
|
||||
{
|
||||
Expression exp = new Expression (parent, functionText);
|
||||
value = (int) exp.getValue ();
|
||||
return this;
|
||||
}
|
||||
}
|
|
@ -11,11 +11,12 @@ class IsError extends Function
|
|||
}
|
||||
|
||||
@Override
|
||||
public void calculate ()
|
||||
public Value calculate ()
|
||||
{
|
||||
expression.calculate ();
|
||||
// value = expression.getValue ();
|
||||
valueType = expression.getValueType ();
|
||||
value = isError () ? 1 : 0;
|
||||
return this;
|
||||
}
|
||||
}
|
|
@ -11,9 +11,10 @@ public class IsNa extends Function
|
|||
}
|
||||
|
||||
@Override
|
||||
public void calculate ()
|
||||
public Value calculate ()
|
||||
{
|
||||
value = expression.getValue ();
|
||||
valueType = expression.getValueType ();
|
||||
return this;
|
||||
}
|
||||
}
|
|
@ -20,13 +20,13 @@ class Lookup extends Function
|
|||
}
|
||||
|
||||
@Override
|
||||
public void calculate ()
|
||||
public Value calculate ()
|
||||
{
|
||||
source.calculate ();
|
||||
if (source.isError () || source.isNaN ())
|
||||
if (source.isError () || source.isNotAvailable ())
|
||||
{
|
||||
valueType = source.getValueType ();
|
||||
return;
|
||||
return this;
|
||||
}
|
||||
double sourceValue = source.getValue ();
|
||||
|
||||
|
@ -44,5 +44,7 @@ class Lookup extends Function
|
|||
value = parent.getCell (target.nextColumn ()).getValue ();
|
||||
else
|
||||
value = parent.getCell (target.nextRow ()).getValue ();
|
||||
|
||||
return this;
|
||||
}
|
||||
}
|
|
@ -11,7 +11,7 @@ class Max extends Function
|
|||
}
|
||||
|
||||
@Override
|
||||
public void calculate ()
|
||||
public Value calculate ()
|
||||
{
|
||||
value = Double.MIN_VALUE;
|
||||
int totalChecked = 0;
|
||||
|
@ -19,13 +19,13 @@ class Max extends Function
|
|||
for (Address address : range)
|
||||
{
|
||||
Cell cell = parent.getCell (address);
|
||||
if (cell == null)
|
||||
if (cell == null || cell.isNotAvailable ())
|
||||
continue;
|
||||
|
||||
if (cell.isError () || cell.isNaN ())
|
||||
if (cell.isError ())
|
||||
{
|
||||
valueType = ValueType.ERROR;
|
||||
return;
|
||||
break;
|
||||
}
|
||||
|
||||
double temp = cell.getValue ();
|
||||
|
@ -35,8 +35,10 @@ class Max extends Function
|
|||
}
|
||||
|
||||
if (totalChecked == 0)
|
||||
valueType = ValueType.ERROR;
|
||||
valueType = ValueType.NA;
|
||||
else
|
||||
valueType = ValueType.VALUE;
|
||||
|
||||
return this;
|
||||
}
|
||||
}
|
|
@ -11,7 +11,7 @@ class Min extends Function
|
|||
}
|
||||
|
||||
@Override
|
||||
public void calculate ()
|
||||
public Value calculate ()
|
||||
{
|
||||
value = Double.MAX_VALUE;
|
||||
int totalChecked = 0;
|
||||
|
@ -19,13 +19,13 @@ class Min extends Function
|
|||
for (Address address : range)
|
||||
{
|
||||
Cell cell = parent.getCell (address);
|
||||
if (cell == null)
|
||||
if (cell == null || cell.isNotAvailable ())
|
||||
continue;
|
||||
|
||||
if (cell.isError () || cell.isNaN ())
|
||||
if (cell.isError ())
|
||||
{
|
||||
valueType = ValueType.ERROR;
|
||||
return;
|
||||
break;
|
||||
}
|
||||
|
||||
double temp = cell.getValue ();
|
||||
|
@ -35,8 +35,10 @@ class Min extends Function
|
|||
}
|
||||
|
||||
if (totalChecked == 0)
|
||||
valueType = ValueType.ERROR;
|
||||
valueType = ValueType.NA;
|
||||
else
|
||||
valueType = ValueType.VALUE;
|
||||
|
||||
return this;
|
||||
}
|
||||
}
|
|
@ -14,7 +14,7 @@ public class Na extends Function
|
|||
}
|
||||
|
||||
@Override
|
||||
public boolean isNaN ()
|
||||
public boolean isNotAvailable ()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
@ -26,7 +26,8 @@ public class Na extends Function
|
|||
}
|
||||
|
||||
@Override
|
||||
public void calculate ()
|
||||
public Value calculate ()
|
||||
{
|
||||
return this;
|
||||
}
|
||||
}
|
|
@ -21,7 +21,7 @@ public class Npv extends Function
|
|||
}
|
||||
|
||||
@Override
|
||||
public void calculate ()
|
||||
public Value calculate ()
|
||||
{
|
||||
value = 0;
|
||||
valueType = ValueType.VALUE;
|
||||
|
@ -29,16 +29,18 @@ public class Npv extends Function
|
|||
for (Address address : range)
|
||||
{
|
||||
Cell cell = parent.getCell (address);
|
||||
if (cell == null)
|
||||
if (cell == null || cell.isNotAvailable ())
|
||||
continue;
|
||||
|
||||
if (cell.isError () || cell.isNaN ())
|
||||
if (cell.isError ())
|
||||
{
|
||||
valueType = ValueType.VALUE;
|
||||
return;
|
||||
valueType = ValueType.ERROR;
|
||||
break;
|
||||
}
|
||||
|
||||
double temp = cell.getValue ();
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
}
|
|
@ -10,6 +10,7 @@ class Number implements Value
|
|||
try
|
||||
{
|
||||
value = Double.parseDouble (text);
|
||||
valueType = ValueType.VALUE;
|
||||
}
|
||||
catch (NumberFormatException e)
|
||||
{
|
||||
|
@ -17,6 +18,12 @@ class Number implements Value
|
|||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isValue ()
|
||||
{
|
||||
return valueType == ValueType.VALUE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isError ()
|
||||
{
|
||||
|
@ -24,9 +31,9 @@ class Number implements Value
|
|||
}
|
||||
|
||||
@Override
|
||||
public boolean isNaN ()
|
||||
public boolean isNotAvailable ()
|
||||
{
|
||||
return Double.isNaN (value);
|
||||
return valueType == ValueType.NA;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -44,17 +51,18 @@ class Number implements Value
|
|||
@Override
|
||||
public String getText ()
|
||||
{
|
||||
return null;
|
||||
return valueType == ValueType.ERROR ? "Error" : "";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void calculate ()
|
||||
public Value calculate ()
|
||||
{
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ValueType getValueType ()
|
||||
{
|
||||
return null;
|
||||
return valueType;
|
||||
}
|
||||
}
|
|
@ -9,8 +9,9 @@ class Or extends Function
|
|||
}
|
||||
|
||||
@Override
|
||||
public void calculate ()
|
||||
public Value calculate ()
|
||||
{
|
||||
value = 0;
|
||||
return this;
|
||||
}
|
||||
}
|
|
@ -8,8 +8,9 @@ class Pi extends Function
|
|||
}
|
||||
|
||||
@Override
|
||||
public void calculate ()
|
||||
public Value calculate ()
|
||||
{
|
||||
value = 3.1415926536;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,7 +11,7 @@ class Sum extends Function
|
|||
}
|
||||
|
||||
@Override
|
||||
public void calculate ()
|
||||
public Value calculate ()
|
||||
{
|
||||
value = 0;
|
||||
valueType = ValueType.VALUE;
|
||||
|
@ -19,15 +19,18 @@ class Sum extends Function
|
|||
for (Address address : range)
|
||||
{
|
||||
Cell cell = parent.getCell (address);
|
||||
if (cell == null)
|
||||
if (cell == null || cell.isNotAvailable ())
|
||||
continue;
|
||||
|
||||
if (cell.isError () || cell.isNaN ())
|
||||
if (cell.isError ())
|
||||
{
|
||||
valueType = ValueType.ERROR;
|
||||
return;
|
||||
break;
|
||||
}
|
||||
|
||||
value += cell.getValue ();
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
}
|
|
@ -13,9 +13,11 @@ interface Value
|
|||
|
||||
public String getText ();
|
||||
|
||||
public boolean isValue ();
|
||||
|
||||
public boolean isError ();
|
||||
|
||||
public boolean isNaN ();
|
||||
public boolean isNotAvailable ();
|
||||
|
||||
public void calculate ();
|
||||
public Value calculate ();
|
||||
}
|
Loading…
Reference in New Issue
Block a user