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

View File

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

View File

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

View File

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

View File

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

View File

@ -114,28 +114,34 @@ abstract class Function implements Value
return valueType;
}
@Override
public boolean isValue ()
{
return valueType == ValueType.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
public boolean isError ()
public boolean is (ValueType type)
{
return valueType == ValueType.ERROR;
}
@Override
public boolean isNotAvailable ()
{
return valueType == ValueType.NA;
}
@Override
public boolean isNotANumber ()
{
return valueType == ValueType.NAN;
return valueType == type;
}
@Override
@ -148,7 +154,18 @@ abstract class Function implements Value
@Override
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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -27,10 +27,10 @@ public class Npv extends RangeFunction
for (Address address : range)
{
Cell cell = parent.getCell (address);
if (cell == null || cell.isNotAvailable ())
if (cell == null || cell.is (ValueType.NA))
continue;
if (cell.isError ())
if (cell.is (ValueType.ERROR))
{
valueType = ValueType.ERROR;
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
public boolean isValue ()
public boolean is (ValueType type)
{
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;
return valueType == type;
}
@Override
@ -54,11 +59,27 @@ class Number implements Value
return value;
}
// @Override
// public String getText ()
// {
// return isNotAvailable () ? "NA" : isError () ? "Error" : isNotANumber () ? "NaN" : "";
// // return valueType == ValueType.ERROR ? "Error" : "";
// }
@Override
public String getText ()
{
return isNotAvailable () ? "NA" : isError () ? "Error" : isNotANumber () ? "NaN" : "";
// return valueType == ValueType.ERROR ? "Error" : "";
switch (valueType)
{
case NA:
return "NA";
case ERROR:
return "Error";
case NAN:
return "NaN";
default:
return "";
}
}
@Override

View File

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

View File

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

View File

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