Value.calculate returns void

This commit is contained in:
Denis Molony 2017-03-01 07:39:26 +11:00
parent 2993fbb510
commit 7451194aaf
26 changed files with 111 additions and 146 deletions

View File

@ -10,7 +10,7 @@ public class Abs extends Function
}
@Override
public Value calculate ()
public void calculate ()
{
if (source == null)
{
@ -20,7 +20,5 @@ public class Abs extends Function
value = Math.abs (source.getValue ());
valueType = source.getValueType ();
return this;
}
}

View File

@ -47,46 +47,51 @@ public abstract class AbstractValue implements Value
case NA:
return "NA";
case ERROR:
return "Error";
return "ERROR";
default:
return "";
}
}
String getValueText (Value value, int depth)
@Override
public void calculate ()
{
}
// for debugging
String getValueText (int depth)
{
StringBuilder text = new StringBuilder ();
String typeText = " " + value.getTypeText ();
if (value.isValueType (ValueType.VALUE))
String typeText = " " + getTypeText ();
if (isValueType (ValueType.VALUE))
{
String valueText = String.format ("%f", value.getValue ());
String valueText = String.format ("%f", getValue ());
text.append (String.format ("| %-10s : %-69s |%n", typeText, valueText));
}
else
text.append (
String.format ("| %-10s : %-69s |%n", typeText, value.getValueType ()));
text.append (String.format ("| %-10s : %-69s |%n", typeText, getValueType ()));
if (value instanceof Expression)
if (this instanceof Expression)
{
text.append (
String.format ("| Expression : %-69s |%n", ((Expression) value).fullText ()));
for (Value v : (Expression) value)
text.append (getValueText (v, depth + 1));
String.format ("| Expression : %-69s |%n", ((Expression) this).fullText ()));
for (Value v : (Expression) this)
text.append (((AbstractValue) v).getValueText (depth + 1));
}
else if (value instanceof Function)
else if (this instanceof Function)
{
text.append (
String.format ("| Function : %-69s |%n", ((Function) value).fullText));
for (Value v : (Function) value)
text.append (getValueText (v, depth + 1));
String.format ("| Function : %-69s |%n", ((Function) this).fullText));
for (Value v : (Function) this)
text.append (((AbstractValue) v).getValueText (depth + 1));
}
else if (value instanceof Condition)
else if (this instanceof Condition)
{
text.append (
String.format ("| Condition : %-69s |%n", ((Condition) value).fullText));
for (Value v : (Condition) value)
text.append (getValueText (v, depth + 1));
String.format ("| Condition : %-69s |%n", ((Condition) this).fullText));
for (Value v : (Condition) this)
text.append (((AbstractValue) v).getValueText (depth + 1));
}
return text.toString ();

View File

@ -16,7 +16,7 @@ class And extends Function
}
@Override
public Value calculate ()
public void calculate ()
{
for (Condition condition : conditions)
{
@ -24,10 +24,9 @@ class And extends Function
if (condition.getValue () == 0)
{
value = 0;
return this;
return;
}
}
value = 1;
return this;
}
}

View File

@ -11,7 +11,7 @@ public class Average extends Function
}
@Override
public Value calculate ()
public void calculate ()
{
double total = 0.0;
int totalChecked = 0;
@ -39,7 +39,5 @@ public class Average extends Function
value = total / totalChecked;
valueType = ValueType.VALUE;
}
return this;
}
}

View File

