minor fixes

This commit is contained in:
Denis Molony
2017-03-25 19:02:02 +11:00
parent ae4547b419
commit bf246bc171
10 changed files with 78 additions and 76 deletions

View File

@@ -55,12 +55,14 @@ public abstract class AbstractValue implements Value//, Iterable<Value>
@Override
public double getDouble ()
{
assert valueType == ValueType.NUMBER;
return value;
}
@Override
public boolean getBoolean ()
{
assert valueType == ValueType.BOOLEAN;
return bool;
}
@@ -100,6 +102,14 @@ public abstract class AbstractValue implements Value//, Iterable<Value>
// System.out.println ("calculate not overridden: " + cell);
}
protected void attach (StringBuilder text, String title, String textValue, Value value)
{
text.append (String.format (FMT4, title, textValue, value.getValueType (),
getValueText (value)));
for (Value v : value)
text.append (v);
}
protected String getValueText (Value value)
{
return "" + (value.getValueType () == ValueType.NUMBER ? value.getDouble ()

View File

@@ -14,6 +14,13 @@ class And extends ConditionListFunction
for (Value condition : conditions)
{
condition.calculate ();
if (condition.getValueType () != ValueType.BOOLEAN)
{
valueResult = ValueResult.ERROR;
return;
}
if (!condition.getBoolean ())
{
bool = false;

View File

@@ -9,25 +9,15 @@ public class ConditionListFunction extends Function
super (cell, text);
conditions = new ConditionList (cell, functionText);
for (Value condition : conditions)
values.add (condition);
valueType = ValueType.BOOLEAN;
}
@Override
public String getType ()
{
return "ConditionListFunction";
}
@Override
public String toString ()
{
StringBuilder text = new StringBuilder ();
text.append (String.format ("%s%n", LINE));
text.append (
String.format (FMT4, "CFN", getFullText (), valueType, getValueText (this)));
for (Value value : conditions)
text.append (String.format (FMT4, value.getType (), value.getFullText (),
value.getValueType (), getValueText (value)));
return text.toString ();
return "CLF";
}
}

View File

@@ -125,8 +125,10 @@ class Expression extends AbstractValue
Value thisValue = values.get (0);
thisValue.calculate ();
value = thisValue.getDouble ();
bool = thisValue.getBoolean ();
if (valueType == ValueType.NUMBER)
value = thisValue.getDouble ();
else
bool = thisValue.getBoolean ();
if (!thisValue.isValid ()) // ERROR / NA
{

View File

@@ -55,8 +55,10 @@ class If extends Function
if (!expTrue.isValid ())
valueResult = expTrue.getValueResult ();
else
else if (valueType == ValueType.NUMBER)
value = expTrue.getDouble ();
else
bool = expTrue.getBoolean ();
}
else // false
{
@@ -64,8 +66,10 @@ class If extends Function
if (!expFalse.isValid ())
valueResult = expTrue.getValueResult ();
else
else if (valueType == ValueType.NUMBER)
value = expFalse.getDouble ();
else
bool = expFalse.getBoolean ();
}
}
@@ -80,22 +84,13 @@ class If extends Function
{
StringBuilder text = new StringBuilder ();
text.append (String.format ("%s%n", LINE));
text.append (
String.format (FMT4, getType (), fullText, valueType, getValueText (this)));
text.append (String.format (FMT4, "condition", conditionText,
condition.getValueType (), getValueText (condition)));
text.append (String.format (FMT4, getType (), getFullText (), getValueType (),
getValueText (this)));
attach (text, "condition", conditionText, condition);
if (condition.getBoolean ())
attach (text, "true", expTrue, textTrue);
attach (text, "true", textTrue, expTrue);
else
attach (text, "false", expFalse, textFalse);
attach (text, "false", textFalse, expFalse);
return text.toString ();
}
private void attach (StringBuilder text, String title, Value exp, String s)
{
text.append (String.format (FMT4, title, s, exp.getValueType (), getValueText (exp)));
if (exp.size () > 1)
for (Value value : exp)
text.append (value);
}
}

View File

@@ -55,12 +55,8 @@ class Lookup extends ValueListFunction
Address adjacentAddress = isVertical ? target.nextColumn () : target.nextRow ();
if (cell.cellExists (adjacentAddress))
{
value = cell.getCell (adjacentAddress).getDouble ();
}
else
{
value = 0;
}
}
}

View File

@@ -19,12 +19,6 @@ class Number extends AbstractValue
}
}
@Override
public String getText ()
{
return value + "";
}
@Override
public String getType ()
{
@@ -36,8 +30,7 @@ class Number extends AbstractValue
{
StringBuilder text = new StringBuilder ();
text.append (String.format ("%s%n", LINE));
text.append (
String.format (FMT4, "Constant", getFullText (), valueType, getValueText (this)));
attach (text, getType (), getFullText (), this);
return text.toString ();
}
}

