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

180 lines
4.9 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
2017-02-26 10:44:10 +00:00
class Condition extends AbstractValue implements Iterable<Value>
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;
2017-03-03 23:41:08 +00:00
private final String fullText;
2017-03-23 13:30:41 +00:00
// private Address address;
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
2017-03-19 09:52:36 +00:00
public 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;
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);
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)
{
if (text.startsWith ("@"))
{
2016-03-13 03:59:19 +00:00
conditionText = text;
2017-03-19 01:03:57 +00:00
conditionExpression = new Expression (cell, text);
2017-03-17 11:03:56 +00:00
values.add (conditionExpression);
2017-03-23 13:30:41 +00:00
// comparator = "=";
//
// valueText = "1";
// valueExpression = new Expression (cell, valueText);
// values.add (valueExpression);
}
else if (cellAddress.matcher (text).matches ())
{
conditionText = text;
conditionExpression = new Expression (cell, text);
conditionExpression.valueType = ValueType.BOOLEAN;
values.add (conditionExpression);
2016-03-12 22:33:18 +00:00
}
else
2017-03-23 13:30:41 +00:00
{
System.out.println ("No comparator and not a function: " + text);
throw new IllegalArgumentException ("No comparator and not a function: " + text);
}
2016-03-12 22:33:18 +00:00
}
}
2017-02-26 10:44:10 +00:00
@Override
2017-02-28 20:39:26 +00:00
public void calculate ()
2016-03-12 22:33:18 +00:00
{
2017-03-23 13:30:41 +00:00
// System.out.printf ("********Calc: %s%n", fullText);
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 ();
// System.out.printf ("********Bool: %s%n", bool);
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);
2017-03-23 13:30:41 +00:00
// System.out.printf ("********Bool: %s%n", bool);
2016-03-12 22:33:18 +00:00
}
2017-03-23 13:30:41 +00:00
@Override
public String getFullText ()
2017-03-03 23:41:08 +00:00
{
return fullText;
}
2016-03-12 22:33:18 +00:00
@Override
2017-03-23 13:30:41 +00:00
public String getType ()
2016-03-12 22:33:18 +00:00
{
2017-03-23 13:30:41 +00:00
return "Condition";
}
static boolean isCondition (String text)
{
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
@Override
public Iterator<Value> iterator ()
{
return values.iterator ();
}
2017-03-23 13:30:41 +00:00
@Override
public String toString ()
{
String line = "+-------------------------------------------------------------+";
StringBuilder text = new StringBuilder ();
text.append (line + "\n");
text.append (String.format ("| %-10.10s: CND : %-34.34s%-8.8s|%n",
cell.getAddressText (), getFullText (), valueType));
text.append (String.format ("| %-10.10s: %-40.40s%-8.8s|%n", "Condition",
conditionText, conditionExpression.getValueType ()));
if (comparator != null)
{
text.append (String.format ("| %-10.10s: %-60.60s|%n", "Comparatr", comparator));
text.append (String.format ("| %-10.10s: %-40.40s%-8.8s|%n", "Value", valueText,
valueExpression.getValueType ()));
}
text.append (line);
return text.toString ();
}
2016-03-12 22:33:18 +00:00
}