@ -2,7 +2,6 @@ package com.bytezone.diskbrowser.visicalc;
class Cell extends AbstractValue implements Comparable<Cell>
{
// private static final DecimalFormat nf = new DecimalFormat ("#####0.00");
private static final String line = "+----------------------------------------"
+ "--------------------------------------------+";
private static final String empty = " ";
@ -10,7 +9,6 @@ class Cell extends AbstractValue implements Comparable<Cell>
private final Address address;
private final Sheet parent;
private CellType cellType;
// private final Format format = new Format ();
private String expressionText;
private char cellFormat = ' ';
@ -72,6 +70,8 @@ class Cell extends AbstractValue implements Comparable<Cell>
void setValue (String command)
{
assert cellType == CellType.EMPTY;
if (!command.isEmpty () && command.charAt (0) == '"')
{
label = command.substring (1);
@ -143,10 +143,11 @@ class Cell extends AbstractValue implements Comparable<Cell>
return Format.justify (empty, colWidth, ' ');
case VALUE:
if (value == null)
calculate ();
if (!isValueType (ValueType.VALUE))
return Format.justify (value.getText (), colWidth, 'R');
char formatChar = cellFormat != ' ' ? cellFormat : globalFormat;
return Format.format (value, formatChar, colWidth);
return " " + Format.format (value, formatChar, colWidth - 1);
default:
assert false;
@ -193,10 +194,10 @@ class Cell extends AbstractValue implements Comparable<Cell>
}
@Override
public Value calculate ()
public void calculate ()
{
if (value != null && value.isValueType (ValueType.VALUE))
return this;
return;
if (value == null)
{
@ -208,7 +209,7 @@ class Cell extends AbstractValue implements Comparable<Cell>
value.calculate ();
return this;
return;
}
public String getDebugText ()
@ -240,7 +241,7 @@ class Cell extends AbstractValue implements Comparable<Cell>
if (value == null)
text.append (String.format ("| Value : %-69s |%n", "null"));
else
text.append (getValueText (value, 0));
text.append (((AbstractValue) value).getValueText (0));
break;
default:

View File

@ -17,10 +17,4 @@ public class Choose extends Function
range = new Range (rangeText);
source = new Number (sourceText);
}
@Override
public Value calculate ()
{
return null;
}
}

View File

@ -49,7 +49,7 @@ class Condition extends AbstractValue implements Iterable<Value>
}
@Override
public Value calculate ()
public void calculate ()
{
value = 0;
@ -67,7 +67,7 @@ class Condition extends AbstractValue implements Iterable<Value>
if (conditionExpression.isValueType (ValueType.ERROR)
|| valueExpression.isValueType (ValueType.ERROR))
return this;
return;
double conditionResult = conditionExpression.getValue ();
double valueResult = valueExpression.getValue ();
@ -86,8 +86,6 @@ class Condition extends AbstractValue implements Iterable<Value>
value = conditionResult >= valueResult ? 1 : 0;
else
System.out.printf ("Unexpected comparator result [%s]%n", comparator);
return this;
}
@Override

View File

@ -11,7 +11,7 @@ class Count extends Function
}
@Override
public Value calculate ()
public void calculate ()
{
value = 0;
valueType = ValueType.VALUE;
@ -31,7 +31,5 @@ class Count extends Function
if (cell.getValue () != 0.0)
value++;
}
return this;
}
}

View File

@ -13,10 +13,4 @@ class Error extends Function
{
return 0;
}
@Override
public Value calculate ()
{
return this;
}
}

View File

@ -29,7 +29,6 @@ class Expression extends AbstractValue implements Iterable<Value>
// [.3*(B4+B7+B8+B9)]
// [+N12+(P12*(.2*K12+K9-O12))]
// private final List<Value> values = new ArrayList<Value> ();
private final List<String> operators = new ArrayList<String> ();
private final List<String> signs = new ArrayList<String> ();
@ -144,12 +143,12 @@ class Expression extends AbstractValue implements Iterable<Value>
}
@Override
public Value calculate ()
public void calculate ()
{
if (values.size () == 0)
{
System.out.println ("nothing to calculate: " + text);
return this;
return;
}
try
@ -163,7 +162,7 @@ class Expression extends AbstractValue implements Iterable<Value>
else
{
valueType = thisValue.getValueType ();
return this;
return;
}
String sign = signs.get (0);
@ -181,7 +180,7 @@ class Expression extends AbstractValue implements Iterable<Value>
else
{
valueType = thisValue.getValueType ();
return this;
return;
}
sign = signs.get (i);
@ -211,8 +210,6 @@ class Expression extends AbstractValue implements Iterable<Value>
valueType = ValueType.ERROR;
e.printStackTrace ();
}
return this;
}
private String checkBrackets (String input)

