This commit is contained in:
Denis Molony 2016-03-13 14:59:19 +11:00
parent 33e701d4a3
commit f7e9854dc8
2 changed files with 23 additions and 20 deletions

View File

@ -1,5 +1,6 @@
package com.bytezone.diskbrowser.visicalc; package com.bytezone.diskbrowser.visicalc;
// Predicate
class Condition class Condition
{ {
private static final String[] comparators = { "<>", "<=", ">=", "=", "<", ">" }; private static final String[] comparators = { "<>", "<=", ">=", "=", "<", ">" };
@ -7,11 +8,11 @@ class Condition
private final Sheet parent; private final Sheet parent;
private String comparator; private String comparator;
private String cond; private String conditionText;
private String value; private String valueText;
private Expression expCond; private Expression conditionExpression;
private Expression expValue; private Expression valueExpression;
public Condition (Sheet parent, String text) public Condition (Sheet parent, String text)
{ {
@ -22,8 +23,8 @@ class Condition
int pos = text.indexOf (comp); int pos = text.indexOf (comp);
if (pos > 0) if (pos > 0)
{ {
cond = text.substring (0, pos); conditionText = text.substring (0, pos);
value = text.substring (pos + comp.length ()); valueText = text.substring (pos + comp.length ());
comparator = comp; comparator = comp;
break; break;
} }
@ -33,8 +34,8 @@ class Condition
{ {
if (text.startsWith ("@")) if (text.startsWith ("@"))
{ {
cond = text; conditionText = text;
value = "1"; valueText = "1";
comparator = "="; comparator = "=";
} }
else else
@ -44,27 +45,27 @@ class Condition
public boolean getResult () public boolean getResult ()
{ {
if (expCond == null) if (conditionExpression == null)
{ {
expCond = new Expression (parent, cond); conditionExpression = new Expression (parent, conditionText);
expValue = new Expression (parent, value); valueExpression = new Expression (parent, valueText);
} }
double condValue = expCond.getValue (); double conditionResult = conditionExpression.getValue ();
double valueValue = expValue.getValue (); double valueResult = valueExpression.getValue ();
if (comparator.equals ("=")) if (comparator.equals ("="))
return condValue == valueValue; return conditionResult == valueResult;
else if (comparator.equals ("<>")) else if (comparator.equals ("<>"))
return condValue != valueValue; return conditionResult != valueResult;
else if (comparator.equals ("<")) else if (comparator.equals ("<"))
return condValue < valueValue; return conditionResult < valueResult;
else if (comparator.equals (">")) else if (comparator.equals (">"))
return condValue > valueValue; return conditionResult > valueResult;
else if (comparator.equals ("<=")) else if (comparator.equals ("<="))
return condValue <= valueValue; return conditionResult <= valueResult;
else if (comparator.equals (">=")) else if (comparator.equals (">="))
return condValue >= valueValue; return conditionResult >= valueResult;
else else
System.out.printf ("Unexpected comparator result [%s]%n", comparator); System.out.printf ("Unexpected comparator result [%s]%n", comparator);
@ -74,6 +75,7 @@ class Condition
@Override @Override
public String toString () public String toString ()
{ {
return String.format ("[cond=%s, op=%s, value=%s]", cond, comparator, value); return String.format ("[cond=%s, op=%s, value=%s]", conditionText, comparator,
valueText);
} }
} }

View File

@ -177,6 +177,7 @@ class Expression implements Value
{ {
System.out.printf ("**** Unbalanced brackets: left:%d, right:%d ****%n", System.out.printf ("**** Unbalanced brackets: left:%d, right:%d ****%n",
leftBracket, rightBracket); leftBracket, rightBracket);
System.out.println (input);
return "@ERROR()"; return "@ERROR()";
} }
return line; return line;