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

105 lines
2.6 KiB
Java
Raw Normal View History

2016-03-12 22:33:18 +00:00
package com.bytezone.diskbrowser.visicalc;
2017-02-26 10:44:10 +00:00
import java.util.Iterator;
2016-08-01 01:22:34 +00:00
2016-03-13 03:59:19 +00:00
// Predicate
2017-02-26 10:44:10 +00:00
class Condition extends AbstractValue implements Iterable<Value>
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;
2017-02-26 10:44:10 +00:00
protected String fullText;
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-26 10:44:10 +00:00
super ("Cond");
2016-03-12 22:33:18 +00:00
this.parent = parent;
2017-02-26 10:44:10 +00:00
fullText = text;
2016-03-12 22:33:18 +00:00
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");
}
}
2017-02-26 10:44:10 +00:00
@Override
public Value calculate ()
2016-03-12 22:33:18 +00:00
{
2017-02-26 10:44:10 +00:00
value = 0;
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
2017-02-26 10:44:10 +00:00
values.add (conditionExpression);
values.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))
2017-02-26 10:44:10 +00:00
return this;
2016-03-16 19:32:25 +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 ("="))
2017-02-26 10:44:10 +00:00
value = conditionResult == valueResult ? 1 : 0;
2016-03-12 22:33:18 +00:00
else if (comparator.equals ("<>"))
2017-02-26 10:44:10 +00:00
value = conditionResult != valueResult ? 1 : 0;
2016-03-12 22:33:18 +00:00
else if (comparator.equals ("<"))
2017-02-26 10:44:10 +00:00
value = conditionResult < valueResult ? 1 : 0;
2016-03-12 22:33:18 +00:00
else if (comparator.equals (">"))
2017-02-26 10:44:10 +00:00
value = conditionResult > valueResult ? 1 : 0;
2016-03-12 22:33:18 +00:00
else if (comparator.equals ("<="))
2017-02-26 10:44:10 +00:00
value = conditionResult <= valueResult ? 1 : 0;
2016-03-12 22:33:18 +00:00
else if (comparator.equals (">="))
2017-02-26 10:44:10 +00:00
value = conditionResult >= valueResult ? 1 : 0;
2016-03-12 22:33:18 +00:00
else
System.out.printf ("Unexpected comparator result [%s]%n", comparator);
2017-02-26 10:44:10 +00:00
return this;
2016-03-12 22:33:18 +00:00
}
@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
}
2017-02-26 10:44:10 +00:00
@Override
public Iterator<Value> iterator ()
{
return values.iterator ();
}
2016-03-12 22:33:18 +00:00
}