View File

@ -2,45 +2,63 @@ package com.bytezone.diskbrowser.visicalc;
import java.text.DecimalFormat;
import com.bytezone.diskbrowser.visicalc.Value.ValueType;
public class Format
{
private static final DecimalFormat nf = new DecimalFormat ("#####0.00");
static String format (Value value, char formatChar, int colWidth)
{
if (!value.isValueType (ValueType.VALUE))
return justify (value.getText (), colWidth, formatChar);
double actualValue = value.getValue ();
if (actualValue == -0.0)
actualValue = 0;
if (formatChar == 'I')
switch (formatChar)
{
String integerFormat = String.format ("%%%d.0f", colWidth);
return String.format (integerFormat, value.getValue ());
}
else if (formatChar == '$')
{
String currencyFormat = String.format ("%%%d.%ds", colWidth, colWidth);
return String.format (currencyFormat, nf.format (value.getValue ()));
}
else if (formatChar == '*')
{
String graphFormat = String.format ("%%-%d.%ds", colWidth, colWidth);
// this is not finished
return String.format (graphFormat, "********************");
}
else
{
// this could be improved
String numberFormat = String.format ("%%%d.5f", colWidth + 6);
String val = String.format (numberFormat, value.getValue ());
while (val.endsWith ("0"))
val = ' ' + val.substring (0, val.length () - 1);
if (val.endsWith ("."))
val = ' ' + val.substring (0, val.length () - 1);
if (val.length () > colWidth)
val = val.substring (val.length () - colWidth);
return val;
case 'L':
case 'R':
case ' ':
// this could be improved
String numberFormat = String.format ("%%%d.5f", colWidth + 6);
String val = String.format (numberFormat, actualValue);
while (val.endsWith ("0"))
val = val.substring (0, val.length () - 1);
if (val.endsWith ("."))
val = val.substring (0, val.length () - 1);
if (val.startsWith ("0."))
val = val.substring (1);
if (val.length () > colWidth)
val = val.substring (val.length () - colWidth);
// System.out.printf ("len:%d fmt: %s%n", val.length (), formatChar);
if (val.startsWith (" ") && formatChar == 'L')
{
String leftFormat = String.format ("%%-%ds", colWidth);
val = String.format (leftFormat, val.trim ());
}
return val;
case 'I':
String integerFormat = String.format ("%%%d.0f", colWidth);
String result = String.format (integerFormat, actualValue);
if (result.length () > colWidth)
return ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>".substring (0, colWidth);
return result;
case '$':
String currencyFormat = String.format ("%%%d.%ds", colWidth, colWidth);
return String.format (currencyFormat, nf.format (actualValue));
case '*':
String graphFormat = String.format ("%%-%d.%ds", colWidth, colWidth);
// this is not finished
return String.format (graphFormat, "********************");
default:
System.out.printf ("[%s]%n", formatChar);
return "??????????????????????".substring (0, colWidth);
}
}

View File

@ -24,7 +24,7 @@ class If extends Function
}
@Override
public Value calculate ()
public void calculate ()
{
valueType = ValueType.VALUE;
condition.calculate ();
@ -59,8 +59,6 @@ class If extends Function
else
value = expFalse.getValue ();
}
return this;
}
@Override

View File

@ -9,11 +9,10 @@ public class Int extends Function
}
@Override
public Value calculate ()
public void calculate ()
{
Expression exp = new Expression (parent, functionText);
value = (int) exp.getValue ();
valueType = exp.getValueType ();
return this;
}
}

View File

@ -10,14 +10,12 @@ class IsError extends Function
}
@Override
public Value calculate ()
public void calculate ()
{
if (cell == null)
cell = parent.getCell (functionText);
value = cell == null ? 1 : cell.isValueType (ValueType.ERROR) ? 1 : 0;
valueType = ValueType.VALUE;
return this;
}
}

