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.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 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 String fullText;
protected ValueType valueType = ValueType.NUMBER; // could be BOOLEAN
protected ValueType valueType; // = ValueType.NUMBER; // could be BOOLEAN
protected double value;
protected boolean bool;
@ -94,8 +94,11 @@ public abstract class AbstractValue implements Value//, Iterable<Value>
protected String getValueText (Value value)
{
return value.getValueType () == ValueType.NUMBER ? value.getDouble () + ""
: value.getBoolean () ? "TRUE" : "FALSE";
if (value.getValueType () == ValueType.NUMBER)
return value.getDouble () + "";
if (value.getValueType () == ValueType.BOOLEAN)
return value.getBoolean () ? "TRUE" : "FALSE";
return "??*??";
}
@Override

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -7,7 +7,6 @@ class Lookup extends ValueListFunction
super (cell, text);
assert text.startsWith ("@LOOKUP(") : text;
valueType = ValueType.NUMBER;
}
@Override
@ -33,11 +32,6 @@ class Lookup extends ValueListFunction
double sourceValue = source.getDouble ();
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
{
Cell cell = (Cell) list.get (i);
@ -52,11 +46,19 @@ class Lookup extends ValueListFunction
return;
}
Address adjacentAddress = isVertical ? target.nextColumn () : target.nextRow ();
Address adjacentAddress = isVertical () ? target.nextColumn () : target.nextRow ();
if (cell.cellExists (adjacentAddress))
value = cell.getCell (adjacentAddress).getDouble ();
else
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);
assert text.startsWith ("@MAX(") : text;
valueType = ValueType.NUMBER;
}
@Override

View File

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

View File

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

View File

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

View File

@ -1,6 +1,5 @@
package com.bytezone.diskbrowser.visicalc;
import java.security.InvalidParameterException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
@ -9,10 +8,10 @@ import java.util.regex.Pattern;
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 =
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 final List<Address> range = new ArrayList<Address> ();
@ -57,7 +56,8 @@ class Range implements Iterable<Address>
cell.getCell (from);
}
else
throw new InvalidParameterException ();
throw new IllegalArgumentException (
"Cannot create range " + from.getText () + ", " + to.getText ());
from = tempFrom;
}

View File

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

View File

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

View File

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

View File

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