This commit is contained in:
Denis Molony 2017-03-26 20:16:36 +11:00
parent a7127cca77
commit d09df374dc
21 changed files with 40 additions and 61 deletions

View File

@ -4,7 +4,7 @@ import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
public abstract class AbstractValue implements Value//, Iterable<Value> public abstract class AbstractValue implements Value
{ {
protected static final String FMT2 = "| %-9.9s : %-70.70s|%n"; protected static final String FMT2 = "| %-9.9s : %-70.70s|%n";
protected static final String FMT4 = "| %-9.9s : %-50.50s %-8.8s %-10.10s|%n"; protected static final String FMT4 = "| %-9.9s : %-50.50s %-8.8s %-10.10s|%n";
@ -15,7 +15,7 @@ public abstract class AbstractValue implements Value//, Iterable<Value>
protected final Cell cell; protected final Cell cell;
protected final String fullText; protected final String fullText;
protected ValueType valueType = ValueType.NUMBER; // could be BOOLEAN protected ValueType valueType; // = ValueType.NUMBER; // could be BOOLEAN
protected double value; protected double value;
protected boolean bool; protected boolean bool;
@ -94,8 +94,11 @@ public abstract class AbstractValue implements Value//, Iterable<Value>
protected String getValueText (Value value) protected String getValueText (Value value)
{ {
return value.getValueType () == ValueType.NUMBER ? value.getDouble () + "" if (value.getValueType () == ValueType.NUMBER)
: value.getBoolean () ? "TRUE" : "FALSE"; return value.getDouble () + "";
if (value.getValueType () == ValueType.BOOLEAN)
return value.getBoolean () ? "TRUE" : "FALSE";
return "??*??";
} }
@Override @Override

View File

@ -9,7 +9,6 @@ public class Average extends ValueListFunction
super (cell, text); super (cell, text);
assert text.startsWith ("@AVERAGE(") : text; assert text.startsWith ("@AVERAGE(") : text;
valueType = ValueType.NUMBER;
} }
@Override @Override

View File

@ -4,8 +4,6 @@ import java.util.Iterator;
class Cell implements Value, Comparable<Cell> class Cell implements Value, Comparable<Cell>
{ {
private static final String line = "+----------------------------------------"
+ "--------------------------------------------+";
private static final String empty = " "; private static final String empty = " ";
private final Address address; private final Address address;

View File

@ -9,7 +9,6 @@ public class Choose extends ValueListFunction
super (cell, text); super (cell, text);
assert text.startsWith ("@CHOOSE(") : text; assert text.startsWith ("@CHOOSE(") : text;
valueType = ValueType.NUMBER;
} }
@Override @Override

View File

@ -12,18 +12,15 @@ class Condition extends AbstractValue implements Iterable<Value>
private String comparator; private String comparator;
private String conditionText; private String conditionText;
private String valueText; private String valueText;
private final String fullText;
// private Address address;
private Expression conditionExpression; private Value conditionExpression;
private Expression valueExpression; private Value valueExpression;
public Condition (Cell cell, String text) public Condition (Cell cell, String text)
{ {
super (cell, text); super (cell, text);
valueType = ValueType.BOOLEAN; valueType = ValueType.BOOLEAN;
fullText = text;
for (String comp : comparators) for (String comp : comparators)
{ {
@ -44,38 +41,20 @@ class Condition extends AbstractValue implements Iterable<Value>
} }
if (comparator == null) if (comparator == null)
{ if (text.startsWith ("@") || cellAddress.matcher (text).matches ())
if (text.startsWith ("@"))
{ {
conditionText = text; conditionText = text;
conditionExpression = new Expression (cell, text); conditionExpression = new Expression (cell, text).reduce ();
values.add (conditionExpression);
// comparator = "=";
//
// valueText = "1";
// valueExpression = new Expression (cell, valueText);
// values.add (valueExpression);
}
else if (cellAddress.matcher (text).matches ())
{
conditionText = text;
conditionExpression = new Expression (cell, text);
conditionExpression.valueType = ValueType.BOOLEAN;
values.add (conditionExpression); values.add (conditionExpression);
} }
else else
{ throw new IllegalArgumentException (
System.out.println ("No comparator and not a function: " + text); "No comparator and not a function or address: " + text);
throw new IllegalArgumentException ("No comparator and not a function: " + text);
}
}
} }
@Override @Override
public void calculate () public void calculate ()
{ {
// System.out.printf ("********Calc: %s%n", fullText);
valueResult = ValueResult.VALID; valueResult = ValueResult.VALID;
conditionExpression.calculate (); conditionExpression.calculate ();
@ -89,7 +68,6 @@ class Condition extends AbstractValue implements Iterable<Value>
if (conditionExpression.getValueType () == ValueType.BOOLEAN) if (conditionExpression.getValueType () == ValueType.BOOLEAN)
{ {
bool = conditionExpression.getBoolean (); bool = conditionExpression.getBoolean ();
// System.out.printf ("********Bool: %s%n", bool);
return; return;
} }
@ -117,7 +95,6 @@ class Condition extends AbstractValue implements Iterable<Value>
bool = conditionResult >= expressionResult; bool = conditionResult >= expressionResult;
else else
System.out.printf ("Unexpected comparator result [%s]%n", comparator); System.out.printf ("Unexpected comparator result [%s]%n", comparator);
// System.out.printf ("********Bool: %s%n", bool);
} }
@Override @Override
@ -162,6 +139,7 @@ class Condition extends AbstractValue implements Iterable<Value>
public String toString () public String toString ()
{ {
StringBuilder text = new StringBuilder (); StringBuilder text = new StringBuilder ();
text.append (LINE + "\n"); text.append (LINE + "\n");
text.append (String.format (FMT4, "Predicate", getFullText (), valueType, text.append (String.format (FMT4, "Predicate", getFullText (), valueType,
getValueText (this))); getValueText (this)));
@ -173,7 +151,7 @@ class Condition extends AbstractValue implements Iterable<Value>
text.append (String.format (FMT4, "Right", valueText, text.append (String.format (FMT4, "Right", valueText,
valueExpression.getValueType (), getValueText (valueExpression))); valueExpression.getValueType (), getValueText (valueExpression)));
} }
// text.append (LINE);
return text.toString (); return text.toString ();
} }
} }