View File

@ -10,7 +10,7 @@ public class IsNa extends Function
}
@Override
public Value calculate ()
public void calculate ()
{
if (expression == null)
expression = new Expression (parent, functionText);
@ -18,6 +18,5 @@ public class IsNa extends Function
expression.calculate ();
value = expression.getValue ();
valueType = expression.getValueType ();
return this;
}
}

View File

@ -18,7 +18,7 @@ class Lookup extends Function
}
@Override
public Value calculate ()
public void calculate ()
{
if (source == null)
{
@ -30,7 +30,7 @@ class Lookup extends Function
if (!source.isValueType (ValueType.VALUE))
{
valueType = source.getValueType ();
return this;
return;
}
double sourceValue = source.getValue ();
@ -54,8 +54,6 @@ class Lookup extends Function
value = adjacentCell.getValue ();
valueType = ValueType.VALUE;
}
return this;
}
// @LOOKUP(B8,F3...F16)

View File

@ -11,7 +11,7 @@ class Max extends Function
}
@Override
public Value calculate ()
public void calculate ()
{
value = Double.MIN_VALUE;
int totalChecked = 0;
@ -38,7 +38,5 @@ class Max extends Function
valueType = ValueType.NA;
else
valueType = ValueType.VALUE;
return this;
}
}

View File

@ -11,7 +11,7 @@ class Min extends Function
}
@Override
public Value calculate ()
public void calculate ()
{
value = Double.MAX_VALUE;
int totalChecked = 0;
@ -38,7 +38,5 @@ class Min extends Function
valueType = ValueType.NA;
else
valueType = ValueType.VALUE;
return this;
}
}

View File

@ -13,10 +13,4 @@ public class Na extends Function
{
return 0;
}
@Override
public Value calculate ()
{
return this;
}
}

View File

@ -21,7 +21,7 @@ public class Npv extends Function
}
@Override
public Value calculate ()
public void calculate ()
{
value = 0;
valueType = ValueType.VALUE;
@ -40,7 +40,5 @@ public class Npv extends Function
double temp = cell.getValue ();
}
return this;
}
}

View File

@ -22,10 +22,4 @@ class Number extends AbstractValue
{
return String.format ("Number: %f", value);
}
@Override
public Value calculate ()
{
return this;
}
}

View File

@ -16,7 +16,7 @@ class Or extends Function
}
@Override
public Value calculate ()
public void calculate ()
{
for (Condition condition : conditions)
{
@ -24,10 +24,9 @@ class Or extends Function
if (condition.getValue () == 1)
{
value = 1;
return this;
return;
}
}
value = 0;
return this;
}
}

View File

@ -7,10 +7,4 @@ class Pi extends Function
super (parent, text);
value = Math.PI;
}
@Override
public Value calculate ()
{
return this;
}
}

View File

@ -21,7 +21,7 @@ public class Sheet
private final Map<Integer, Cell> columnOrderCells = new TreeMap<Integer, Cell> ();
private final List<String> lines = new ArrayList<String> ();
private char globalFormat;
private char globalFormat = ' ';
private final Map<Integer, Integer> columnWidths = new TreeMap<Integer, Integer> ();
private int columnWidth = 12;

View File

@ -11,7 +11,7 @@ class Sum extends Function
}
@Override
public Value calculate ()
public void calculate ()
{
value = 0;
valueType = ValueType.VALUE;
@ -30,7 +30,5 @@ class Sum extends Function
value += cell.getValue ();
}
return this;
}
}

View File

@ -7,15 +7,15 @@ interface Value
VALUE, ERROR, NA
}
public double getValue ();
public double getValue (); // if ValueType == VALUE
public String getText ();
public String getText (); // if ValueType != VALUE
public boolean isValueType (ValueType valueType);
public ValueType getValueType ();
public Value calculate ();
public void calculate ();
public String getTypeText ();
public String getTypeText (); // Number/Function/Expression etc
}