View File

@@ -14,6 +14,13 @@ class Or extends ConditionListFunction
for (Value condition : conditions)
{
condition.calculate ();
if (condition.getValueType () != ValueType.BOOLEAN)
{
valueResult = ValueResult.ERROR;
return;
}
if (condition.getBoolean ())
{
bool = true;

View File

@@ -386,38 +386,41 @@ public class Sheet
}
}
List<String> counts = new ArrayList<String> ();
for (int i = 0; i < functionTotals.length; i++)
if (functionTotals[i] > 0)
{
String name = Function.functionList[i];
if (name.endsWith ("("))
name = name.substring (0, name.length () - 1);
counts.add (String.format ("%-10s%d", name, functionTotals[i]));
}
if (debug)
{
List<String> counts = new ArrayList<String> ();
for (int i = 0; i < functionTotals.length; i++)
if (functionTotals[i] > 0)
{
String name = Function.functionList[i];
if (name.endsWith ("("))
name = name.substring (0, name.length () - 1);
counts.add (String.format ("%-10s%d", name, functionTotals[i]));
}
while (counts.size () < 18)
counts.add ("");
while (counts.size () < 18)
counts.add ("");
text.append (String.format ("+%-83.83s+%n", underline));
text.append (String.format ("| Global format : %-18s %-14s %-14s %-14s |%n",
globalFormat, counts.get (0), counts.get (6), counts.get (12)));
text.append (String.format ("| Column width : %-2d %-15s %-14s %-14s %-14s |%n",
columnWidth, "", counts.get (1), counts.get (7), counts.get (13)));
text.append (String.format ("| Recalc order : %-18s %-14s %-14s %-14s |%n",
recalculationOrder == 'R' ? "Row" : "Column", counts.get (2), counts.get (8),
counts.get (14)));
text.append (String.format ("| Recalculation : %-18s %-14s %-14s %-14s |%n",
recalculation == 'A' ? "Automatic" : "Manual", counts.get (3), counts.get (9),
counts.get (15)));
text.append (String.format ("| Cells : %-5d %-11s %-14s %-14s %-14s |%n",
size (), "", counts.get (4), counts.get (10), counts.get (16)));
text.append (String.format ("+%-83.83s+%n", underline));
text.append (String.format ("| Global format : %-18s %-14s %-14s %-14s |%n",
globalFormat, counts.get (0), counts.get (6), counts.get (12)));
text.append (String.format ("| Column width : %-2d %-15s %-14s %-14s %-14s |%n",
columnWidth, "", counts.get (1), counts.get (7), counts.get (13)));
text.append (String.format ("| Recalc order : %-18s %-14s %-14s %-14s |%n",
recalculationOrder == 'R' ? "Row" : "Column", counts.get (2), counts.get (8),
counts.get (14)));
text.append (String.format ("| Recalculation : %-18s %-14s %-14s %-14s |%n",
recalculation == 'A' ? "Automatic" : "Manual", counts.get (3), counts.get (9),
counts.get (15)));
text.append (String.format ("| Cells : %-5d %-11s %-14s %-14s %-14s |%n",
size (), "", counts.get (4), counts.get (10), counts.get (16)));
String rangeText = size () > 0 ? Address.getCellName (minRow + 1, minColumn) + ":"
+ Address.getCellName (maxRow + 1, maxColumn) : "";
text.append (String.format ("| Range : %-18s %-14s %-14s %-14s |%n",
rangeText, counts.get (5), counts.get (11), counts.get (17)));
text.append (String.format ("+%-83.83s+%n", underline));
String rangeText = size () > 0 ? Address.getCellName (minRow + 1, minColumn) + ":"
+ Address.getCellName (maxRow + 1, maxColumn) : "";
text.append (String.format ("| Range : %-18s %-14s %-14s %-14s |%n",
rangeText, counts.get (5), counts.get (11), counts.get (17)));
text.append (String.format ("+%-83.83s+%n", underline));
}
if (debug)
{

View File

@@ -4,13 +4,14 @@ public abstract class ValueFunction extends Function
{
protected Value source;
abstract double calculateValue ();
ValueFunction (Cell cell, String text)
{
super (cell, text);
source = cell.getExpressionValue (functionText);
values.add (source);
// is valueType NUMBER?
}
@Override
@@ -32,8 +33,6 @@ public abstract class ValueFunction extends Function
valueResult = ValueResult.ERROR;
}
abstract double calculateValue ();
@Override
public String getType ()
{