View File

@ -7,6 +7,7 @@ class Count extends ValueListFunction
public Count (Cell cell, String text) public Count (Cell cell, String text)
{ {
super (cell, text); super (cell, text);
assert text.startsWith ("@COUNT(") : text; assert text.startsWith ("@COUNT(") : text;
} }
@ -14,7 +15,6 @@ class Count extends ValueListFunction
public void calculate () public void calculate ()
{ {
value = 0; value = 0;
valueType = ValueType.NUMBER;
if (!isRange) if (!isRange)
value = list.size (); value = list.size ();

View File

@ -7,6 +7,8 @@ class Error extends ConstantFunction
super (cell, text); super (cell, text);
assert text.startsWith ("@ERROR") : text; assert text.startsWith ("@ERROR") : text;
valueResult = ValueResult.ERROR; valueResult = ValueResult.ERROR;
valueType = ValueType.NUMBER;
} }
} }

View File

@ -65,8 +65,6 @@ public class Format
val = String.format (rightFormat, val); val = String.format (rightFormat, val);
} }
// System.out.printf ("[%s]%n", val);
if (val.length () > colWidth) if (val.length () > colWidth)
return OVERFLOW.substring (0, colWidth); return OVERFLOW.substring (0, colWidth);

View File

@ -83,6 +83,7 @@ class If extends Function
public String toString () public String toString ()
{ {
StringBuilder text = new StringBuilder (); StringBuilder text = new StringBuilder ();
text.append (String.format ("%s%n", LINE)); text.append (String.format ("%s%n", LINE));
text.append (String.format (FMT4, getType (), getFullText (), getValueType (), text.append (String.format (FMT4, getType (), getFullText (), getValueType (),
getValueText (this))); getValueText (this)));
@ -91,6 +92,7 @@ class If extends Function
attach (text, "true", textTrue, expTrue); attach (text, "true", textTrue, expTrue);
else else
attach (text, "false", textFalse, expFalse); attach (text, "false", textFalse, expFalse);
return text.toString (); return text.toString ();
} }
} }

View File

@ -7,7 +7,6 @@ class IsError extends BooleanFunction
super (cell, text); super (cell, text);
assert text.startsWith ("@ISERROR(") : text; assert text.startsWith ("@ISERROR(") : text;
valueType = ValueType.BOOLEAN;
} }
@Override @Override

View File

@ -7,7 +7,6 @@ public class IsNa extends BooleanFunction
super (cell, text); super (cell, text);
assert text.startsWith ("@ISNA(") : text; assert text.startsWith ("@ISNA(") : text;
valueType = ValueType.BOOLEAN;
} }
@Override @Override

View File

@ -7,7 +7,6 @@ class Lookup extends ValueListFunction
super (cell, text); super (cell, text);
assert text.startsWith ("@LOOKUP(") : text; assert text.startsWith ("@LOOKUP(") : text;
valueType = ValueType.NUMBER;
} }
@Override @Override
@ -33,11 +32,6 @@ class Lookup extends ValueListFunction
double sourceValue = source.getDouble (); double sourceValue = source.getDouble ();
Address target = null; Address target = null;
// is the range horizontal or vertical?
Cell firstCell = (Cell) list.get (1);
Cell lastCell = (Cell) list.get (list.size () - 1);
boolean isVertical = firstCell.getAddress ().columnMatches (lastCell.getAddress ());
for (int i = 1; i < list.size (); i++) // skip first entry for (int i = 1; i < list.size (); i++) // skip first entry
{ {
Cell cell = (Cell) list.get (i); Cell cell = (Cell) list.get (i);
@ -52,11 +46,19 @@ class Lookup extends ValueListFunction
return; return;
} }
Address adjacentAddress = isVertical ? target.nextColumn () : target.nextRow (); Address adjacentAddress = isVertical () ? target.nextColumn () : target.nextRow ();
if (cell.cellExists (adjacentAddress)) if (cell.cellExists (adjacentAddress))
value = cell.getCell (adjacentAddress).getDouble (); value = cell.getCell (adjacentAddress).getDouble ();
else else
value = 0; value = 0;
} }
// is the range horizontal or vertical?
private boolean isVertical ()
{
Cell firstCell = (Cell) list.get (1);
Cell lastCell = (Cell) list.get (list.size () - 1);
return firstCell.getAddress ().columnMatches (lastCell.getAddress ());
}
} }

