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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -7,10 +7,4 @@ class Pi extends Function
super (parent, text); super (parent, text);
value = Math.PI; 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 Map<Integer, Cell> columnOrderCells = new TreeMap<Integer, Cell> ();
private final List<String> lines = new ArrayList<String> (); 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 final Map<Integer, Integer> columnWidths = new TreeMap<Integer, Integer> ();
private int columnWidth = 12; private int columnWidth = 12;

View File

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

View File

@ -7,15 +7,15 @@ interface Value
VALUE, ERROR, NA 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 boolean isValueType (ValueType valueType);
public ValueType getValueType (); public ValueType getValueType ();
public Value calculate (); public void calculate ();
public String getTypeText (); public String getTypeText (); // Number/Function/Expression etc
} }