2016-03-12 22:33:18 +00:00
|
|
|
package com.bytezone.diskbrowser.visicalc;
|
|
|
|
|
2016-08-01 01:22:34 +00:00
|
|
|
import com.bytezone.diskbrowser.visicalc.Value.ValueType;
|
|
|
|
|
2016-03-13 03:59:19 +00:00
|
|
|
// Predicate
|
2017-02-25 10:30:56 +00:00
|
|
|
class Condition //extends AbstractValue
|
2016-03-12 22:33:18 +00:00
|
|
|
{
|
2016-03-12 22:38:03 +00:00
|
|
|
private static final String[] comparators = { "<>", "<=", ">=", "=", "<", ">" };
|
2016-03-12 22:33:18 +00:00
|
|
|
|
|
|
|
private final Sheet parent;
|
|
|
|
|
|
|
|
private String comparator;
|
2016-03-13 03:59:19 +00:00
|
|
|
private String conditionText;
|
|
|
|
private String valueText;
|
2016-03-12 22:33:18 +00:00
|
|
|
|
2016-03-13 03:59:19 +00:00
|
|
|
private Expression conditionExpression;
|
|
|
|
private Expression valueExpression;
|
2016-03-12 22:33:18 +00:00
|
|
|
|
|
|
|
public Condition (Sheet parent, String text)
|
|
|
|
{
|
2017-02-25 10:30:56 +00:00
|
|
|
// super ("Condition");
|
2016-03-12 22:33:18 +00:00
|
|
|
this.parent = parent;
|
|
|
|
|
|
|
|
for (String comp : comparators)
|
|
|
|
{
|
|
|
|
int pos = text.indexOf (comp);
|
|
|
|
if (pos > 0)
|
|
|
|
{
|
2016-03-13 03:59:19 +00:00
|
|
|
conditionText = text.substring (0, pos);
|
|
|
|
valueText = text.substring (pos + comp.length ());
|
2016-03-12 22:33:18 +00:00
|
|
|
comparator = comp;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (comparator == null)
|
|
|
|
{
|
|
|
|
if (text.startsWith ("@"))
|
|
|
|
{
|
2016-03-13 03:59:19 +00:00
|
|
|
conditionText = text;
|
|
|
|
valueText = "1";
|
2016-03-12 22:33:18 +00:00
|
|
|
comparator = "=";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
System.out.println ("No comparator and not a function");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean getResult ()
|
|
|
|
{
|
2016-03-13 03:59:19 +00:00
|
|
|
if (conditionExpression == null)
|
2016-03-12 22:33:18 +00:00
|
|
|
{
|
2016-03-13 03:59:19 +00:00
|
|
|
conditionExpression = new Expression (parent, conditionText);
|
|
|
|
valueExpression = new Expression (parent, valueText);
|
2016-03-16 19:32:25 +00:00
|
|
|
|
|
|
|
conditionExpression.calculate ();
|
|
|
|
valueExpression.calculate ();
|
2017-02-25 03:56:22 +00:00
|
|
|
|
|
|
|
// expressions.add (conditionExpression);
|
|
|
|
// expressions.add (valueExpression);
|
2016-03-12 22:33:18 +00:00
|
|
|
}
|
|
|
|
|
2017-02-25 03:56:22 +00:00
|
|
|
if (conditionExpression.isValueType (ValueType.ERROR)
|
|
|
|
|| valueExpression.isValueType (ValueType.ERROR))
|
2016-03-16 19:32:25 +00:00
|
|
|
return false;
|
|
|
|
|
2016-03-13 03:59:19 +00:00
|
|
|
double conditionResult = conditionExpression.getValue ();
|
|
|
|
double valueResult = valueExpression.getValue ();
|
2016-03-12 22:33:18 +00:00
|
|
|
|
|
|
|
if (comparator.equals ("="))
|
2016-03-13 03:59:19 +00:00
|
|
|
return conditionResult == valueResult;
|
2016-03-12 22:33:18 +00:00
|
|
|
else if (comparator.equals ("<>"))
|
2016-03-13 03:59:19 +00:00
|
|
|
return conditionResult != valueResult;
|
2016-03-12 22:33:18 +00:00
|
|
|
else if (comparator.equals ("<"))
|
2016-03-13 03:59:19 +00:00
|
|
|
return conditionResult < valueResult;
|
2016-03-12 22:33:18 +00:00
|
|
|
else if (comparator.equals (">"))
|
2016-03-13 03:59:19 +00:00
|
|
|
return conditionResult > valueResult;
|
2016-03-12 22:33:18 +00:00
|
|
|
else if (comparator.equals ("<="))
|
2016-03-13 03:59:19 +00:00
|
|
|
return conditionResult <= valueResult;
|
2016-03-12 22:33:18 +00:00
|
|
|
else if (comparator.equals (">="))
|
2016-03-13 03:59:19 +00:00
|
|
|
return conditionResult >= valueResult;
|
2016-03-12 22:33:18 +00:00
|
|
|
else
|
|
|
|
System.out.printf ("Unexpected comparator result [%s]%n", comparator);
|
|
|
|
|
|
|
|
return false; // flag error?
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString ()
|
|
|
|
{
|
2016-03-13 03:59:19 +00:00
|
|
|
return String.format ("[cond=%s, op=%s, value=%s]", conditionText, comparator,
|
2017-02-25 03:56:22 +00:00
|
|
|
valueText);
|
2016-03-12 22:33:18 +00:00
|
|
|
}
|
|
|
|
}
|