implemented general purpose is()

This commit is contained in:
Denis Molony 2016-08-01 11:22:34 +10:00
parent 732e883e3b
commit 82d8201180
16 changed files with 208 additions and 147 deletions

View File

@ -8,7 +8,7 @@ class Cell implements Comparable<Cell>, Value
final Address address; final Address address;
private final Sheet parent; private final Sheet parent;
private CellType type; private CellType cellType;
private char cellFormat = ' '; private char cellFormat = ' ';
private char repeatingChar; private char repeatingChar;
@ -29,13 +29,9 @@ class Cell implements Comparable<Cell>, Value
{ {
this.parent = parent; this.parent = parent;
this.address = address; this.address = address;
type = CellType.VALUE; // default to VALUE, formatting may change it
}
@Override cellType = CellType.VALUE; // default to VALUE, formatting may change it
public boolean isValue () valueType = ValueType.VALUE;
{
return type == CellType.VALUE;
} }
void format (String format) void format (String format)
@ -55,7 +51,7 @@ class Cell implements Comparable<Cell>, Value
repeatingChar = format.charAt (2); repeatingChar = format.charAt (2);
for (int i = 0; i < 20; i++) for (int i = 0; i < 20; i++)
repeat += repeatingChar; repeat += repeatingChar;
type = CellType.REPEATING_CHARACTER; cellType = CellType.REPEATING_CHARACTER;
} }
else else
System.out.printf ("Unexpected format [%s]%n", format); System.out.printf ("Unexpected format [%s]%n", format);
@ -66,12 +62,12 @@ class Cell implements Comparable<Cell>, Value
if (command.charAt (0) == '"') if (command.charAt (0) == '"')
{ {
label = command.substring (1); label = command.substring (1);
type = CellType.LABEL; cellType = CellType.LABEL;
} }
else else
{ {
expressionText = command; expressionText = command;
type = CellType.VALUE; cellType = CellType.VALUE;
} }
// FUTURE.VC // FUTURE.VC
@ -114,7 +110,7 @@ class Cell implements Comparable<Cell>, Value
{ {
char format = cellFormat != ' ' ? cellFormat : defaultFormat; char format = cellFormat != ' ' ? cellFormat : defaultFormat;
switch (type) switch (cellType)
{ {
case LABEL: case LABEL:
return justify (label, colWidth, cellFormat); return justify (label, colWidth, cellFormat);
@ -123,7 +119,8 @@ class Cell implements Comparable<Cell>, Value
return justify (repeat, colWidth, format); return justify (repeat, colWidth, format);
case VALUE: case VALUE:
if (value.isError () || value.isNotAvailable () || value.isNotANumber ()) if (value.is (ValueType.ERROR) || value.is (ValueType.NA)
|| value.is (ValueType.NAN))
return justify (value.getText (), colWidth, format); return justify (value.getText (), colWidth, format);
Double thisValue = value.getValue (); Double thisValue = value.getValue ();
@ -178,7 +175,7 @@ class Cell implements Comparable<Cell>, Value
@Override @Override
public double getValue () public double getValue ()
{ {
assert type == CellType.VALUE; assert cellType == CellType.VALUE;
return value.getValue (); return value.getValue ();
} }
@ -191,44 +188,56 @@ class Cell implements Comparable<Cell>, Value
@Override @Override
public String getText () public String getText ()
{ {
assert isValue () : "Cell type: " + type; assert is (CellType.VALUE) : "Cell type: " + cellType;
return value.getText (); return value.getText ();
} }
@Override // @Override
public boolean isError () // public boolean isValue ()
{ // {
// assert isValue () : "Cell type: " + type; // return type == CellType.VALUE;
return value.isError (); // }
} //
// @Override
// public boolean isError ()
// {
// return value.isError ();
// }
//
// @Override
// public boolean isNotAvailable ()
// {
// if (!isValue ())
// return true;
// return value.isNotAvailable ();
// }
//
// @Override
// public boolean isNotANumber ()
// {
// return value.isNotANumber ();
// }
@Override @Override
public boolean isNotAvailable () public boolean is (ValueType type)
{ {
// assert type == CellType.VALUE : "Cell type: " + type; return valueType == type;
if (!isValue ())
return true;
return value.isNotAvailable ();
} }
@Override public boolean is (CellType type)
public boolean isNotANumber ()
{ {
// assert type == CellType.VALUE : "Cell type: " + type; return cellType == type;
// if (!isValue ())
// return true;
return value.isNotANumber ();
} }
@Override @Override
public Value calculate () public Value calculate ()
{ {
if (!isValue ()) if (!is (CellType.VALUE))
{ {
// System.out.println (value); // System.out.println (value);
return this; return this;
} }
assert isValue () : "Cell type: " + type + " @ " + address; assert is (CellType.VALUE) : "Cell type: " + cellType + " @ " + address;
if (expressionText == null) if (expressionText == null)
{ {
System.out.printf ("%s null expression text %n", address); System.out.printf ("%s null expression text %n", address);
@ -250,7 +259,7 @@ class Cell implements Comparable<Cell>, Value
{ {
String contents = ""; String contents = "";
switch (type) switch (cellType)
{ {
case LABEL: case LABEL:
contents = "Labl: " + label; contents = "Labl: " + label;

View File

@ -1,5 +1,7 @@
package com.bytezone.diskbrowser.visicalc; package com.bytezone.diskbrowser.visicalc;
import com.bytezone.diskbrowser.visicalc.Value.ValueType;
// Predicate // Predicate
class Condition class Condition
{ {
@ -54,7 +56,7 @@ class Condition
valueExpression.calculate (); valueExpression.calculate ();
} }
if (conditionExpression.isError () || valueExpression.isError ()) if (conditionExpression.is (ValueType.ERROR) || valueExpression.is (ValueType.ERROR))
return false; return false;
double conditionResult = conditionExpression.getValue (); double conditionResult = conditionExpression.getValue ();

View File

@ -16,10 +16,10 @@ class Count extends RangeFunction
for (Address address : range) for (Address address : range)
{ {
Cell cell = parent.getCell (address); Cell cell = parent.getCell (address);
if (cell == null || cell.isNotAvailable ()) if (cell == null || cell.is (ValueType.NA))
continue; continue;
if (cell.isError ()) if (cell.is (ValueType.ERROR))
{ {
valueType = ValueType.ERROR; valueType = ValueType.ERROR;
break; break;

View File

@ -7,18 +7,6 @@ class Error extends Function
super (parent, text); super (parent, text);
} }
@Override
public boolean isError ()
{
return true;
}
@Override
public boolean isNotAvailable ()
{
return false;
}
@Override @Override
public double getValue () public double getValue ()
{ {

View File

@ -127,12 +127,13 @@ class Expression implements Value
{ {
Value thisValue = values.get (0); Value thisValue = values.get (0);
thisValue.calculate (); thisValue.calculate ();
if (thisValue.isError ()) if (thisValue.is (ValueType.ERROR))
{ {
valueType = thisValue.getValueType (); valueType = thisValue.getValueType ();
return this; return this;
} }
value = thisValue.isNotAvailable () ? 0 : thisValue.getValue ();
value = thisValue.is (ValueType.NA) ? 0 : thisValue.getValue ();
String sign = signs.get (0); String sign = signs.get (0);
if (sign.equals ("(-)")) if (sign.equals ("(-)"))
@ -142,13 +143,13 @@ class Expression implements Value
{ {
thisValue = values.get (i); thisValue = values.get (i);
thisValue.calculate (); thisValue.calculate ();
if (thisValue.isError ()) if (thisValue.is (ValueType.ERROR))
{ {
valueType = thisValue.getValueType (); valueType = thisValue.getValueType ();
return this; return this;
} }
double nextValue = thisValue.isNotAvailable () ? 0 : thisValue.getValue (); double nextValue = thisValue.is (ValueType.NA) ? 0 : thisValue.getValue ();
sign = signs.get (i); sign = signs.get (i);
if (sign.equals ("(-)")) if (sign.equals ("(-)"))
@ -186,28 +187,33 @@ class Expression implements Value
return valueType; return valueType;
} }
// @Override
// public boolean isValue ()
// {
// return valueType == ValueType.VALUE;
// }
//
// @Override
// public boolean isNotAvailable ()
// {
// return valueType == ValueType.NA;
// }
//
// @Override
// public boolean isNotANumber ()
// {
// return valueType == ValueType.NAN;
// }
//
// @Override
// public boolean isError ()
// {
// return valueType == ValueType.ERROR;
// }
@Override @Override
public boolean isValue () public boolean is (ValueType type)
{ {
return valueType == ValueType.VALUE; return valueType == type;
}
@Override
public boolean isNotAvailable ()
{
return valueType == ValueType.NA;
}
@Override
public boolean isNotANumber ()
{
return valueType == ValueType.NAN;
}
@Override
public boolean isError ()
{
return valueType == ValueType.ERROR;
} }
@Override @Override
@ -217,11 +223,27 @@ class Expression implements Value
return value; return value;
} }
// @Override
// public String getText ()
// {
// return isNotAvailable () ? "NA" : isError () ? "Error" : isNotANumber () ? "NaN" : "";
// // return isNotAvailable () ? "NA" : isError () ? "Error" : "";
// }
@Override @Override
public String getText () public String getText ()
{ {
return isNotAvailable () ? "NA" : isError () ? "Error" : isNotANumber () ? "NaN" : ""; switch (valueType)
// return isNotAvailable () ? "NA" : isError () ? "Error" : ""; {
case NA:
return "NA";
case ERROR:
return "Error";
case NAN:
return "NaN";
default:
return "";
}
} }
private String checkBrackets (String input) private String checkBrackets (String input)

View File

@ -114,28 +114,34 @@ abstract class Function implements Value
return valueType; return valueType;
} }
@Override // @Override
public boolean isValue () // public boolean isValue ()
{ // {
return valueType == ValueType.VALUE; // return valueType == ValueType.VALUE;
} // }
//
// @Override
// public boolean isError ()
// {
// return valueType == ValueType.ERROR;
// }
//
// @Override
// public boolean isNotAvailable ()
// {
// return valueType == ValueType.NA;
// }
//
// @Override
// public boolean isNotANumber ()
// {
// return valueType == ValueType.NAN;
// }
@Override @Override
public boolean isError () public boolean is (ValueType type)
{ {
return valueType == ValueType.ERROR; return valueType == type;
}
@Override
public boolean isNotAvailable ()
{
return valueType == ValueType.NA;
}
@Override
public boolean isNotANumber ()
{
return valueType == ValueType.NAN;
} }
@Override @Override
@ -148,7 +154,18 @@ abstract class Function implements Value
@Override @Override
public String getText () public String getText ()
{ {
return isNotAvailable () ? "NA" : isError () ? "Error" : isNotANumber () ? "NaN" : ""; switch (valueType)
{
case NA:
return "NA";
case ERROR:
return "Error";
case NAN:
return "NaN";
default:
return "";
}
// return isNotAvailable () ? "NA" : isError () ? "Error" : isNotANumber () ? "NaN" : "";
} }
@Override @Override

View File

@ -36,7 +36,7 @@ class If extends Function
expTrue.calculate (); expTrue.calculate ();
if (expTrue.isError () || expTrue.isNotAvailable ()) if (expTrue.is (ValueType.ERROR) || expTrue.is (ValueType.NA))
valueType = expTrue.getValueType (); valueType = expTrue.getValueType ();
else else
value = expTrue.getValue (); value = expTrue.getValue ();
@ -49,7 +49,7 @@ class If extends Function
expFalse.calculate (); expFalse.calculate ();
if (expFalse.isError () || expFalse.isNotAvailable ()) if (expFalse.is (ValueType.ERROR) || expFalse.is (ValueType.NA))
valueType = expFalse.getValueType (); valueType = expFalse.getValueType ();
else else
value = expFalse.getValue (); value = expFalse.getValue ();

View File

@ -22,7 +22,7 @@ class Lookup extends RangeFunction
source = new Expression (parent, sourceText); source = new Expression (parent, sourceText);
source.calculate (); source.calculate ();
if (source.isError () || source.isNotAvailable ()) if (source.is (ValueType.ERROR) || source.is (ValueType.NA))
{ {
valueType = source.getValueType (); valueType = source.getValueType ();
return this; return this;

View File

@ -16,10 +16,10 @@ class Max extends RangeFunction
for (Address address : range) for (Address address : range)
{ {
Cell cell = parent.getCell (address); Cell cell = parent.getCell (address);
if (cell == null || cell.isNotAvailable ()) if (cell == null || cell.is (ValueType.NA))
continue; continue;
if (cell.isError ()) if (cell.is (ValueType.ERROR))
{ {
valueType = ValueType.ERROR; valueType = ValueType.ERROR;
break; break;

View File

@ -16,10 +16,10 @@ class Min extends RangeFunction
for (Address address : range) for (Address address : range)
{ {
Cell cell = parent.getCell (address); Cell cell = parent.getCell (address);
if (cell == null || cell.isNotAvailable ()) if (cell == null || cell.is (ValueType.NA))
continue; continue;
if (cell.isError ()) if (cell.is (ValueType.ERROR))
{ {
valueType = ValueType.ERROR; valueType = ValueType.ERROR;
break; break;

View File

@ -7,17 +7,17 @@ public class Na extends Function
super (parent, text); super (parent, text);
} }
@Override // @Override
public boolean isError () // public boolean isError ()
{ // {
return false; // return false;
} // }
//
@Override // @Override
public boolean isNotAvailable () // public boolean isNotAvailable ()
{ // {
return true; // return true;
} // }
@Override @Override
public double getValue () public double getValue ()

View File

@ -27,10 +27,10 @@ public class Npv extends RangeFunction
for (Address address : range) for (Address address : range)
{ {
Cell cell = parent.getCell (address); Cell cell = parent.getCell (address);
if (cell == null || cell.isNotAvailable ()) if (cell == null || cell.is (ValueType.NA))
continue; continue;
if (cell.isError ()) if (cell.is (ValueType.ERROR))
{ {
valueType = ValueType.ERROR; valueType = ValueType.ERROR;
break; break;

View File

@ -18,28 +18,33 @@ class Number implements Value
} }
} }
// @Override
// public boolean isValue ()
// {
// return valueType == ValueType.VALUE;
// }
//
// @Override
// public boolean isError ()
// {
// return valueType == ValueType.ERROR;
// }
//
// @Override
// public boolean isNotAvailable ()
// {
// return valueType == ValueType.NA;
// }
//
// @Override
// public boolean isNotANumber ()
// {
// return valueType == ValueType.NAN;
// }
@Override @Override
public boolean isValue () public boolean is (ValueType type)
{ {
return valueType == ValueType.VALUE; return valueType == type;
}
@Override
public boolean isError ()
{
return valueType == ValueType.ERROR;
}
@Override
public boolean isNotAvailable ()
{
return valueType == ValueType.NA;
}
@Override
public boolean isNotANumber ()
{
return valueType == ValueType.NAN;
} }
@Override @Override
@ -54,11 +59,27 @@ class Number implements Value
return value; return value;
} }
// @Override
// public String getText ()
// {
// return isNotAvailable () ? "NA" : isError () ? "Error" : isNotANumber () ? "NaN" : "";
// // return valueType == ValueType.ERROR ? "Error" : "";
// }
@Override @Override
public String getText () public String getText ()
{ {
return isNotAvailable () ? "NA" : isError () ? "Error" : isNotANumber () ? "NaN" : ""; switch (valueType)
// return valueType == ValueType.ERROR ? "Error" : ""; {
case NA:
return "NA";
case ERROR:
return "Error";
case NAN:
return "NaN";
default:
return "";
}
} }
@Override @Override

View File

@ -8,6 +8,7 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import com.bytezone.diskbrowser.utilities.HexFormatter; import com.bytezone.diskbrowser.utilities.HexFormatter;
import com.bytezone.diskbrowser.visicalc.Value.ValueType;
public class Sheet public class Sheet
{ {
@ -163,7 +164,7 @@ public class Sheet
{ {
Map<Integer, Cell> cells = order == 'R' ? rowOrderCells : columnOrderCells; Map<Integer, Cell> cells = order == 'R' ? rowOrderCells : columnOrderCells;
for (Cell cell : cells.values ()) for (Cell cell : cells.values ())
if (cell.isValue ()) if (cell.is (ValueType.VALUE))
cell.calculate (); cell.calculate ();
} }

View File

@ -16,10 +16,10 @@ class Sum extends RangeFunction
for (Address address : range) for (Address address : range)
{ {
Cell cell = parent.getCell (address); Cell cell = parent.getCell (address);
if (cell == null || cell.isNotAvailable ()) if (cell == null || cell.is (ValueType.NA))
continue; continue;
if (cell.isError ()) if (cell.is (ValueType.ERROR))
{ {
valueType = ValueType.ERROR; valueType = ValueType.ERROR;
break; break;

View File

@ -13,13 +13,14 @@ interface Value
public String getText (); public String getText ();
public boolean isValue (); // public boolean isValue ();
//
public boolean isError (); // public boolean isError ();
//
public boolean isNotAvailable (); // public boolean isNotAvailable ();
//
public boolean isNotANumber (); // public boolean isNotANumber ();
public boolean is (ValueType valueType);
public Value calculate (); public Value calculate ();
} }