dmolony-DiskBrowser/src/com/bytezone/diskbrowser/visicalc/Condition.java

81 lines
2.0 KiB
Java
Raw Normal View History

2016-03-12 22:33:18 +00:00
package com.bytezone.diskbrowser.visicalc;
2016-03-13 03:59:19 +00:00
// Predicate
2016-03-12 22:38:03 +00:00
class Condition
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)
{
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-12 22:33:18 +00:00
}
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,
valueText);
2016-03-12 22:33:18 +00:00
}
}