View File

@ -7,7 +7,6 @@ class Max extends ValueListFunction
super (cell, text); super (cell, text);
assert text.startsWith ("@MAX(") : text; assert text.startsWith ("@MAX(") : text;
valueType = ValueType.NUMBER;
} }
@Override @Override

View File

@ -7,7 +7,6 @@ class Min extends ValueListFunction
super (cell, text); super (cell, text);
assert text.startsWith ("@MIN(") : text; assert text.startsWith ("@MIN(") : text;
valueType = ValueType.NUMBER;
} }
@Override @Override

View File

@ -7,6 +7,8 @@ public class Na extends ConstantFunction
super (cell, text); super (cell, text);
assert text.equals ("@NA") : text; assert text.equals ("@NA") : text;
valueResult = ValueResult.NA; valueResult = ValueResult.NA;
valueType = ValueType.NUMBER;
} }
} }

View File

@ -9,7 +9,6 @@ public class Npv extends ValueListFunction
super (cell, text); super (cell, text);
assert text.startsWith ("@NPV(") : text; assert text.startsWith ("@NPV(") : text;
valueType = ValueType.NUMBER;
} }
@Override @Override

View File

@ -1,6 +1,5 @@
package com.bytezone.diskbrowser.visicalc; package com.bytezone.diskbrowser.visicalc;
import java.security.InvalidParameterException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
@ -9,10 +8,10 @@ import java.util.regex.Pattern;
class Range implements Iterable<Address> class Range implements Iterable<Address>
{ {
private static final Pattern cellAddress = Pattern.compile ("[A-B]?[A-Z][0-9]{1,3}"); // private static final Pattern cellAddress = Pattern.compile ("[A-B]?[A-Z][0-9]{1,3}");
private static final Pattern rangePattern = private static final Pattern rangePattern =
Pattern.compile ("([A-B]?[A-Z])([0-9]{1,3})\\.\\.\\.([A-B]?[A-Z])([0-9]{1,3})"); Pattern.compile ("([A-B]?[A-Z])([0-9]{1,3})\\.\\.\\.([A-B]?[A-Z])([0-9]{1,3})");
private static final Pattern addressList = Pattern.compile ("\\(([^,]+(,[^,]+)*)\\)"); // private static final Pattern addressList = Pattern.compile ("\\(([^,]+(,[^,]+)*)\\)");
private Address from, to; private Address from, to;
private final List<Address> range = new ArrayList<Address> (); private final List<Address> range = new ArrayList<Address> ();
@ -57,7 +56,8 @@ class Range implements Iterable<Address>
cell.getCell (from); cell.getCell (from);
} }
else else
throw new InvalidParameterException (); throw new IllegalArgumentException (
"Cannot create range " + from.getText () + ", " + to.getText ());
from = tempFrom; from = tempFrom;
} }

View File

@ -7,7 +7,6 @@ class Sum extends ValueListFunction
super (cell, text); super (cell, text);
assert text.startsWith ("@SUM(") : text; assert text.startsWith ("@SUM(") : text;
valueType = ValueType.NUMBER;
} }
@Override @Override

View File

@ -12,6 +12,7 @@ public abstract class ValueFunction extends Function
source = cell.getExpressionValue (functionText); source = cell.getExpressionValue (functionText);
values.add (source); values.add (source);
valueType = ValueType.NUMBER;
} }
@Override @Override

View File

@ -7,7 +7,7 @@ import java.util.List;
public class ValueList implements Iterable<Value> public class ValueList implements Iterable<Value>
{ {
private final List<Value> values = new ArrayList<Value> (); private final List<Value> values = new ArrayList<Value> ();
private boolean rangeFound; private boolean hasRange;
public ValueList (Cell cell, String text) public ValueList (Cell cell, String text)
{ {
@ -19,7 +19,7 @@ public class ValueList implements Iterable<Value>
if (Range.isRange (parameter)) if (Range.isRange (parameter))
{ {
rangeFound = true; hasRange = true;
for (Address address : new Range (cell, parameter)) for (Address address : new Range (cell, parameter))
values.add (cell.getCell (address)); values.add (cell.getCell (address));
} }
@ -35,7 +35,7 @@ public class ValueList implements Iterable<Value>
public boolean hasRange () public boolean hasRange ()
{ {
return rangeFound; return hasRange;
} }
public Value get (int index) public Value get (int index)

View File

@ -11,6 +11,7 @@ public abstract class ValueListFunction extends Function
list = new ValueList (cell, functionText); list = new ValueList (cell, functionText);
isRange = functionText.indexOf ("...") > 0; isRange = functionText.indexOf ("...") > 0;
valueType = ValueType.NUMBER;
for (Value v : list) for (Value v : list)
values.add (v); values.add (v);