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

108 lines
3.3 KiB
Java
Raw Permalink Normal View History

2016-03-09 10:38:53 +00:00
package com.bytezone.diskbrowser.visicalc;
2020-02-10 11:05:40 +00:00
// -----------------------------------------------------------------------------------//
2016-03-12 22:38:03 +00:00
class If extends Function
2020-02-10 11:05:40 +00:00
// -----------------------------------------------------------------------------------//
2016-03-09 10:38:53 +00:00
{
2017-03-14 11:28:52 +00:00
private final String conditionText;
2016-03-09 10:38:53 +00:00
private final String textTrue;
private final String textFalse;
2017-03-14 11:28:52 +00:00
private final Condition condition;
2017-03-23 13:30:41 +00:00
private final Value expTrue;
private final Value expFalse;
2016-03-10 02:39:23 +00:00
2020-02-10 11:05:40 +00:00
// ---------------------------------------------------------------------------------//
If (Cell cell, String text)
// ---------------------------------------------------------------------------------//
2016-03-09 10:38:53 +00:00
{
super (cell, text);
2016-03-09 10:38:53 +00:00
2017-03-18 09:21:11 +00:00
assert text.startsWith ("@IF(") : text;
2017-03-17 23:57:48 +00:00
conditionText = Expression.getParameter (functionText);
2017-03-23 13:30:41 +00:00
int ptr = conditionText.length () + 1;
if (ptr >= functionText.length ())
throw new IllegalArgumentException (text);
textTrue = Expression.getParameter (functionText.substring (ptr));
ptr = conditionText.length () + textTrue.length () + 2;
if (ptr >= functionText.length ())
throw new IllegalArgumentException (text);
textFalse = Expression.getParameter (functionText.substring (ptr));
2016-03-16 06:15:39 +00:00
2017-03-19 09:52:36 +00:00
condition = new Condition (cell, conditionText);
2017-02-26 10:44:10 +00:00
values.add (condition);
2016-03-16 06:15:39 +00:00
2017-03-23 13:30:41 +00:00
expTrue = new Expression (cell, textTrue).reduce ();
2017-03-03 23:41:08 +00:00
values.add (expTrue);
2017-03-14 11:28:52 +00:00
2017-03-23 13:30:41 +00:00
expFalse = new Expression (cell, textFalse).reduce ();
2017-03-03 23:41:08 +00:00
values.add (expFalse);
2017-03-23 13:30:41 +00:00
valueType = expTrue.getValueType ();
2016-03-09 10:38:53 +00:00
}
2020-02-10 11:05:40 +00:00
// ---------------------------------------------------------------------------------//
2016-03-09 10:38:53 +00:00
@Override
2017-02-28 20:39:26 +00:00
public void calculate ()
2020-02-10 11:05:40 +00:00
// ---------------------------------------------------------------------------------//
2016-03-09 10:38:53 +00:00
{
2017-03-23 13:30:41 +00:00
valueResult = ValueResult.VALID;
2017-02-26 10:44:10 +00:00
condition.calculate ();
2016-03-16 19:32:25 +00:00
2017-03-23 13:30:41 +00:00
if (condition.getBoolean ()) // true
2016-03-10 02:39:23 +00:00
{
2016-03-19 05:31:30 +00:00
expTrue.calculate ();
2017-03-23 13:30:41 +00:00
if (!expTrue.isValid ())
valueResult = expTrue.getValueResult ();
2017-03-25 08:02:02 +00:00
else if (valueType == ValueType.NUMBER)
2017-03-23 13:30:41 +00:00
value = expTrue.getDouble ();
2017-03-25 08:02:02 +00:00
else
bool = expTrue.getBoolean ();
2016-03-10 02:39:23 +00:00
}
2017-03-23 13:30:41 +00:00
else // false
2016-03-10 02:39:23 +00:00
{
2016-03-19 05:31:30 +00:00
expFalse.calculate ();
2017-03-23 13:30:41 +00:00
if (!expFalse.isValid ())
valueResult = expTrue.getValueResult ();
2017-03-25 08:02:02 +00:00
else if (valueType == ValueType.NUMBER)
2017-03-23 13:30:41 +00:00
value = expFalse.getDouble ();
2017-03-25 08:02:02 +00:00
else
bool = expFalse.getBoolean ();
2016-03-10 02:39:23 +00:00
}
2016-03-09 10:38:53 +00:00
}
2016-03-14 08:58:54 +00:00
2020-02-10 11:05:40 +00:00
// ---------------------------------------------------------------------------------//
2017-03-23 13:30:41 +00:00
@Override
public String getType ()
2020-02-10 11:05:40 +00:00
// ---------------------------------------------------------------------------------//
2017-03-23 13:30:41 +00:00
{
return "If";
}
2020-02-10 11:05:40 +00:00
// ---------------------------------------------------------------------------------//
2016-03-14 08:58:54 +00:00
@Override
public String toString ()
2020-02-10 11:05:40 +00:00
// ---------------------------------------------------------------------------------//
2016-03-14 08:58:54 +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 (String.format ("%s%n", LINE));
2017-03-25 08:02:02 +00:00
text.append (String.format (FMT4, getType (), getFullText (), getValueType (),
getValueText (this)));
attach (text, "condition", conditionText, condition);
2017-03-24 02:00:11 +00:00
if (condition.getBoolean ())
2017-03-25 08:02:02 +00:00
attach (text, "true", textTrue, expTrue);
2017-03-24 02:00:11 +00:00
else
2017-03-25 08:02:02 +00:00
attach (text, "false", textFalse, expFalse);
2017-03-26 09:16:36 +00:00
2017-03-23 13:30:41 +00:00
return text.toString ();
2016-03-14 08:58:54 +00:00
}
2016-03-09 10:38:53 +00:00
}