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

173 lines
5.4 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;
2017-03-23 13:30:41 +00:00
import java.util.regex.Pattern;
2016-08-01 01:22:34 +00:00
2016-03-13 03:59:19 +00:00
// Predicate
2020-02-10 11:05:40 +00:00
// -----------------------------------------------------------------------------------//
2017-02-26 10:44:10 +00:00
class Condition extends AbstractValue implements Iterable<Value>
2020-02-10 11:05:40 +00:00
// -----------------------------------------------------------------------------------//
2016-03-12 22:33:18 +00:00
{
2017-03-23 13:30:41 +00:00
private static final Pattern cellAddress = Pattern.compile ("[A-B]?[A-Z][0-9]{1,3}");
2016-03-12 22:38:03 +00:00
private static final String[] comparators = { "<>", "<=", ">=", "=", "<", ">" };
2016-03-12 22:33:18 +00:00
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
2017-03-26 09:16:36 +00:00
private Value conditionExpression;
private Value valueExpression;
2016-03-12 22:33:18 +00:00
2020-02-10 11:05:40 +00:00
// ---------------------------------------------------------------------------------//
Condition (Cell cell, String text)
// ---------------------------------------------------------------------------------//
2016-03-12 22:33:18 +00:00
{
2017-03-23 13:30:41 +00:00
super (cell, text);
valueType = ValueType.BOOLEAN;
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);
2017-03-19 01:03:57 +00:00
conditionExpression = new Expression (cell, conditionText);
2017-03-03 23:41:08 +00:00
values.add (conditionExpression);
2017-03-17 11:03:56 +00:00
2016-03-12 22:33:18 +00:00
comparator = comp;
2017-03-17 11:03:56 +00:00
valueText = text.substring (pos + comp.length ());
2017-03-19 01:03:57 +00:00
valueExpression = new Expression (cell, valueText);
2017-03-17 11:03:56 +00:00
values.add (valueExpression);
2016-03-12 22:33:18 +00:00
break;
}
}
if (comparator == null)
2017-03-26 09:16:36 +00:00
if (text.startsWith ("@") || cellAddress.matcher (text).matches ())
2017-03-23 13:30:41 +00:00
{
conditionText = text;
2017-03-26 09:16:36 +00:00
conditionExpression = new Expression (cell, text).reduce ();
2017-03-23 13:30:41 +00:00
values.add (conditionExpression);
2016-03-12 22:33:18 +00:00
}
else
2017-03-26 09:16:36 +00:00
throw new IllegalArgumentException (
"No comparator and not a function or address: " + text);
2016-03-12 22:33:18 +00:00
}
2020-02-10 11:05:40 +00:00
// ---------------------------------------------------------------------------------//
2017-02-26 10:44:10 +00:00
@Override
2017-02-28 20:39:26 +00:00
public void calculate ()
2020-02-10 11:05:40 +00:00
// ---------------------------------------------------------------------------------//
2016-03-12 22:33:18 +00:00
{
2017-03-23 13:30:41 +00:00
valueResult = ValueResult.VALID;
2017-02-26 10:44:10 +00:00
2017-03-03 23:41:08 +00:00
conditionExpression.calculate ();
2017-03-23 13:30:41 +00:00
if (!conditionExpression.isValid ())
{
valueResult = conditionExpression.getValueResult ();
return;
}
2016-03-12 22:33:18 +00:00
2017-03-23 13:30:41 +00:00
// a boolean won't have a comparator or a valueExpression
if (conditionExpression.getValueType () == ValueType.BOOLEAN)
{
bool = conditionExpression.getBoolean ();
2017-02-28 20:39:26 +00:00
return;
2017-03-23 13:30:41 +00:00
}
2016-03-16 19:32:25 +00:00
2017-03-23 13:30:41 +00:00
valueExpression.calculate ();
if (!valueExpression.isValid ())
{
valueResult = valueExpression.getValueResult ();
return;
}
double conditionResult = conditionExpression.getDouble ();
double expressionResult = valueExpression.getDouble ();
2016-03-12 22:33:18 +00:00
if (comparator.equals ("="))
2017-03-23 13:30:41 +00:00
bool = conditionResult == expressionResult;
2016-03-12 22:33:18 +00:00
else if (comparator.equals ("<>"))
2017-03-23 13:30:41 +00:00
bool = conditionResult != expressionResult;
2016-03-12 22:33:18 +00:00
else if (comparator.equals ("<"))
2017-03-23 13:30:41 +00:00
bool = conditionResult < expressionResult;
2016-03-12 22:33:18 +00:00
else if (comparator.equals (">"))
2017-03-23 13:30:41 +00:00
bool = conditionResult > expressionResult;
2016-03-12 22:33:18 +00:00
else if (comparator.equals ("<="))
2017-03-23 13:30:41 +00:00
bool = conditionResult <= expressionResult;
2016-03-12 22:33:18 +00:00
else if (comparator.equals (">="))
2017-03-23 13:30:41 +00:00
bool = conditionResult >= expressionResult;
2016-03-12 22:33:18 +00:00
else
System.out.printf ("Unexpected comparator result [%s]%n", comparator);
}
2020-02-10 11:05:40 +00:00
// ---------------------------------------------------------------------------------//
2017-03-23 13:30:41 +00:00
@Override
public String getFullText ()
2020-02-10 11:05:40 +00:00
// ---------------------------------------------------------------------------------//
2017-03-03 23:41:08 +00:00
{
return fullText;
}
2020-02-10 11:05:40 +00:00
// ---------------------------------------------------------------------------------//
2016-03-12 22:33:18 +00:00
@Override
2017-03-23 13:30:41 +00:00
public String getType ()
2020-02-10 11:05:40 +00:00
// ---------------------------------------------------------------------------------//
2016-03-12 22:33:18 +00:00
{
2017-03-23 13:30:41 +00:00
return "Condition";
}
2020-02-10 11:05:40 +00:00
// ---------------------------------------------------------------------------------//
2017-03-23 13:30:41 +00:00
static boolean isCondition (String text)
2020-02-10 11:05:40 +00:00
// ---------------------------------------------------------------------------------//
2017-03-23 13:30:41 +00:00
{
int ptr = 0;
int depth = 0;
while (ptr < text.length ())
{
char c = text.charAt (ptr);
if (c == '(')
++depth;
else if (c == ')')
--depth;
else if (depth == 0 && (c == '=' || c == '<' || c == '>'))
return true;
++ptr;
}
return false;
2016-03-12 22:33:18 +00:00
}
2017-02-26 10:44:10 +00:00
2020-02-10 11:05:40 +00:00
// ---------------------------------------------------------------------------------//
2017-02-26 10:44:10 +00:00
@Override
public Iterator<Value> iterator ()
2020-02-10 11:05:40 +00:00
// ---------------------------------------------------------------------------------//
2017-02-26 10:44:10 +00:00
{
return values.iterator ();
}
2017-03-23 13:30:41 +00:00
2020-02-10 11:05:40 +00:00
// ---------------------------------------------------------------------------------//
2017-03-23 13:30:41 +00:00
@Override
public String toString ()
2020-02-10 11:05:40 +00:00
// ---------------------------------------------------------------------------------//
2017-03-23 13:30:41 +00:00
{
StringBuilder text = new StringBuilder ();
2017-03-26 09:16:36 +00:00
2017-03-24 02:00:11 +00:00
text.append (LINE + "\n");
text.append (String.format (FMT4, "Predicate", getFullText (), valueType,
getValueText (this)));
text.append (String.format (FMT4, "Left", conditionText,
conditionExpression.getValueType (), getValueText (conditionExpression)));
2017-03-23 13:30:41 +00:00
if (comparator != null)
{
2017-03-24 02:00:11 +00:00
text.append (String.format (FMT2, "Comparatr", comparator));
text.append (String.format (FMT4, "Right", valueText,
valueExpression.getValueType (), getValueText (valueExpression)));
2017-03-23 13:30:41 +00:00
}
2017-03-26 09:16:36 +00:00
2017-03-23 13:30:41 +00:00
return text.toString ();
}
2016-03-12 22:33:18 +00:00
}